spring schedule
2016-06-08 13:36
344 查看
package spring.schedule;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class AppListener implements ServletContextListener {private final static Logger LOGGER
= LoggerFactory.getLogger(AppListener.class);@Overridepublic void contextInitialized(ServletContextEvent sce) {// System.out.println("***************自定义监听器!!!***************");LOGGER.info("***************自定义监听器!!!***************");}@Overridepublic void contextDestroyed(ServletContextEvent
sce) {// TODO Auto-generated method stub}}
package spring.schedule;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Job1 extends QuartzJobBean {
private int timeout;
private static int i = 0;
//调度工厂实例化后,经过timeout时间开始执行调度
public void setTimeout(int timeout) {
this.timeout = timeout;
}
/**
* 要调度的具体任务
*/
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("job1----------定时任务执行中…");
}
}
package spring.schedule;
public class Job2 {
public void doJob2() {
System.out.println("不继承QuartzJobBean方式-调度进行中...");
}
}
package spring.schedule;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Title:
* @Description:
* @Team: 技术1部Java开发小组
* @Author zhuolin ji
* @Date 2016年6月7日 下午2:08:48
* @Version V1.0
* @param <T> */
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scheduled
{
public abstract String cron();
/**
* An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
* @return
*/
public abstract long fixedDelay();
/**
* An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
* @return
*/
public abstract long fixedRate();
}
package spring.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Service
@Component("taskJob")
public class TaskJob {
@Scheduled(cron = "0 0 3 * * ?")
public void job1() {
System.out.println("Taskjob-----------任务进行中。。。");
}
}
package spring.schedule;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author alvin
*/
@SuppressWarnings("all")
public class WebAppContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b debug(\u663e\u793aSQL) \u548c\u8f93\u51fa\u76ee\u7684\u5730 ###
log_home=../logs
log4j.rootLogger=info,Schedule,stdout,logfile
### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[Schedule] - %d{yyyy-MM-dd HH:mm:ss,SSS} %p %C{2}.%M(%L).[%t] | %m%n
### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u6587\u4ef6\uff1ajbit.log ###
#log4j.appender.logfile=org.apache.log4j.FileAppender
#log4j.appender.logfile.File=jbit.log
#log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
log4j.appender.Schedule.File=${log_home}/Schedule
log4j.appender.Schedule.Encoding=UTF-8
log4j.appender.Schedule.Append=true
log4j.appender.Schedule.Threshold=INFO
log4j.appender.Schedule.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.Schedule.layout=org.apache.log4j.PatternLayout
log4j.appender.Schedule.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %c [%t] %-5p %c %x - %m%n
###\u5f00\u53d1\u73af\u5883\u4e0b\u4f7f\u7528\uff1adebug \u6a21\u5f0f\u4e0b\uff0c\u663e\u793aSQL\u8bed\u53e5\u90e8\u5206
#log4j.logger.com.ibatis=debug
#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
#log4j.logger.java.sql.Connection=debug
#log4j.logger.java.sql.Statement=debug
#log4j.logger.java.sql.PreparedStatement=debug
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd -->
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath*:log4j.properties</value>
<!-- <value>classpath*:jdbc_mysql_db.properties</value>
<value>classpath*:const.properties</value>
<value>classpath*:redis-mq-log.properties</value> -->
</list>
</property>
</bean>
<bean class="spring.schedule.WebAppContextUtil"/>
<context:annotation-config />
<!--SpringMvc启用注解映射的支持 -->
<!-- context:annotation-config / -->
<!-- bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" / -->
<!-- bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" / -->
<!-- spring扫描注解的配置 -->
<context:component-scan base-package="spring.schedule" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<import resource="classpath*:spring-quartz-jobs.xml"/>
<!-- 开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
</task:scheduled-tasks>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-lazy-init="false">
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="spring.schedule.Job1" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="spring.schedule.Job2" />
</property>
<property name="targetMethod" value="doJob2" />
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<!-- *************调度工厂***************** -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
<!-- ***************时间间隔调度*************** -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job2" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>
<!-- **************定时调度****************** -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job1" />
<!—每天12:00运行一次 -->
<property name="cronExpression" value="0 0 12 * * ?" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job2" />
<!—每天12:00运行一次 -->
<property name="cronExpression" value="0 0 12 * * ?" />
</bean>
</beans>
= LoggerFactory.getLogger(AppListener.class);@Overridepublic void contextInitialized(ServletContextEvent sce) {// System.out.println("***************自定义监听器!!!***************");LOGGER.info("***************自定义监听器!!!***************");}@Overridepublic void contextDestroyed(ServletContextEvent
sce) {// TODO Auto-generated method stub}}
package spring.schedule;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Job1 extends QuartzJobBean {
private int timeout;
private static int i = 0;
//调度工厂实例化后,经过timeout时间开始执行调度
public void setTimeout(int timeout) {
this.timeout = timeout;
}
/**
* 要调度的具体任务
*/
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("job1----------定时任务执行中…");
}
}
package spring.schedule;
public class Job2 {
public void doJob2() {
System.out.println("不继承QuartzJobBean方式-调度进行中...");
}
}
package spring.schedule;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Title:
* @Description:
* @Team: 技术1部Java开发小组
* @Author zhuolin ji
* @Date 2016年6月7日 下午2:08:48
* @Version V1.0
* @param <T> */
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scheduled
{
public abstract String cron();
/**
* An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
* @return
*/
public abstract long fixedDelay();
/**
* An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
* @return
*/
public abstract long fixedRate();
}
package spring.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Service
@Component("taskJob")
public class TaskJob {
@Scheduled(cron = "0 0 3 * * ?")
public void job1() {
System.out.println("Taskjob-----------任务进行中。。。");
}
}
package spring.schedule;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author alvin
*/
@SuppressWarnings("all")
public class WebAppContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b debug(\u663e\u793aSQL) \u548c\u8f93\u51fa\u76ee\u7684\u5730 ###
log_home=../logs
log4j.rootLogger=info,Schedule,stdout,logfile
### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[Schedule] - %d{yyyy-MM-dd HH:mm:ss,SSS} %p %C{2}.%M(%L).[%t] | %m%n
### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u6587\u4ef6\uff1ajbit.log ###
#log4j.appender.logfile=org.apache.log4j.FileAppender
#log4j.appender.logfile.File=jbit.log
#log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
log4j.appender.Schedule.File=${log_home}/Schedule
log4j.appender.Schedule.Encoding=UTF-8
log4j.appender.Schedule.Append=true
log4j.appender.Schedule.Threshold=INFO
log4j.appender.Schedule.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.Schedule.layout=org.apache.log4j.PatternLayout
log4j.appender.Schedule.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %c [%t] %-5p %c %x - %m%n
###\u5f00\u53d1\u73af\u5883\u4e0b\u4f7f\u7528\uff1adebug \u6a21\u5f0f\u4e0b\uff0c\u663e\u793aSQL\u8bed\u53e5\u90e8\u5206
#log4j.logger.com.ibatis=debug
#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
#log4j.logger.java.sql.Connection=debug
#log4j.logger.java.sql.Statement=debug
#log4j.logger.java.sql.PreparedStatement=debug
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd -->
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath*:log4j.properties</value>
<!-- <value>classpath*:jdbc_mysql_db.properties</value>
<value>classpath*:const.properties</value>
<value>classpath*:redis-mq-log.properties</value> -->
</list>
</property>
</bean>
<bean class="spring.schedule.WebAppContextUtil"/>
<context:annotation-config />
<!--SpringMvc启用注解映射的支持 -->
<!-- context:annotation-config / -->
<!-- bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" / -->
<!-- bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" / -->
<!-- spring扫描注解的配置 -->
<context:component-scan base-package="spring.schedule" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<import resource="classpath*:spring-quartz-jobs.xml"/>
<!-- 开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
</task:scheduled-tasks>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-lazy-init="false">
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="spring.schedule.Job1" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="spring.schedule.Job2" />
</property>
<property name="targetMethod" value="doJob2" />
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<!-- *************调度工厂***************** -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
<!-- ***************时间间隔调度*************** -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job2" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>
<!-- **************定时调度****************** -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job1" />
<!—每天12:00运行一次 -->
<property name="cronExpression" value="0 0 12 * * ?" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job2" />
<!—每天12:00运行一次 -->
<property name="cronExpression" value="0 0 12 * * ?" />
</bean>
</beans>
相关文章推荐
- Java 按行读写文件
- myeclipse运行过程中报错 Java was started ,but return exit code=1
- MVP+RXJAVA+RecyclerView实现sd卡根目录下的所有文件中的照片加载并显示
- java多线程基础概念
- springmvc,mybatis,logback整合相关
- Spring事务传播
- java多线程完美篇
- ActiveMQ实例2--Spring JMS发送消息
- NDK 之 C调用Java
- JNI调用Java方法
- RxJava(01-介绍与初体验)
- RxJava(01-介绍与初体验)
- SMS短信通API——(1)Java应用发送手机短信
- Java图片处理的类库:SimpleImage
- java程序调用xfire发布的webService服务(二)
- Failed to load JavaHL Library
- MyEclipse10.6 myeclipse2013下添加jadClipse反编译插件 .
- java通过反射得到get、set方法
- map的引用传递浅谈
- LinkedHashMap与TreeMap浅析