1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ciip.cloud.core.market</groupId>
<artifactId>market-serivce</artifactId>
<version>1.0</version>
<name>market-serivce</name>
<description>Demo project for Spring Boot</description>
<!-- lookup parent from repository -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<docker.harbor.ip>192.168.2.45</docker.harbor.ip>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringCloud依赖,一定要放到dependencyManagement中,起到管理版本的作用即可 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.定时任务
package com.ciip.cloud.core.market.job;
import com.ciip.cloud.core.market.service.cooperation.CooperationCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* 定时器
* 2020-04-21
* @author kindin
*/
@Service
@EnableScheduling
@Slf4j
public class MarketJob {
@Autowired
CooperationCustomerService cooperationCustomerService;
/**
* 定时任务: 报备失效
* 1. 添加合作伙伴客户,15天之后自动失效
* 2. 定时任务在凌晨十二点左右时执行此方法
* kindin 2020-04-21
*
*/
@Scheduled(cron = "59 59 23 * * ?")
public void reportInvalid() {
cooperationCustomerService.reportInvalid();
}
}
3.service层
package com.ciip.cloud.core.market.service.cooperation;
/**
* Title: CooperationCustomerService
* Description:CooperationCustomerService
*
* @author kindin
* @created 2020/3/25 15:30
*/
public interface CooperationCustomerService {
/**
* 15天后报备失效,定时任务
* kindin 2020-04-21
*/
void reportInvalid();
}
4.实现层
package com.ciip.cloud.core.market.service.impl.cooperation;
import com.ciip.cloud.core.common.constant.CommonConstant;
import com.ciip.cloud.core.common.constant.enums.market.cooperation.CooperationRelationStatus;
import com.ciip.cloud.core.market.model.cooperation.CooperationCustomer;
import com.ciip.cloud.core.market.repository.cooperation.CooperationCustomerRepository;
import com.ciip.cloud.core.market.service.cooperation.CooperationCustomerService;
import com.github.wenhao.jpa.Specifications;
import org.restlet.engine.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* Title: CooperationCustomerServiceImpl
* Description: CooperationCustomerServiceImpl
*
* @author kindin
* @created 2020/3/25 15:30
*/
@Service
public class CooperationCustomerServiceImpl implements CooperationCustomerService {
@Autowired
CooperationCustomerRepository cooperationCustomerRepository;
/**
* 15天后报备失效,定时任务
* kindin 2020-04-21
*/
@Override
public void reportInvalid() {
//获取所有已报备的客户
Specification<CooperationCustomer> specification = Specifications.<CooperationCustomer>and()
.eq("delFlag", CommonConstant.DelFlag.NO)
.eq("relationStatus", CooperationRelationStatus.REPORTED)
.build();
List<CooperationCustomer> list = cooperationCustomerRepository.findAll(specification);
System.out.println("reportInvalid定时器运行了。。。。。"+DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
list.forEach(item->{
//当前时间
Date currentDate = new Date();
String currentStr = DateUtils.format(currentDate,"yyyy-MM-dd");
//失效时间
Date invalidDate = item.getInvalidDate();
String invalidStr = DateUtils.format(invalidDate,"yyyy-MM-dd");
//如果当前时间和失效时间一致,则修改状态
if (currentStr.equals(invalidStr)){
//报备失效状态
item.setRelationStatus(CooperationRelationStatus.REPORT_FAILURE);
System.out.println("定时器,符合条件。。。查询CustomerId="+item.getId()+",失效状态:"+item.getRelationStatus());
cooperationCustomerRepository.save(item);
}
});
}
}
5.Application
package com.ciip.cloud.core.market;
import com.ciip.cloud.core.market.config.JdFinanceProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableConfigurationProperties({JdFinanceProperties.class})
@EnableDiscoveryClient
@EnableFeignClients//开启扫描Spring Cloud Feign客户端的功能
@ComponentScan(basePackages = {"com.ciip.cloud.core.*"})
@ServletComponentScan
@EnableSwagger2
@EnableSpringDataWebSupport
@EnableJpaRepositories
@SpringBootApplication
@RefreshScope
@EnableCaching // 启动缓存
public class MarketSerivceApplication {
public static void main(String[] args) {
SpringApplication.run(MarketSerivceApplication.class, args);
}
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
@Bean
public Docket swaggerMarketServerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.ciip.cloud.core.market.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder().version("1.0").title("MARKET API").description("Documentation MARKET API v1.0").build());
}
}
6.注解解释
(1) @EnableScheduling 开启对定时任务的支持
(2) @Scheduled:定时任务,可选固定时间、cron表达式等类型
其中Scheduled注解中有以下几个参数:
1.cron是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次 秒 分 时 天 月
2.zone表示执行时间的时区
3.fixedDelay 和fixedDelayString 表示一个固定延迟时间执行,上个任务完成后,延迟多长时间执行
4.fixedRate 和fixedRateString表示一个固定频率执行,上个任务开始后,多长时间后开始执行
5.initialDelay 和initialDelayString表示一个初始延迟时间,第一次被调用前延迟的时间
7.注意
(1) 定时器的任务方法不能有返回值;