日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
聊聊分布式服務(wù)下的八種異步實(shí)現(xiàn)方式

一、異步的八種實(shí)現(xiàn)方式

1、線程Thread

為方城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及方城網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、方城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

2、Future

3、異步框架CompletableFuture

4、Spring注解@Async

5、Spring ApplicationEvent事件

6、消息隊(duì)列

7、第三方異步框架,比如Hutool的ThreadUtil

8、Guava異步

二、什么是異步?

首先我們先看一個(gè)常見的用戶下單的場(chǎng)景:

什么是異步?

在同步操作中,我們執(zhí)行到 發(fā)送短信 的時(shí)候,我們必須等待這個(gè)方法徹底執(zhí)行完才能執(zhí)行 贈(zèng)送積分 這個(gè)操作,如果 贈(zèng)送積分 這個(gè)動(dòng)作執(zhí)行時(shí)間較長(zhǎng),發(fā)送短信需要等待,這就是典型的同步場(chǎng)景。

實(shí)際上,發(fā)送短信和贈(zèng)送積分沒有任何的依賴關(guān)系,通過異步,我們可以實(shí)現(xiàn)贈(zèng)送積分和發(fā)送短信這兩個(gè)操作能夠同時(shí)進(jìn)行,比如:

異步

這就是所謂的異步,是不是非常簡(jiǎn)單,下面就說說異步的幾種實(shí)現(xiàn)方式吧。

三、異步編程

1、線程異步

public class AsyncThread extends Thread {


    @Override
    public void run() {
        System.out.println("Current thread name:" + Thread.currentThread().getName() + " Send email success!");
    }


    public static void main(String[] args) {
        AsyncThread asyncThread = new AsyncThread();
        asyncThread.run();
    }
}

當(dāng)然如果每次都創(chuàng)建一個(gè)Thread線程,頻繁的創(chuàng)建、銷毀,浪費(fèi)系統(tǒng)資源,我們可以采用線程池:

private ExecutorService executorService = Executors.newCachedThreadPool();


public void fun() {
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            log.info("執(zhí)行業(yè)務(wù)邏輯...");
        }
    });
}

可以將業(yè)務(wù)邏輯封裝到Runnable或Callable中,交由線程池來執(zhí)行。

2、 Future異步

@Slf4j
public class FutureManager {


    public String execute() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future future = executor.submit(new Callable() {
            @Override
            public String call() throws Exception {
                System.out.println(" --- task start --- ");
                Thread.sleep(3000);
                System.out.println(" --- task finish ---");
                return "this is future execute final result!!!";
            }
        });
        //這里需要返回值時(shí)會(huì)阻塞主線程
        String result = future.get();
        log.info("Future get result: {}", result);
        return result;
    }


    @SneakyThrows
    public static void main(String[] args) {
        FutureManager manager = new FutureManager();
        manager.execute();
    }
}

輸出結(jié)果:

--- task start --- 
 --- task finish ---
 Future get result: this is future execute final result!!!

(1) Future的不足之處

Future的不足之處的包括以下幾點(diǎn):

  • 無法被動(dòng)接收異步任務(wù)的計(jì)算結(jié)果:雖然我們可以主動(dòng)將異步任務(wù)提交給線程池中的線程來執(zhí)行,但是待異步任務(wù)執(zhí)行結(jié)束之后,主線程無法得到任務(wù)完成與否的通知,它需要通過get方法主動(dòng)獲取任務(wù)執(zhí)行的結(jié)果。
  • Future件彼此孤立:有時(shí)某一個(gè)耗時(shí)很長(zhǎng)的異步任務(wù)執(zhí)行結(jié)束之后,你想利用它返回的結(jié)果再做進(jìn)一步的運(yùn)算,該運(yùn)算也會(huì)是一個(gè)異步任務(wù),兩者之間的關(guān)系需要程序開發(fā)人員手動(dòng)進(jìn)行綁定賦予,F(xiàn)uture并不能將其形成一個(gè)任務(wù)流(pipeline),每一個(gè)Future都是彼此之間都是孤立的,所以才有了后面的CompletableFuture,CompletableFuture就可以將多個(gè)Future串聯(lián)起來形成任務(wù)流。
  • Futrue沒有很好的錯(cuò)誤處理機(jī)制:截止目前,如果某個(gè)異步任務(wù)在執(zhí)行發(fā)的過程中發(fā)生了異常,調(diào)用者無法被動(dòng)感知,必須通過捕獲get方法的異常才知曉異步任務(wù)執(zhí)行是否出現(xiàn)了錯(cuò)誤,從而在做進(jìn)一步的判斷處理。

3、CompletableFuture實(shí)現(xiàn)異步

