溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

spring的事件機(jī)制實(shí)戰(zhàn)

發(fā)布時(shí)間:2020-02-21 09:15:14 來源:網(wǎng)絡(luò) 閱讀:288 作者:carterspring 欄目:開發(fā)技術(shù)

理論

在分布式場景下,實(shí)現(xiàn)同步轉(zhuǎn)異步的方式有三種方式:1.異步線程池執(zhí)行;比如借助@Asyn注解,放到spring自帶的線程池中去執(zhí)行;
br/>1.異步線程池執(zhí)行;比如借助@Asyn注解,放到spring自帶的線程池中去執(zhí)行;
3.基于spring的事件機(jī)制,觸發(fā)事件,在監(jiān)聽器里實(shí)現(xiàn)相關(guān)邏輯;

spring中自帶了事件的支持,核心類是ApplicationEventPublisher;

事件包括三個(gè)要點(diǎn):下面是一個(gè)demo的實(shí)現(xiàn),理論結(jié)合實(shí)戰(zhàn)。

1 事件的定義;

package com.springbootpractice.demoevent.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;

/**
 * @說明 吃飯事件
 * @作者 carter
 * @創(chuàng)建時(shí)間 2019年07月12日 13:12
 **/
public class EatEvent extends ApplicationEvent {

    private static final Logger logger = LoggerFactory.getLogger(EatEvent.class);

    private Boolean eatFinished;

    public EatEvent(Boolean eatFinished) {
        super(eatFinished);
        this.eatFinished = eatFinished;
    }

    public void callGirlFriend() {
        logger.info("美女,吃完飯了,來收拾一下吧!");
    }

    public void callBrothers() {
        logger.info("兄弟們,吃完飯了,來打dota !");
    }

    public Boolean getEatFinished() {
        return eatFinished;
    }
}

2 事件監(jiān)聽的定義;

package com.springbootpractice.demoevent.event.listener;

import com.springbootpractice.demoevent.event.EatEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 說明: XEvent的事件監(jiān)聽器
 *
 * @author carter
 * 創(chuàng)建時(shí)間 2019年07月12日 13:19
 **/
@Component
public class EatEventListener implements ApplicationListener<EatEvent> {

    private static final Logger logger = LoggerFactory.getLogger(EatEventListener.class);

    @Override
    public void onApplicationEvent(EatEvent xEvent) {
        if (Objects.isNull(xEvent)) {
            return;
        }

        if (xEvent.getEatFinished()) {
            xEvent.callGirlFriend();
            logger.info("xxxx,女朋友拒絕收拾!");
            xEvent.callBrothers();
            logger.info("滿人了,下次帶你!");
        }

    }
}

3 發(fā)布事件;

package com.springbootpractice.demoevent.web;

import com.springbootpractice.demoevent.event.EatEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 說明 測試控制器
 *
 * @author carter
 * 創(chuàng)建時(shí)間 2019年07月12日 13:23
 **/
@RestController
public class TestController {

    private final ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    public TestController(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @GetMapping(path = "/eatOver")
    public Object eatOver() {
        EatEvent xEvent = new EatEvent(true);
        applicationEventPublisher.publishEvent(xEvent);
        return "eat over and publish event success";
    }

}

無需多余的配置,springmvc直接支持的;

實(shí)戰(zhàn)

完整代碼地址

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI