SpringCloud2.x Task

Task

簡介

為了短期或者一次性執(zhí)行某種任務(wù),比如統(tǒng)計(jì),更新數(shù)據(jù)等所使用的短期框架,也可以與flow data 大數(shù)據(jù)分析進(jìn)行結(jié)合,但本文章只說明Task單獨(dú)使用

搭建

1. 使用spring cloud的框架

<?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.demo</groupId>
    <artifactId>task</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
        <!-- lookup parent thirdpart repository -->
    </parent>

   <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

      <!-- 短期任務(wù) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-task</artifactId>
        </dependency>
       <!-- task依賴,不要去除 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- jpa jar -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

添加注解

在Application中添加注解

@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigurationProperties
@EnableTask
public class Application{
   public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

添加任務(wù)執(zhí)行

使用注解方式,添加任務(wù),任務(wù)的執(zhí)行會分為三部分

  • @BeforeTask 任務(wù)開始
  • @AfterTask 任務(wù)結(jié)束
  • @FailedTask 任務(wù)異常,如果發(fā)生會優(yōu)先結(jié)束執(zhí)行
@Component
public class taskRunner{
    @BeforeTask
    public void onTaskStartup(TaskExecution taskExecution) {
        logger.info("任務(wù)執(zhí)行 start");
    }
    @AfterTask
    public void onTaskEnd(TaskExecution taskExecution) {
        logger.info("任務(wù)執(zhí)行 end");
    }

    @FailedTask
    public void onTaskEndFailed(TaskExecution taskExecution, Throwable throwable) {
        logger.info("任務(wù)執(zhí)行 failed"");
    }
}

這樣就可以正常運(yùn)行一個(gè)簡單的Task了。

配置

一、配置項(xiàng)說明 TaskConfigurer

TaskConfigurer是一個(gè)允許用戶自定義Spring Cloud Task組件配置的策略接口,其默認(rèn)實(shí)現(xiàn)DefaultTaskConfigurer提供了應(yīng)用的默認(rèn)邏輯(當(dāng)應(yīng)用上下文中沒有DataSource時(shí)使用內(nèi)存Map作為任務(wù)倉庫存儲。當(dāng)上下文中有DataSource時(shí),使用依賴于 JDBC 的組件實(shí)現(xiàn)任務(wù)倉庫。)。

這里有個(gè)疑問:有數(shù)據(jù)庫的情況下,必須創(chuàng)建和存儲任務(wù)數(shù)據(jù),沒辦法不保存數(shù)據(jù)。如下場景,更新業(yè)務(wù)數(shù)據(jù)庫中數(shù)據(jù),但是不想在數(shù)據(jù)庫中添加任務(wù)的相關(guān)結(jié)構(gòu);或者單獨(dú)存儲任務(wù)的數(shù)據(jù),在一個(gè)任務(wù)中執(zhí)行了不同的業(yè)務(wù)數(shù)據(jù)的操作;

TaskConfigurer主要包含三種組件,如表1所示:

組件名 描述 默認(rèn)值(由 TaskConfigurer 提供)
TaskRepository TaskRepository接口的實(shí)現(xiàn)類,作為任務(wù)倉庫使用 SimpleTaskRepository
TaskExplorer TaskExplorer接口的實(shí)現(xiàn)類(任務(wù)探測器是只讀訪問任務(wù)倉庫的組件) SimpleTaskExplorer
PlatformTransactionManager 對任務(wù)倉庫執(zhí)行更新操作時(shí)所使用的事務(wù)管理器 如果應(yīng)用上下文中配置有DataSource,默認(rèn)的事務(wù)管理器為DataSourceTransactionManager。如果應(yīng)用上下文中沒有數(shù)據(jù)源,則默認(rèn)的事務(wù)管理器是ResourcelessTransactionManager。

表1 TaskConfigurer的三種主要組件
在定制 TaskConfigurer 時(shí)推薦直接繼承已有的成熟TaskConfigurer接口,例如繼承 DefaultTaskConfigurer,覆蓋所需要的getter就足夠了。否則的話,你需要重寫整個(gè) TaskConfigurer 的邏輯。

二、執(zhí)行方式

spring:
  cloud:
    task:
      closecontext-enabled: true # 執(zhí)行完成后,關(guān)閉服務(wù)

三、數(shù)據(jù)庫配置

默認(rèn)情況下,會使用H2 嵌入式內(nèi)存數(shù)據(jù)庫作為數(shù)據(jù)源來啟動(dòng)發(fā)布task,如果需要進(jìn)行存儲任務(wù)執(zhí)行的情況,可以對在配置文件中添加相關(guān)的配置,這里我介紹下使用JPA的使用方式:
spring.cloud.task配置開頭,例如:spring.cloud.task.tablePrefix

下面對配置項(xiàng)進(jìn)行說明:

配置項(xiàng) 說明
tablePrefix 數(shù)據(jù)庫表,命名前綴:默認(rèn)為TASK_
executionid 任務(wù)實(shí)例ID,存儲到數(shù)據(jù)庫
external-execution-id 外部任務(wù)ID
parent-execution-id 父類任務(wù)ID,可以在一個(gè)任務(wù)中調(diào)用另一個(gè)任務(wù)
single-instance-enabled 啟用單個(gè)實(shí)例模式
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容