public class CompletableFutureCompose {
    /**
     * thenAccept子任務(wù)和父任務(wù)公用同一個(gè)線程
     */
    @SneakyThrows
    public static void thenRunAsync() {
        CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread() + " cf1 do something....");
            return 1;
        });
        CompletableFuture cf2 = cf1.thenRunAsync(() -> {
            System.out.println(Thread.currentThread() + " cf2 do something...");
        });
        //等待任務(wù)1執(zhí)行完成
        System.out.println("cf1結(jié)果->" + cf1.get());
        //等待任務(wù)2執(zhí)行完成
        System.out.println("cf2結(jié)果->" + cf2.get());
    }


    public static void main(String[] args) {
        thenRunAsync();
    }
}

我們不需要顯式使用ExecutorService,CompletableFuture 內(nèi)部使用了ForkJoinPool來處理異步任務(wù),如果在某些業(yè)務(wù)場(chǎng)景我們想自定義自己的異步線程池也是可以的。

4、Spring的@Async異步

(1)自定義異步線程池

/**
 * 線程池參數(shù)配置,多個(gè)線程池實(shí)現(xiàn)線程池隔離,@Async注解,默認(rèn)使用系統(tǒng)自定義線程池,可在項(xiàng)目中設(shè)置多個(gè)線程池,在異步調(diào)用的時(shí)候,指明需要調(diào)用的線程池名稱,比如:@Async("taskName")
@EnableAsync
@Configuration
public class TaskPoolConfig {
    /**
     * 自定義線程池
     *
     **/
    @Bean("taskExecutor")
    public Executor taskExecutor() {
        //返回可用處理器的Java虛擬機(jī)的數(shù)量 12
        int i = Runtime.getRuntime().availableProcessors();
        System.out.println("系統(tǒng)最大線程數(shù)  :" + i);
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心線程池大小
        executor.setCorePoolSize(16);
        //最大線程數(shù)
        executor.setMaxPoolSize(20);
        //配置隊(duì)列容量,默認(rèn)值為Integer.MAX_VALUE
        executor.setQueueCapacity(99999);
        //活躍時(shí)間
        executor.setKeepAliveSeconds(60);
        //線程名字前綴
        executor.setThreadNamePrefix("asyncServiceExecutor -");
        //設(shè)置此執(zhí)行程序應(yīng)該在關(guān)閉時(shí)阻止的最大秒數(shù),以便在容器的其余部分繼續(xù)關(guān)閉之前等待剩余的任務(wù)完成他們的執(zhí)行
        executor.setAwaitTerminationSeconds(60);
        //等待所有的任務(wù)結(jié)束后再關(guān)閉線程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}

(2) AsyncService

public interface AsyncService {


    MessageResult sendSms(String callPrefix, String mobile, String actionType, String content);


    MessageResult sendEmail(String email, String subject, String content);
}


@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {
    @Autowired
    private IMessageHandler mesageHandler;


    @Override
    @Async("taskExecutor")
    public MessageResult sendSms(String callPrefix, String mobile, String actionType, String content) {
        try {




            Thread.sleep(1000);
            mesageHandler.sendSms(callPrefix, mobile, actionType, content);




        } catch (Exception e) {
            log.error("發(fā)送短信異常 -> ", e)
        }
    }
    
    @Override
    @Async("taskExecutor")
    public sendEmail(String email, String subject, String content) {
        try {


            Thread.sleep(1000);
            mesageHandler.sendsendEmail(email, subject, content);


        } catch (Exception e) {
            log.error("發(fā)送email異常 -> ", e)
        }
    }
}

在實(shí)際項(xiàng)目中, 使用@Async調(diào)用線程池,推薦等方式是是使用自定義線程池的模式,不推薦直接使用@Async直接實(shí)現(xiàn)異步。

5、Spring ApplicationEvent事件實(shí)現(xiàn)異步

(1)定義事件

public class AsyncSendEmailEvent extends ApplicationEvent {
    /**
     * 郵箱
     **/
    private String email;
   /**
     * 主題
     **/
    private String subject;
    /**
     * 內(nèi)容
     **/
    private String content;
  
    /**
     * 接收者
     **/
    private String targetUserId;


}

(2)定義事件處理器

@Slf4j
@Component
public class AsyncSendEmailEventHandler implements ApplicationListener {


    @Autowired
    private IMessageHandler mesageHandler;
    
    @Async("taskExecutor")
    @Override
    public void onApplicationEvent(AsyncSendEmailEvent event) {
        if (event == null) {
            return;
        }


        String email = event.getEmail();
        String subject = event.getSubject();
        String content = event.getContent();
        String targetUserId = event.getTargetUserId();
        mesageHandler.sendsendEmailSms(email, subject, content, targerUserId);
      }
}

另外,可能有些時(shí)候采用ApplicationEvent實(shí)現(xiàn)異步的使用,當(dāng)程序出現(xiàn)異常錯(cuò)誤的時(shí)候,需要考慮補(bǔ)償機(jī)制,那么這時(shí)候可以結(jié)合Spring Retry重試來幫助我們避免這種異常造成數(shù)據(jù)不一致問題。

6、消息隊(duì)列

(1)回調(diào)事件消息生產(chǎn)者

@Slf4j
@Component
public class CallbackProducer {


    @Autowired
    AmqpTemplate amqpTemplate;


    public void sendCallbackMessage(CallbackDTO allbackDTO, final long delayTimes) {


        log.info("生產(chǎn)者發(fā)送消息,callbackDTO,{}", callbackDTO);


        amqpTemplate.convertAndSend(CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getExchange(), CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getRoutingKey(), JsonMapper.getInstance().toJson(genseeCallbackDTO), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //給消息設(shè)置延遲毫秒值,通過給消息設(shè)置x-delay頭來設(shè)置消息從交換機(jī)發(fā)送到隊(duì)列的延遲時(shí)間
                message.getMessageProperties().setHeader("x-delay", delayTimes);
                message.getMessageProperties().setCorrelationId(callbackDTO.getSdkId());
                return message;
            }
        });
    }
}

