SSM(SpringBoot)下实现定时任务

配置类

package com.yq.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;

/**
 * 多线程执行定时任务配置类
 */
@EnableScheduling(SpringBoot项目该注解可放到Application启动类)
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        //设定一个长度10的定时任务线程池
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}

执行任务内容类

package com.yq.util;

import org.change.util.PageData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 下架活动过期的商品的定时任务工作类
 */
@Component
public class OutGoodsTiming {

    @Value("${outGoodsHttp}")
    private String url;

    /**
     * 定时查询有效限时活动,超时下架对应商品
     */
    @Scheduled(cron = "0 0 * * * *")//每小时执行一次(这里是发送请求),不同时间可百度cron(测试可用"*/5 * * * * ?",5秒执行一次)
    public void orderTimeOut() {
        System.out.println("定时下架商品任务开始执行------------------------------------------------------------------------------------------");
        PageData pd = new PageData();
        pd.put("activity_type", 2);
        String result = ProveBusiness.sendGet(url + "/activity/outGoods", "activity_type=2");
        System.out.println(result);
        System.out.println("定时下架商品任务执行结束------------------------------------------------------------------------------------------");
    }

}

PS:java读取配置文件的数值

java:
@Value("${outGoodsHttp}")
    private String url;

ssm:
spring配置文件:
  
	
 
jdbc.properties配置文件:
outGoodsHttp:http://localhost:8080/mallweb_war_exploded

springBoot:
application.properties配置文件:
outGoodsHttp:http://localhost:8080/mallweb_war_exploded

 

你可能感兴趣的:(Java)