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

使用Spring(17)Spring中的定时调度(Scheduling)--传统方式创建Quartz任务

2016-10-28 21:34 711 查看
1.下载Quartz jar包







2.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd"> 
<!-- 传统方式创建Quartz任务Bean -->
<bean name="t_quartzjob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.yw.test17.QuartzJob" />
<property name="jobDataAsMap">
<map>
<entry key="command">
<value>更新</value>
</entry>
</map>
</property>
</bean>

<!-- 定义触发器 简单触发器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="t_quartzjob" />
<!-- 10 seconds -->
<property name="startDelay" value="10000" />
<!-- repeat every 50 seconds -->
<property name="repeatInterval" value="50000" />
</bean>

<!-- 启动任务 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>

</beans>


3.

package com.yw.test17;

import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class QuartzJob extends QuartzJobBean
{

private String command;

public String getCommand()
{
return command;
}

public void setCommand(String command)
{
this.command = command;
}

@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException
{
System.out.println(new Date()+":传统Quartz任务被调度");
for(int i=1;i<=10;i++){
System.out.println("第"+i+"次"+command+"被调用");
}
System.out.println("调用结束");

}

}


4.

package com.yw.test17;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01
{
public static void main(String[] args)
{
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml",Test01.class);
}
}


5.运行效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: