上一篇讲到用spring配合quartz 进行任务调度,这次还是不例外,仍然是用这两种结合进行处理,但是增加了quartz 的相关时间表达方式。
先来一个简单点的:
项目一
1. 创建一个简单的maven项目,pom 文件的主要内容为:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- Transaction dependency is required with Quartz integration -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- Quartz framework -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
如果熟悉maven的朋友,应该知道上面的代码其实是可以优化的,这里我就不多说了。
2. 创建一个简单的class文件,不需要继承其他类,加上spring的注解:
import org.springframework.stereotype.Component;
@Component("myBean")
public class MyBean {
public void printMessage() {
System.out.println("I am MyBean. I am called by MethodInvokingJobDetailFactoryBean using SimpleTriggerFactoryBean");
}
}
3. spring的配置文件applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.test.job" />
<!-- For times when you just need to invoke a method on a specific object -->
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myBean" />
<property name="targetMethod" value="printMessage" />
</bean>
<!-- Run the job every 2 seconds with initial delay of 1 second -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail" />
<property name="startDelay" value="1000" />
<property name="repeatInterval" value="2000" />
</bean>
<!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="simpleJobDetail" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
</beans>
4. 编写测试类Maintest:
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
5. 运行。
可以看到,这一项目非常简单,只需要基本的配置即可,那么开始第二个项目:
项目二:
1. 创建pom文件,与上面的相同。
2. 创建一个class,命名为FirstScheduledJob,继承QuartzJobBean,即org.springframework.scheduling.quartz.QuartzJobBean。代码如下:
public class FirstScheduledJob extends QuartzJobBean{
private AnotherBean anotherBean;
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("I am FirstScheduledJob");
this.anotherBean.printAnotherMessage();
}
public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
}
3. 创建AnotherBean
import org.springframework.stereotype.Component;
@Component("anotherBean")
public class AnotherBean {
public void printAnotherMessage(){
System.out.println("I am AnotherBean. I am called by Quartz jobBean using CronTriggerFactoryBean");
}
}
4. 创建applicationContext.xml文件:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.test.job" />
<!-- Run the job every 5 seconds -->
<!-- For times when you need more complex processing, passing data to the scheduled job -->
<bean name="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.test.job.FirstScheduledJob" />
<property name="jobDataMap">
<map>
<entry key="anotherBean" value-ref="anotherBean" />
</map>
</property>
<property name="durability" value="true" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="firstComplexJobDetail" />
<!--<property name="cronExpression" value="0/5 * * ? * SAT-SUN" />-->
<property name="cronExpression" value="0/5 * * ? * *" />
<!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="firstComplexJobDetail" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
5. 创建maintest测试类:public class Maintest {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
上面项目二采用配置cron Trigger 的方式进行处理,如果想了解更详细的信息,可以自行搜索相关资料。
项目3:
1. 创建maven项目,pom文件内容与上面的相同。
2. 创建一个SecondScheduledJob,继承org.springframework.scheduling.quartz.QuartzJobBean。
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SecondScheduledJob extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("I am SecondScheduledJob");
}
}
3. 创建spring的配置文件applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.test.job" />
<!-- Always run at 20:00 everyday -->
<bean name="secondComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.test.job.SecondScheduledJob" />
<property name="durability" value="true" />
</bean>
<!-- Always run at 20:00 everyday -->
<bean id="secondCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="secondComplexJobDetail" />
<property name="cronExpression" value="0 0 20 ? * *" />
</bean>
<!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="secondComplexJobDetail" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="secondCronTrigger" />
</list>
</property>
</bean>
</beans>
4. 创建测试类 Maintest:
public class Maintest {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
5. 运行。
以上是3种方式处理,第一种方式,是普通方式。第二种方式使用cron ttrgger的方式处理。第三种方式是定时任务处理。
实际我们在做项目的时候,会结合两种方式处理,一种是定时处理,一种是每间隔一定的时间自动处理。
另外,任务调度可以和多线程相结合进行处理,这个我就不多说,后续如果有机会会再研究,大家也可以自行研究多线程处理任务调度。
主要参考的博客文章为:
http://blog.csdn.net/defonds/article/details/49496895
另外大家还可以参考这个url :https://www.ibm.com/developerworks/cn/java/j-quartz/index.html ,对多种方式进行了介绍。