spring-boot 如何實現(xiàn)單次執(zhí)行程序
spring-boot 單次執(zhí)行程序
spring-boot做為spring的集大成框架,大部分時候作為WEB服務被集成使用,但某些情況下,需要手動執(zhí)行一些邏輯的情況下,單次運行的類似腳本的程序也是很有用的。
本文記錄一下使用spring-boot作為單次可執(zhí)行程序配置方式。
pom.xml
注意:pom.xml部分只需引入spring-boot-starter模塊,尤其不要引入web模塊,其他非spring本身模塊可以隨意引入
<?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>
<parent>
<!-- 按工程習慣處理parent部分 -->
</parent>
<groupId>com.leon</groupId>
<artifactId>sprint-boot-task</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主要代碼結構
Service類
@Service
public class StatService {
public void doSomething() {
System.out.println("===================: this is a test service but nothing");
}
}
執(zhí)行邏輯入口類
@Component
public class StatTask {
private StatService statService;
@Autowired
public StatTask(StatService statService) {
this.statService = statService;
}
public void doSomething() {
statService.doSomething();
}
}
Spring-boot 啟動類
@SpringBootApplication
public class TaskApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TaskApplication.class, args);
StatTask statTask = context.getBean(StatTask.class); // 獲取邏輯入口類的實例
statTask.doSomething();
}
}
如此這般后,啟動這個springboot工程,執(zhí)行完啟動類中的調用過程后,程序就會自動退出。
基本上,不配置啟用spring mvc和定時Job,這種配置下的springboot就是一個“腳本”程序。
這里舉個?,上面的代碼加上兩個注解,就會變成常駐進程程序:
執(zhí)行邏輯入口類
@Component
public class StatTask {
private StatService statService;
@Autowired
public StatTask(StatService statService) {
this.statService = statService;
}
@Scheduled(fixedRate = 5000L) // --------------這里-----------------
public void doSomething() {
statService.doSomething();
}
}
Spring-boot 啟動類
@SpringBootApplication
@EnableScheduling // --------------這里---------------
public class TaskApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TaskApplication.class, args);
StatTask statTask = context.getBean(StatTask.class);
statTask.doSomething();
}
}
與最上面區(qū)別的是,上面只執(zhí)行一次,輸出 “this is a test service but nothing” 就完事了,進程自動退出,
加上兩個注解后就會每5秒輸出一次 “this is a test service but nothing”,且進程永駐。
當然這種情況下使用腳本語言如python、nodeJs等可能更好一些,但在其他語言不熟的情況下,使用spring-boot來應急也是極好的。
啟動時執(zhí)行單次任務
最近做任務遇到一個問題,需要在項目啟動時候執(zhí)行掃描數(shù)據(jù)庫表的任務,用于異?;謴腿轂模婚_始想的是可不可以使用定時任務
代碼如下 并且在啟動類加上
@EnableScheduling注解就可以實現(xiàn)定時去執(zhí)行任務了
package com.beihui.service.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class XXXTask {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Scheduled(cron = "0 0 0 * * ?")
public void bTask() {
long startCurrentTime = System.currentTimeMillis();
logger.info("開始執(zhí)行定時任務:" + startCurrentTime);
//業(yè)務處理
long endTime = System.currentTimeMillis();
logger.info("定時任務:執(zhí)行結束,花費時間" + (endTime - startCurrentTime));
}
@Scheduled(cron = "0 */1 * * * ?")
public void runUpdateDbTask() {
long startCurrentTime = System.currentTimeMillis();
logger.info("開始執(zhí)行更新數(shù)據(jù)庫剩余次數(shù)定時任務:" + startCurrentTime);
//業(yè)務處理
long endTime = System.currentTimeMillis();
logger.info("定時任務:執(zhí)行結束,花費時間" + (endTime - startCurrentTime));
}
@Scheduled(fixedDelay = 60 * 1000 * 10)
public void cTask() {
long startCurrentTime = System.currentTimeMillis();
//業(yè)務處理
long endTime = System.currentTimeMillis();
logger.info("定時任務:執(zhí)行結束,花費時間" + (endTime - startCurrentTime));
}
}
但是這個并不能單次執(zhí)行任務,所以后來 使用listener
代碼如下,并在啟動類加上
@ServletComponentScan注解
package xx.xx.xx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class XXXListener implements ServletContextListener {
private Logger logger = LoggerFactory.getLogger(this.getClass());
//項目啟動執(zhí)行
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
long startTime = System.currentTimeMillis();
logger.info("開始執(zhí)行啟動任務,{}"+startTime);
//業(yè)務處理
long endTime = System.currentTimeMillis();
logger.info("執(zhí)行啟動任務結束,共花費時間{}"+(startTime-endTime));
}
//項目終止時執(zhí)行
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java語言實現(xiàn)簡單FTP軟件 FTP上傳下載隊列窗口實現(xiàn)(7)
這篇文章主要為大家詳細介紹了Java語言實現(xiàn)簡單FTP軟件,F(xiàn)TP上傳下載隊列窗口的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
IDEA報錯Error?running‘Application‘:Command?line?is?too?lo
這篇文章主要介紹了IDEA報錯Error?running?‘Application‘:Command?line?is?too?long的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
JavaSE實現(xiàn)圖書管理系統(tǒng)的示例代碼
這篇博客是在學習了一部分Java基礎語法之后的練習項目,通過這個小項目的練習,對Java中的類和對象,抽象類和接口等進行熟悉理解。快跟隨小編一起學習學習吧2022-08-08
springboot中@ConfigurationProperties無效果的解決方法
本文主要介紹了springboot中@ConfigurationProperties無效果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06

