java spring 集成定时器
2019獨角獸企業重金招聘Python工程師標準>>>
接口
public interface Time {
?? ?public void execute() ;
}
實現類
@Component(value = "logTask")//xml中裝配
public class TimeUtil implements Time{
?? ?@Override
?? ?public void execute() {
?? ??? ?// TODO Auto-generated method stub
? ? ? ?要實現的方法
?? ??? ?System.out.println("啟動了");
?? ?}
}
spring-job.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">
?? ?<bean id="jobDetail"
?? ??? ?class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
?? ??? ?<property name="targetObject">
?? ??? ??? ?<ref bean="logTask" />//裝備的類
?? ??? ?</property>
?? ??? ?<property name="targetMethod">
?? ??? ??? ?<value>execute</value>//執行的方法
?? ??? ?</property>
?? ?</bean>
?? ?<bean id="jobTrigger"
?? ??? ?class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
?? ??? ?<property name="jobDetail">
?? ??? ??? ?<ref bean="jobDetail" />
?? ??? ?</property>
?? ??? ?<property name="cronExpression">
?? ??? ??? ?<value>0 21 19 * * ?</value>//時間
?? ??? ?</property>
<!-- ?? ?
?? ??? ?字段?? ? ?? ?允許值?? ? ?? ?允許的特殊字符
秒?? ? ?? ?0-59?? ? ?? ?, - * /
分?? ? ?? ?0-59?? ? ?? ?, - * /
小時?? ? ?? ?0-23?? ? ?? ?, - * /
日期?? ? ?? ?1-31?? ? ?? ?, - * ? / L W C
月份?? ? ?? ?1-12 或者 JAN-DEC?? ? ?? ?, - * /
星期?? ? ?? ?1-7 或者 SUN-SAT?? ? ?? ?, - * ? / L C #
年(可選)?? ? ?? ?留空, 1970-2099?? ? ?? ?, - * /
?-->?? ?
?? ?</bean>
?? ?<bean id="jobScheduler"
?? ??? ?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
?? ??? ?<property name="triggers">
?? ??? ??? ?<list>
?? ??? ??? ??? ?<ref bean="jobTrigger" />
?? ??? ??? ?</list>
?? ??? ?</property>
?? ?</bean>
</beans>
?
最后在spring-context.xml中進行注冊:
<import resource="classpath:spring/spring-job.xml" />?
轉載于:https://my.oschina.net/xiaoshoubingliang/blog/729716
總結
以上是生活随笔為你收集整理的java spring 集成定时器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux phpstorm安装和激活
- 下一篇: 收藏转一个东东