(2)回調(diào)事件消息消費(fèi)者

@Slf4j
@Component
@RabbitListener(queues = "message.callback", containerFactory = "rabbitListenerContainerFactory")
public class CallbackConsumer {


    @Autowired
    private IGlobalUserService globalUserService;


    @RabbitHandler
    public void handle(String json, Channel channel, @Headers Map map) throws Exception {


        if (map.get("error") != null) {
            //否認(rèn)消息
            channel.basicNack((Long) map.get(AmqpHeaders.DELIVERY_TAG), false, true);
            return;
        }


        try {
            CallbackDTO callbackDTO = JsonMapper.getInstance().fromJson(json, CallbackDTO.class);
            //執(zhí)行業(yè)務(wù)邏輯
            globalUserService.execute(callbackDTO);
            //消息消息成功手動(dòng)確認(rèn),對(duì)應(yīng)消息確認(rèn)模式acknowledge-mode: manual
            channel.basicAck((Long) map.get(AmqpHeaders.DELIVERY_TAG), false);
        } catch (Exception e) {
            log.error("回調(diào)失敗 -> {}", e);
        }
    }
}

7、ThreadUtil異步工具類

@Slf4j
public class ThreadUtils {


    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            ThreadUtil.execAsync(() -> {
                ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
                int number = threadLocalRandom.nextInt(20) + 1;
                System.out.println(number);
            });
            log.info("當(dāng)前第:" + i + "個(gè)線程");
        }


        log.info("task finish!");
    }
}

8、Guava異步

Guava的ListenableFuture顧名思義就是可以監(jiān)聽的Future,是對(duì)java原生Future的擴(kuò)展增強(qiáng)。我們知道Future表示一個(gè)異步計(jì)算任務(wù),當(dāng)任務(wù)完成時(shí)可以得到計(jì)算結(jié)果。如果我們希望一旦計(jì)算完成就拿到結(jié)果展示給用戶或者做另外的計(jì)算,就必須使用另一個(gè)線程不斷的查詢計(jì)算狀態(tài)。這樣做,代碼復(fù)雜,而且效率低下。使用「Guava ListenableFuture」可以幫我們檢測(cè)Future是否完成了,不需要再通過get()方法苦苦等待異步的計(jì)算結(jié)果,如果完成就自動(dòng)調(diào)用回調(diào)函數(shù),這樣可以減少并發(fā)程序的復(fù)雜度。

ListenableFuture是一個(gè)接口,它從jdk的Future接口繼承,添加了void addListener(Runnable listener, Executor executor)方法。

我們看下如何使用ListenableFuture。首先需要定義ListenableFuture的實(shí)例:

ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        final ListenableFuture listenableFuture = executorService.submit(new Callable() {
            @Override
            public Integer call() throws Exception {
                log.info("callable execute...")
                TimeUnit.SECONDS.sleep(1);
                return 1;
            }
        });

首先通過MoreExecutors類的靜態(tài)方法listeningDecorator方法初始化一個(gè)ListeningExecutorService的方法,然后使用此實(shí)例的submit方法即可初始化ListenableFuture對(duì)象。

ListenableFuture要做的工作,在Callable接口的實(shí)現(xiàn)類中定義,這里只是休眠了1秒鐘然后返回一個(gè)數(shù)字1,有了ListenableFuture實(shí)例,可以執(zhí)行此Future并執(zhí)行Future完成之后的回調(diào)函數(shù)。

Futures.addCallback(listenableFuture, new FutureCallback() {
    @Override
    public void onSuccess(Integer result) {
        //成功執(zhí)行...
        System.out.println("Get listenable future's result with callback " + result);
    }


    @Override
    public void onFailure(Throwable t) {
        //異常情況處理...
        t.printStackTrace();
    }
});

那么,以上就是本期介紹的分布式服務(wù)下實(shí)現(xiàn)異步的八種方式了,供您參考。


新聞名稱:聊聊分布式服務(wù)下的八種異步實(shí)現(xiàn)方式
URL鏈接:http://www.5511xx.com/article/dpsdgjp.html