您的位置:首页 > 编程语言 > Java开发

java项目中使用springmvc的调度任务

2016-09-30 14:37 375 查看
工作闲了,研究下springmvc的调度任务,只使用了定义普通java类的实现方式:

1、定义实现类,由于spring的定时任务需要依赖slf4j,所以要在src下新建

log4j.properties文件:

log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=ldap.log
log4j.appender.logfile.MaxFileSize=15120KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

定义实现类:
package com.jb.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service(value="scheduleService")
@Scope(value="prototype")
public class ScheduleServiceImpl {
/** 日志记录 */
private static Logger logger = LoggerFactory.getLogger(ScheduleServiceImpl.class);
/**
*leip 2016年9月30日
*TODO
**/
public void scheduleTest(){
logger.info("调度日志记录中……");
System.out.println("调度任务执行中……");
}
}


2、配置实现类
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--spring调度任务配置 -->
<!-- SimpleTriggerBean只支持按照一定频度执行,比如每隔30分钟 -->
<!--
<bean id="testTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1" />
<property name="startDelay" value="0" />调度工厂实例化后,经过0秒开始执行调度
<property name="repeatInterval" value="2000" />每2秒调度一次
</bean>
-->
<!-- CronTriggerFactoryBean支持指定时间运行一次,比如每天12点 -->
<bean id="testTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="testDetail" />
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>

<bean id="testDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="scheduleService" />
<property name="targetMethod" value="scheduleTest" />
<property name="concurrent" value="false" />
</bean>

<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="testTrigger" />
</list>
</property>
</bean>

</beans>

所需jar包下载路径:
springmvc调度任务jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: