java中定时器的写法

一、在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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
           http://cxf.apache.org/jaxws  http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration 
           http://cxf.apache.org/schemas/configuration/http-conf.xsd">
           
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


<import resource="classpath*:cxf-jaxws-server.xml" />


<context:component-scan base-package="*" />
<aop:aspectj-autoproxy />
<tx:annotation-driven transaction-manager="transactionManager" />


<!-- 数据源配置,在测试环境使用 -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" /> 
<property name="url" value="jdbc:db2://10.194.4.10:50000/YT" />
<property name="username" value="administrator" /> 
<property name="password" value="admin@123" /> 
</bean>

<!-- 数据源配置,在生产环境使用应用服务器的数据库连接池 -->
<!-- -->
<!-- <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/JDBC/GZJDC" />-->


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.DB2Dialect
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
</props>
</property>
<property name="packagesToScan" value="com.zkyt.entity" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="jsonConverter" class="com.zkyt.modules.json.GsonConverter" />
<bean id="beanContext" class="com.zkyt.modules.utils.SpringBeanContext" />
<bean id="handlerExceptionResolver" class="com.zkyt.modules.web.SimplifyExceptionHandler">
<!--property name="statusCode" value="311"/-->
</bean>

<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />


<!--定时器配置,需要导入的文件-->
  <bean id="starQuartzs" lazy-init="false" autowire="default" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
  <list>
  <ref bean="doDaily"/>
  </list>
  </property>
  </bean>
  
  <!--定制每天要执行的计划-->
  <bean id="doDaily" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
  <ref bean="jobDailyTask"/>
  </property>
 
  <!--定制执行计划的时间-->
  <property name="cronExpression">
  <value>0 * 17 * * ?</value><!--第月一日凌晨1:00执行-->
  </property>
  </bean>
  
  <!--定制任务执行的类和方法-->
  <bean id="jobDailyTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!--类-->
  <property name="targetObject" >
  <ref bean="dailyService"/>  
  </property>
  <!--方法-->
  <property name="targetMethod">
  <value>doDailyThing</value>
  </property>
  </bean>
  
  <!--要执行的类-->
  <bean id="dailyService" class="com.zkyt.service.quartz.DailyService" />
</beans>


二、编写执行方法

@Service
public class DailyService {
public void doDailyThing()
{
System.out.println("============TestOk===========");
}
}

 

常用的时间表示方法

"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

表示式

说明

"0 0 12 * * ? "

每天12点运行

"0 15 10 ? * *"

每天10:15运行

"0 15 10 * * ?"

每天10:15运行

"0 15 10 * * ? *"

每天10:15运行

"0 15 10 * * ? 2008"

在2008年的每天10:15运行

"0 * 14 * * ?"

每天14点到15点之间每分钟运行一次,开始于14:00,结束于14:59。

"0 0/5 14 * * ?"

每天14点到15点每5分钟运行一次,开始于14:00,结束于14:55。

"0 0/5 14,18 * * ?"

每天14点到15点每5分钟运行一次,此外每天18点到19点每5钟也运行一次。

"0 0-5 14 * * ?"

每天14:00点到14:05,每分钟运行一次。

"0 10,44 14 ? 3 WED"

3月每周三的14:10分到14:44,每分钟运行一次。?

"0 15 10 ? * MON-FRI"

每周一,二,三,四,五的10:15分运行。

"0 15 10 15 * ?"

每月15日10:15分运行。

"0 15 10 L * ?"

每月最后一天10:15分运行。

"0 15 10 ? * 6L"

每月最后一个星期五10:15分运行。

"0 15 10 ? * 6L 2007-2009"

在2007,2008,2009年每个月的最后一个星期五的10:15分运行。

"0 15 10 ? * 6#3"

每月第三个星期五的10:15分运行。


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值