Commit 6ac946397af27ebe879b77cb05873bbbb0c1df62
1 parent
989070a5
随应用启动定时器
Showing
2 changed files
with
63 additions
and
0 deletions
src/main/java/com/bsth/StartCommand.java
0 → 100644
| 1 | +package com.bsth; | |
| 2 | + | |
| 3 | + | |
| 4 | +import com.bsth.thread.WaybillCalcThrad; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.boot.CommandLineRunner; | |
| 7 | +import org.springframework.stereotype.Component; | |
| 8 | + | |
| 9 | +import java.util.concurrent.Executors; | |
| 10 | +import java.util.concurrent.ScheduledExecutorService; | |
| 11 | +import java.util.concurrent.TimeUnit; | |
| 12 | + | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * 随应用启动运行 | |
| 16 | + * | |
| 17 | + */ | |
| 18 | +@Component | |
| 19 | +public class StartCommand implements CommandLineRunner{ | |
| 20 | + | |
| 21 | + public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(2); | |
| 22 | + | |
| 23 | + @Autowired | |
| 24 | + WaybillCalcThrad waybillCalcThrad; | |
| 25 | + | |
| 26 | + @Override | |
| 27 | + public void run(String... arg0){ | |
| 28 | + | |
| 29 | + try { | |
| 30 | + mainServices.scheduleAtFixedRate(waybillCalcThrad, 10, 24l*60*60, TimeUnit.SECONDS); | |
| 31 | + } catch (Exception e) { | |
| 32 | + e.printStackTrace(); | |
| 33 | + } | |
| 34 | + } | |
| 35 | +} | ... | ... |
src/main/java/com/bsth/thread/WaybillCalcThrad.java
0 → 100644
| 1 | +package com.bsth.thread; | |
| 2 | + | |
| 3 | +import org.slf4j.Logger; | |
| 4 | +import org.slf4j.LoggerFactory; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.stereotype.Component; | |
| 7 | + | |
| 8 | +@Component | |
| 9 | +public class WaybillCalcThrad extends Thread { | |
| 10 | + | |
| 11 | +// @Autowired | |
| 12 | +// Class class; | |
| 13 | + | |
| 14 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 15 | + | |
| 16 | + @Override | |
| 17 | + public void run() { | |
| 18 | + logger.info("------------定时器开始工作--------------"); | |
| 19 | + | |
| 20 | + try { | |
| 21 | + | |
| 22 | + } catch (Exception e) { | |
| 23 | + logger.error("", e); | |
| 24 | + } | |
| 25 | + | |
| 26 | + logger.info("------------定时器结束工作--------------"); | |
| 27 | + } | |
| 28 | +} | ... | ... |