定时器

微信公众号开发中,用到quartz定时器定时保存access_token,这里记录一下过程:

     1、在pom.xml中引入依赖:

<!-- quartz -->
<dependency>
   <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz</artifactId>
   <version>2.2.1</version>
</dependency>
<dependency>
   <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz-jobs</artifactId>
   <version>2.2.1</version>
</dependency>

package com.xhwz.wxsite.modules.frontweb.weixin.accesstoken;

import com.xhwz.wxsite.common.web.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 定时任务:每两小时刷新一次微信access_token
 * @author 
 * @version 2018-08-08
 */
public class QuartzJob extends BaseController {
   private static Logger logger = Logger.getLogger(QuartzJob.class);
   @Autowired
   private AccessTokenService accessTokenService;
   /**
    * 任务执行:每两小时刷新一次微信access_token,并且更新数据库
    */
   public void workForToken() {
      try {
            WXTokenUtil wxTokenUtil = new WXTokenUtil();
         AccessToken accessToken = wxTokenUtil.getAccessToken();
         //保存到数据库中
         accessTokenService.saveAccessToken(accessToken);
      } catch (Exception e) {
         logger.error(e, e);
      }
   }

}


3、编写spring-quartz.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
      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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">
   <!-- 要调用的工作类 -->
   <bean id="quartzJob" class="com.xhwz.wxsite.modules.frontweb.weixin.accesstoken.QuartzJob"></bean>

   <!-- 定义调用对象和调用对象的方法 -->
   <bean id="jobtaskForToken" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <!-- 调用的类 -->
      <property name="targetObject">
         <ref bean="quartzJob" />
      </property>
      <!-- 调用类中的方法 -->
      <property name="targetMethod">
         <value>workForToken</value>
      </property>

   </bean>
   <!-- 定义触发时间 -->
   <bean id="doTimeForToken" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
      <property name="jobDetail">
         <ref bean="jobtaskForToken" />
      </property>
      <!-- cron 表达式  1分钟 0 0/1 * * * ? *  两小时:0 0 0/2 * * ? *   -->
      <property name="cronExpression">
         <value>0 0/3 * * * ? *</value>
      </property>
   </bean>

   <!-- 配置项目启动后任务就执行一次 -->
   <bean id="rsh_simpleTrigger1" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
      <property name="jobDetail" ref="jobtaskForToken" />
      <property name="startDelay" value="500" />
      <property name="repeatInterval" value="0" />
      <property name="repeatCount" value="0" />
   </bean>


   <!-- 总管理类 如果将 lazy-init='false'那么容器启动就会执行调度程序 -->
   <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
         <list>
            <ref bean="rsh_simpleTrigger1" />
            <ref bean="doTimeForToken" />
         </list>
      </property>
   </bean>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值