spring对任务调度的支持-----Quartz篇

     Quartz为任务调度提供了大量的功能,是一个强大的企业级任务调度框架。而spring对Quartz提供了一流的继承支持,让我们大大简化了任务调度的实现。

   如何简化应用Quartz,spring为我们提供了如下若干类:     

     1、QuertzJobBean 

     Quartzjob接口的简单实现(子类),提供了executeInternal() 方法定义待执行的任务;

     2、JobDetailBean 

     spring为简化JobDetail子类开发的实现,可以借助JobDetailBean中的jobClass属性设置Job对象类型;

     3、SimpleTriggerBean QuartzSimpleTrigger类的子类;

     4、CronTriggerBean 

     QuartzCronTrigger类的子类,其中可以借助cronExpression 属性能够设定任务的执行时机;

     5、MethodInvokingJobDetailFactoryBean

   不比需要QuartzJobBean 类即可完成任务调度,只需知道类的方法名即可;

     6、SchedulerFactoryBean:用于设置Quartz scheduler,供暴露给应用使用,借助于他启动定时器。

   需要的jar包

       spring框架的jar包+quartz.jar

 

通过继承QuertzJobBean,JobDetailBean发布实现

 

package com.openv.spring;

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

public class LogJobBean extends QuartzJobBean{

	//设定超时时间
	@SuppressWarnings("unused")
	private int timeout;
	
     public void setTimeout(int timeout) {
		this.timeout = timeout;
	}
	//定义待完成的任务
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		 System.out.println("任务正在执行.......");
	}

}

  spring配置文件

<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD  BEAN//EN"
                   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<!-- 定义JobDetailBean -->
	<bean id="logJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<!-- 定义任务 -->
		<property name="jobClass">
			<value>com.openv.spring.LogJobBean</value>
		</property>
		<!-- 定义属性 -->
		<property name="jobDataAsMap">
			<map>
				<entry key="timeout">
					<value>10</value>
				</entry>
			</map>
		</property>
	</bean>

	<!-- 定义触发条件 -->
	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<!-- 应用上述LogJobBean -->
		<property name="jobDetail">
			<ref bean="logJob" />
		</property>
		<!-- 第一次执行的时间,需要等待5秒钟 -->
		<property name="startDelay">
			<value>5000</value>
		</property>
		<!-- 任务执行周期为2秒 -->
		<property name="repeatInterval">
			<value>2000</value>
		</property>
		<!-- 执行次数 -->
		<property name="repeatCount">
			<value>2</value>
		</property>
	</bean>

	<!-- 把任务交给日程 -->
	<bean id="sfb"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="simpleTrigger" />
			</list>
		</property>
	</bean>
</beans>

客户端调用测试

package com.openv.spring;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//客户端测试
public class LogJobBeanTest {

	 public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("logContent.xml");
		Scheduler sfb=(Scheduler) ac.getBean("sfb");
		
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//关闭
		try {
			sfb.shutdown();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}

 

通过MethodInvokingJobDetailFactoryBean实现

任务类代码:

package com.openv.spring;


 public class LogJobMethodBean {
    
	 //具体任务
	  public void log(){
		   System.out.println("任务正在执行.......");
	  }
}

 

spring配置文件

<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD  BEAN//EN"
                   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
     <bean id="logJobMethodBean"
               class="com.openv.spring.LogJobMethodBean">
     </bean>
     
     <bean id="miJobDetail"
               class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                 <ref bean="logJobMethodBean"/>
            </property> 
            <property name="targetMethod">
                 <value>log</value>
            </property>  
      </bean>
      
      <bean id="simpleTrigger" 
                class="org.springframework.scheduling.quartz.SimpleTriggerBean">
             <property name="jobDetail">
                   <!-- 引用上述LogJobBean -->
                   <ref bean="miJobDetail"/>
             </property>
             <property name="startDelay">
                  <!-- 第一次执行任务前,需要等待5秒钟 -->
                  <value>5000</value>
             </property>
             <property name="repeatInterval">
                  <!-- 任务执行周期为2秒钟 -->
                  <value>2000</value>
             </property>
             <property name="repeatCount">
                 <!-- 执行次数 -->
                 <value>3</value>
             </property>
       </bean>  
            <bean id="sfb"
                 class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                 <property name="triggers">
                    <list>
                      <ref local="simpleTrigger"/>
                    </list>
                 </property>
       </bean>
       
</beans>


 测试客户端代码

 

package com.openv.spring;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LogJobMethodBeanTest {

	 public static void main(String[] args) {
		 ApplicationContext ac=new ClassPathXmlApplicationContext("methodContent.xml"); 
		 Scheduler sfb=(Scheduler) ac.getBean("sfb");
        
		  try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			sfb.shutdown();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}


 另外可以通过CronTriggerBean精确控制任务的具体执行时间,这在实际应用中广为使用。如下给出配置片段

<bean id="cronTrigger"
                 class="org.springframework.scheduling.quartz.CronTriggerBean">
             <property name="jobDetail">
                 <ref bean="miJobDetail"/>
             </property>
             <property name="cronExpression">
                <!-- 每隔10秒执行一次 -->
                <value>0/10 * * * * ?</value>
             </property>
        </bean>

其中,cronExpression表达式为6或7位。各个位之间通常用空格隔开。每位的具体含义如图下:


 


 


 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Quartz Starter是一个用于在Spring Boot应用程序中集成Quartz调度框架的库。Quartz是一个功能强大的开源任务调度框架,可以用于在Java应用程序中执行定时任务、计划任务等。 使用Spring Boot Quartz Starter,你可以很方便地将Quartz集成到你的Spring Boot应用程序中。它提供了一些自动配置和便利的功能,让你能够更快地开始使用Quartz。 要使用Spring Boot Quartz Starter,你需要在你的项目中添加相应的依赖。在Maven项目中,你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 添加了依赖之后,你可以使用Spring Boot提供的注解和配置来定义和管理Quartz任务。通过使用`@EnableScheduling`注解,你可以启用Spring任务调度功能。然后,你可以使用`@Scheduled`注解来定义定时任务的执行规则。 下面是一个简单的示例,演示了如何使用Spring Boot Quartz Starter创建一个定时任务: ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class MyScheduler { @Scheduled(cron = "0 0/5 * * * ?") // 每5分钟执行一次 public void myTask() { // 定时任务的业务逻辑 System.out.println("定时任务执行了!"); } } ``` 在这个示例中,我们创建了一个名为`MyScheduler`的组件,并使用`@EnableScheduling`注解启用了Spring任务调度功能。然后,我们使用`@Scheduled`注解定义了一个定时任务`myTask()`,它将每5分钟执行一次。 这只是一个简单的示例,你可以根据自己的需求来定义更复杂的定时任务。Spring Boot Quartz Starter提供了更多的功能和配置选项,可以帮助你更好地管理和调度任务。 希望能对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值