directory

  • 1. The basic
  • 2. Xml-based configuration of IOC
  • 3. Annotations based configuration of IOC
  • 4. Concepts related to AOP
  • 6. The spring into the order
  • 7. Message monitoring mechanism
  • 8. Behavior of things control and dissemination
  • 9. The Spring source code
  • 10. The Spring lifecycle

Message events are an important feature of Spring, where notifications are sent in one place and code is executed in another.

1. Related concepts:

A message: any class that implements the ApplicationEvent interface can be used as a message

(2) Message publisher: Spring’s event publisher publishes our message and calls the publishEvent method of AppliacionPublisher to publish the message

(iii) Message listener: receiver of Spring message, and complete relevant operations after receiving the message

2. Practical application

Message:

// Implements the ApplicationEvent method, overriding its constructor
public class MyEvent1 extends ApplicationEvent {
    private String message;

    public MyEvent1(Object source,String message) {
        super(source);
        this.message = message;
    }

    public String getMessage(a) {
        return message;
    }

    public void setMessage(String message) {
        this.message = message; }}Copy the code

Publisher:

/ / call the publisher applicationEventPublisher method, the news release
class PublishEvent {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void getEvent(a) {
        System.out.println("---- release 1 start -------");
        MyEvent1 event = new MyEvent1(this."News 1");
        applicationEventPublisher.publishEvent(event);// The publisher publishes the message
        System.out.println("---- released 1 completed -------"); }}Copy the code

Listener:

// Implements the ApplicationListener interface, implements the onApplicationEvent method, and registers it with the Spring container
@Component
public class Listener1 implements ApplicationListener<MyEvent1> {

    @Override
    public void onApplicationEvent(MyEvent1 event) {
        System.out.println("Listener1 received an event notification:" + event.getMessage());
        //do something}}Copy the code

3. Use the @eventListener annotation

Using the @EventListener annotation on a listener’s method implements the ApplicationListener interface, and a listener can have multiple @EventListeners to listen for multiple messages

/** * Students listen to various events and respond to them */
@Component
public class Student {

	/** * listen for the bell@param event1
	 */
	@EventListener
	public void listenSchoolBell(MyEvent1 event1) {
		System.out.println("That's the bell. I'm going to class...");
		//do something
	}
	
	/** * listen to the teacher's questions *@param event2
	 */
	@EventListener
	public void listenTeacherAskMe(MyEvent1 event2) {
		System.out.println("The teacher asked a question, I will stand up to answer the question...");
		//do something}}Copy the code

4. Asynchronous listening

In spring news listening in default is to comply with the provisions of circular manner, if a message listener is abnormal, the subsequent news conference, so we can use asynchronous messaging to monitor, after each listener receives the message, to create a new thread to handle the news events, so that you can avoid the message listener abnormal blocking problem

(1) Use @enableAsync annotation on the configuration class to indicate that asynchronous methods are enabled in this project

(2) use the @async annotation on methods where listeners need to use asynchronous listening

// Enable asynchronous processing
@EnableAsync
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
		public static void main(String[] args) { SpringApplication.run(Application.class, args); }}@Component
public class Listener4 {

// Use the @async annotation on the listener method
	@Async
	@EventListener
	public void listenerMyEvent1(MyEvent1 event) {
		System.out.println("Listener4 received an event notification:"+ event.getMessage()); }}Copy the code

5. Source code analysis

(I) Spring event mechanism process

To be continued…