java三大框架实现任务调度——IRemindService

         java实现任务调度,主要分为几个步骤:

            1.创建一个服务
            2.创建一个任务类,将服务作为一个任务去完成(实现job接口)
            3.创建一个任务类实体
            4.创建一个触发器,指定触发规则
            5.创建一个调度,绑定任务和触发器

接下来我们先看看IRemindService接口的代码:

 /**  
* @Title: IRemindService.java
* @Package org.service
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com  
* @date 2017-5-26 下午7:54:13
* @version V1.0  
*/
package org.service;

 /**   
 *    
 * 项目名称:spring_Schop07   
 * 类名称:IRemindService   
 * 类描述:   
 * 创建人:Mu Xiongxiong  
 * 创建时间:2017-5-26 下午7:54:13   
 * 修改人:Mu Xiongxiong   
 * 修改时间:2017-5-26 下午7:54:13   
 * 修改备注:   
 * @version    
 *    
 */
public interface IRemindService {
	
	void sendMsg();
	
	void sendMsg(String userName);

}
      它的实现类也很简单,简单看看就行,重点不在这里,这里只是拟定一个任务,主要的调度在后面:

 /**  
* @Title: RemindServiceImpl.java
* @Package org.service.impl
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com  
* @date 2017-5-26 下午7:55:19
* @version V1.0  
*/
package org.service.impl;

import org.service.IRemindService;

 /**   
 *    
 * 项目名称:spring_Schop07   
 * 类名称:RemindServiceImpl   
 * 类描述:   
 * 创建人:Mu Xiongxiong  
 * 创建时间:2017-5-26 下午7:55:19   
 * 修改人:Mu Xiongxiong   
 * 修改时间:2017-5-26 下午7:55:19   
 * 修改备注:   
 * @version    
 *    
 */
public class RemindServiceImpl implements IRemindService {

	/**(非 Javadoc)
	 * <p>Title: sendMsg</p>
	 * <p>Description(描述): </p>
	 * @see org.service.IRemindService#sendMsg()
	 */

	@Override
	public void sendMsg() {
		System.out.println("像张三发送一条信息!");

	}

	@Override
	public void sendMsg(String userName) {
		System.out.println("像"+userName+"发送一条信息!");
		
	}

}

要想实现任务调度,那么久得实现Job接口,具体代码如下:

 /**  
* @Title: RemindJob.java
* @Package org.service.impl
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com  
* @date 2017-5-26 下午7:58:09
* @version V1.0  
*/
package org.service.impl;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.service.IRemindService;

 /**   
 *    
 * 项目名称:spring_Schop07   
 * 类名称:RemindJob   
 * 类描述:   
 * 创建人:Mu Xiongxiong  
 * 创建时间:2017-5-26 下午7:58:09   
 * 修改人:Mu Xiongxiong   
 * 修改时间:2017-5-26 下午7:58:09   
 * 修改备注:   
 * @version    
 *    
 */
public class RemindJob implements Job {

	/**(非 Javadoc)
	 * <p>Title: execute</p>
	 * <p>Description(描述): 任务调度</p>
	 * @param arg0
	 * @throws JobExecutionException
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
	 */

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		IRemindService remindService = new  RemindServiceImpl();
		System.out.println("任务:"+context.getJobDetail().getName());
		remindService.sendMsg(context.getJobDetail().getJobDataMap().getString("user"));
	}

}

接下来看看spring里面是如何配置的:

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<!-- 
		1.创建任务实体类 
		2.创建触发器
		3.创建调度(注入数据)
	-->
	<!-- <bean id="detailJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		 <property name="jobClass" value="org.service.impl.RemindJob"></property> 
		 <property name="jobDataMap">
		 	<map>
		 		<entry key="user" value="王五"></entry>
		 	</map>
		 </property>
	</bean> -->
	
	<bean id="remindService" class="org.service.impl.RemindServiceImpl"></bean>
	<!-- 讲普通的javaBean定义为任务,而省略了任务类 -->
	<bean id="detailJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="remindService"></property>
		<property name="targetMethod" value="sendMsg"></property>
		<property name="arguments">
			<list>
				<value>赵六</value>
			</list>
		</property>
	</bean>
	
	<!-- 简单触发器 -->
	 <bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="repeatCount" value="-1"></property>
		<property name="repeatInterval" value="3000"></property>
		<property name="startDelay" value="1000"></property>
		<property name="jobDetail" ref="detailJob"></property>
	</bean> 
	
	<!-- 复杂触发器 -->
	<!-- <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="detailJob"></property>
		<property name="cronExpression" value="0 39 22 26 * ?"></property>
	</bean> -->
	
	<bean id="sche" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="trigger"/>
			</list>
		</property>
	</bean>
</beans>

代码都编写完毕,我们来测试一下:

package org.service.impl;

import java.text.ParseException;
import java.util.Date;

import org.junit.Test;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 
*    
* 项目名称:spring_Schop07   
* 类名称:RemindServiceImplTest   
* 类描述:   测试类
* 创建人:Mu Xiongxiong  
* 创建时间:2017-5-26 下午8:04:00   
* 修改人:Mu Xiongxiong   
* 修改时间:2017-5-26 下午8:04:00   
* 修改备注:   
* @version    
*
 */
public class RemindServiceImplTest {

	@Test
	public void testMethod() throws SchedulerException, InterruptedException, ParseException {
		//创建任务实体
		JobDetail jobDetail  = new JobDetail("job1","group1",RemindJob.class);
		//给任务传递数据
		jobDetail.getJobDataMap().put("user","李四");
		
		//创建简单触发器
		SimpleTrigger trigger = new SimpleTrigger("trigger1",SimpleTrigger.REPEAT_INDEFINITELY,3000);
		
		//复杂触发器
		//每个月的26号 21:20:00
		//CronTrigger trigger = new CronTrigger("tigger3","group2","0 30 21 26 * ?");
		//开始时间(当前毫秒数+1秒,延迟一秒)
		trigger.setStartTime(new Date(System.currentTimeMillis()+1000));
		//创建调度器
		SchedulerFactory sfc = new StdSchedulerFactory();
		Scheduler sche = sfc.getScheduler();
		//注册任务和触发器
		sche.scheduleJob(jobDetail, trigger);
		//启动任务工程
		sche.start();
		//延迟10000ms
		Thread.sleep(100000);
		//关闭
		sche.shutdown();
	}
	
	
	/**
	 * @throws InterruptedException 
	 * 
	* @Title: testSpring
	* @Description: 该方法的主要作用通过spring实现quartz
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void testSpring() throws InterruptedException{
		ApplicationContext ctx = new  ClassPathXmlApplicationContext("applicationContext.xml");
		Thread.sleep(10000);
	}

}


posted @ 2017-05-31 18:56  穆雄雄  阅读(174)  评论(0编辑  收藏  举报