springboot增加定时器,直接贴代码

1.增加执行类

@Slf4j
@EnableScheduling   // 2.开启定时任务
@Component
@ImportResource(value ="classpath:/scheduling-config.xml")
public class WeixinInfoTransferJob{

    @Autowired
    FusionEAMAPIUtil frutil = new FusionEAMAPIUtil();
    
    static String INFORM_DATE = "4";//周几进行下周维护任务的通知
    
    @Autowired
    WeixinMessageService wxService;
    protected void execute()
            throws JobExecutionException {
        log.info("{}:执行定时任务-"+this.getClass().getName()+",开始...",LocalDateTime.now());
        Date nowDate = new Date();
         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        //通知明天任务
        String InfoDate = myFormatter.format(DateUtils.addDays(nowDate,0));
        try {
            List<WorkOrder> list =frutil.getPreventWkOdrs("PREVENTIVE", "ORA_RELEASED",InfoDate );
            if(CollectionUtil.isNotEmpty(list)){
                wxService.send("112520,083380","", list.toString());
            }
        } catch (Exception e) {
            log.error(this.getClass().getName()+":获取工单失败:"+e.getMessage());
        }
        log.info("{}:执行定时任务-"+this.getClass().getName()+",成功...",LocalDateTime.now());

    }
        
    }

 

2.增加sheduling-config.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="weixinInfoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetObject" ref="weixinInfoTransferJob"></property>
    <!-- 要执行的方法 -->
    <property name="targetMethod" value="execute"></property>
     </bean>
    <bean id="SchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
      <property name="jobDetail" ref="weixinInfoJob" />
      <property name="cronExpression">
       <value>*/10 * * * * ?</value>
      </property>
     </bean>
     <!-- 配置Job类 -->
         <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
          <property name="triggers">
               <list>
                <ref bean="SchedulerTaskTrigger" />
               </list>
         </property>
     </bean>
</beans>

3.增加pom.xml的maven依赖

<dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.mchange</groupId>
                    <artifactId>c3p0</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

在<properties>标签增加

    <quartz.version>2.3.0</quartz.version>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值