spring event mechanism combat

theory

In the distributed scenario, synchronized asynchronous transfer mode in three ways: . Br />. 1 performs asynchronous thread pool; @Asyn means such annotations, spring comes into the thread pool to execute;
2 into the message queue , the asynchronous code in consumer spending, the implementation of the relevant logic;
3. event-based mechanism spring trigger events, implementation-dependent logic inside the listener;

spring comes with the support of the event, the core class is ApplicationEventPublisher ;

Event includes three main points: The following is a demo implementation theory with actual combat.

Definition 1 event;

package com.springbootpractice.demoevent.event;

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

/**
 * @说明 吃饭事件
 * @作者 carter
 * @创建时间 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 event listener definition;

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的事件监听器
 *
 * @author carter
 * 创建时间 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 release event;

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
 * 创建时间 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";
    }

}

No extra configuration, springmvc directly supported;

Real

The complete code addresses

Guess you like

Origin blog.51cto.com/14502478/2430181