程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> quartz 添加定時任務,quartz添加

quartz 添加定時任務,quartz添加

編輯:JAVA綜合教程

quartz 添加定時任務,quartz添加


最近都在做API接口,發現有些接口沒辦法長期跟蹤,但是有經常出錯,所以需要做監控,本來這個網絡運維也可以做的,但是發現java裡面也有很好的方法,最終決定還是在java裡面實現。不多說,直接上代碼。

 

a.首先就是創建一個quartz-config.xml,裡面配置需要啟動的類以及方法,定時的時間。

 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


<!-- 要調用驗卡的工作類 -->
<bean id="sendChannel" class="main.java.ssm.sendchannel.controller.SendChannelController"></bean>
<!-- 定義調用對象和調用對象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 調用的類 -->
<property name="targetObject">
<ref bean="sendChannel"/>
</property>
<!-- 調用類中的方法 -->
<!-- <property name="targetMethod">
<value>work</value>
</property> -->
<property name="targetMethod">
<value>verifyInfo</value>
</property>
</bean>
<!-- 定義觸發時間 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表達式 -->
<property name="cronExpression">
<value>0 0/30 * * * ?</value>
</property>
</bean>

<!-- 總管理類 如果將lazy-init='false'那麼容器啟動就會執行調度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>

 

b.接著就在web.xml配置quartz-config.xml的調用

<!-- 配置上游接口監控 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/quartz-config.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

c.重要的就是方法的格式,lz發現方法有返回值的話是沒有辦法調用成功的,希望有大牛能解決。……——……

public class SendChannelController
{

public void verifyInfo()
{
log.info("Quartz的任務調度!!!");
}

}

d.到這裡所有的代碼都寫好了,當然需要測試!!!

public static void main(String[] args)
{
System.out.print("Test start.");
ApplicationContext context = new ClassPathXmlApplicationContext("spring/quartz-config.xml");
//如果配置文件中將startQuertz bean的lazy-init設置為false 則不用實例化
//context.getBean("startQuertz");
System.out.print("Test end..\n");
}

e.測試結果一切正常,可以啟動服務了。這時定時器就可以隨服務一起啟動了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved