成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

SpringCloud(第 047 篇)注解式Async配置異步任務(wù)

StonePanda / 3416人閱讀

摘要:耗時(shí)毫秒耗時(shí)毫秒耗時(shí)毫秒添加異步任務(wù)控制器測(cè)試異步任務(wù)控制器。

SpringCloud(第 047 篇)注解式Async配置異步任務(wù)

-

一、大致介紹
1、有時(shí)候我們?cè)谔幚硪恍┤蝿?wù)的時(shí)候,需要開啟線程去異步去處理,原有邏輯繼續(xù)往下執(zhí)行;
2、當(dāng)遇到這種場(chǎng)景的時(shí)候,線程是可以將我們完成,然后在SpringCloud中也有這樣的注解來支撐異步任務(wù)處理;
二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包


    4.0.0

    springms-async
    1.0-SNAPSHOT
    jar
    
    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    


2.2 添加應(yīng)用配置文件(springms-asyncsrcmainresourcesapplication.yml)
server:
  port: 8345
spring:
  application:
    name: springms-async  #全部小寫

2.3 添加異步任務(wù)類(springms-asyncsrcmainjavacomspringmscloudtaskAsyncTasks.java)
package com.springms.cloud.task;

import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.concurrent.Future;

/**
 * Async實(shí)現(xiàn)異步調(diào)用。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/10/19
 *
 */
@Component
public class AsyncTasks {

    public static Random random = new Random();

    @Async
    public Future doTaskOne() throws Exception {
        System.out.println("Async, taskOne, Start...");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("Async, taskOne, End, 耗時(shí): " + (end - start) + "毫秒");
        return new AsyncResult<>("AsyncTaskOne Finished");
    }

    @Async
    public Future doTaskTwo() throws Exception {
        System.out.println("Async, taskTwo, Start");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("Async, taskTwo, End, 耗時(shí): " + (end - start) + "毫秒");
        return new AsyncResult<>("AsyncTaskTwo Finished");
    }

    @Async
    public Future doTaskThree() throws Exception {
        System.out.println("Async, taskThree, Start");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(5000));
        long end = System.currentTimeMillis();
        System.out.println("Async, taskThree, End, 耗時(shí): " + (end - start) + "毫秒");
        return new AsyncResult<>("AsyncTaskThree Finished");
    }
}
2.4 添加異步任務(wù)Web控制器(springms-asyncsrcmainjavacomspringmscloudcontrollerAsyncTaskController.java)
package com.springms.cloud.controller;

import com.springms.cloud.task.AsyncTasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * 測(cè)試異步任務(wù)Web控制器。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/10/19
 *
 */
@RestController
public class AsyncTaskController {

    @Autowired
    AsyncTasks asyncTasks;

    /**
     * 測(cè)試異步任務(wù)。
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/task")
    public String task() throws Exception {
        long start = System.currentTimeMillis();

        Future task1 = asyncTasks.doTaskOne();
        Future task2 = asyncTasks.doTaskTwo();
        Future task3 = asyncTasks.doTaskThree();

        while(true) {
            if(task1.isDone() && task2.isDone() && task3.isDone()) {
                // 三個(gè)任務(wù)都調(diào)用完成,退出循環(huán)等待
                break;
            }
            Thread.sleep(1000);
        }

        long end = System.currentTimeMillis();

        String result = "任務(wù)全部完成,總耗時(shí):" + (end - start) + "毫秒";
        return result;
    }
}
2.5 添加微服務(wù)啟動(dòng)類(springms-schedulesrcmainjavacomspringmscloudMsScheduleApplication.java)
package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 注解式Async配置異步任務(wù);
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/10/19
 *
 */
@SpringBootApplication
@EnableAsync
public class MsAsyncApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsAsyncApplication.class, args);
        System.out.println("【【【【【【 Async異步任務(wù)微服務(wù) 】】】】】】已啟動(dòng).");
    }
}
三、測(cè)試
/****************************************************************************************
 一、簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(Async實(shí)現(xiàn)異步調(diào)用):

 1、添加注解 EnableAsync、Async 以及任務(wù)類上注解 Component ;
 2、啟動(dòng) springms-async 模塊服務(wù),啟動(dòng)1個(gè)端口;
 3、然后在瀏覽器輸入地址 http://localhost:8345/task 然后等待大約10多秒后,成功打印所有信息,一切正常;

 總結(jié):說明 Async 異步任務(wù)配置生效了;
 ****************************************************************************************/
四、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接

歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/67835.html

相關(guān)文章

  • SpringCloud 046 注解Schedule配置定時(shí)任務(wù),不支持任務(wù)調(diào)度

    摘要:當(dāng)前時(shí)間打印當(dāng)前時(shí)間定時(shí)任務(wù)觸發(fā),操作多個(gè)添加數(shù)據(jù),事務(wù)中任一異常,都可以正常導(dǎo)致數(shù)據(jù)回滾。當(dāng)前時(shí)間當(dāng)前時(shí)間添加微服務(wù)啟動(dòng)類注解式配置定時(shí)任務(wù),不支持任務(wù)調(diào)度。 SpringCloud(第 046 篇)注解式Schedule配置定時(shí)任務(wù),不支持任務(wù)調(diào)度 - 一、大致介紹 1、很多時(shí)候我們需要隔一定的時(shí)間去執(zhí)行某個(gè)任務(wù),為了實(shí)現(xiàn)這樣的需求通常最普通的方式就是利用多線程來實(shí)現(xiàn); 2、但是有...

    masturbator 評(píng)論0 收藏0
  • SpringCloud 027 )集成異構(gòu)微服務(wù)系統(tǒng)到 SpringCloud 生態(tài)圈中(比如

    摘要:注意注解能注冊(cè)到服務(wù)上,是因?yàn)樵撟⒔獍丝蛻舳说淖⒔?,該是一個(gè)復(fù)合注解。包含了客戶端注解,同時(shí)也包含了斷路器模塊注解,還包含了網(wǎng)關(guān)模塊。 SpringCloud(第 027 篇)集成異構(gòu)微服務(wù)系統(tǒng)到 SpringCloud 生態(tài)圈中(比如集成 nodejs 微服務(wù)) - 一、大致介紹 1、在一些稍微復(fù)雜點(diǎn)系統(tǒng)中,往往都不是單一代碼寫的服務(wù),而恰恰相反集成了各種語(yǔ)言寫的系統(tǒng),并且我們還...

    caozhijian 評(píng)論0 收藏0
  • SpringCloud 009 )簡(jiǎn)單 Quartz 微服務(wù),不支持分布

    摘要:添加任務(wù)成功運(yùn)行任務(wù)名稱添加定時(shí)任務(wù)服務(wù)定時(shí)任務(wù)服務(wù)。觸發(fā)器計(jì)劃列表添加測(cè)試任務(wù)類測(cè)試任務(wù)類被任務(wù)調(diào)度后執(zhí)行該任務(wù)類。聲明一個(gè)靜態(tài)變量保存添加啟動(dòng)類簡(jiǎn)單微服務(wù),不支持分布式。 SpringCloud(第 009 篇)簡(jiǎn)單 Quartz 微服務(wù),不支持分布式 - 一、大致介紹 1、本章節(jié)僅僅只是為了測(cè)試 Quartz 在微服務(wù)中的使用情況; 2、其實(shí)若只是簡(jiǎn)單的實(shí)現(xiàn)任務(wù)調(diào)用而言的話,Sp...

    awkj 評(píng)論0 收藏0
  • SpringCloud 054 )簡(jiǎn)單 Quartz-Cluster 微服務(wù),采用注解配置 Q

    摘要:加載配置文件失敗加載配置文件失敗添加定時(shí)調(diào)度任務(wù)定時(shí)調(diào)度任務(wù)添加定時(shí)調(diào)度任務(wù)定時(shí)調(diào)度任務(wù)執(zhí)行的張表入數(shù)據(jù)庫(kù)添加啟動(dòng)類簡(jiǎn)單微服務(wù),采用注解配置分布式集群。 SpringCloud(第 054 篇)簡(jiǎn)單 Quartz-Cluster 微服務(wù),采用注解配置 Quartz 分布式集群 - 一、大致介紹 1、因網(wǎng)友提到有沒有采用注解式配置的Quartz例子,因此本人就貼上了這樣一個(gè)樣例; 2、至...

    isLishude 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

StonePanda

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<