In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about the principle of the message event mechanism in Spring Boot. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Spring Boot has its own message mechanism, which allows us to publish messages in one place, receive messages and process messages in multiple places at the same time, of course, this is done in the same JVM memory, and different processes need to use MQ to implement. I think this message pattern is somewhat different from the observer pattern, which generally observes the use of an object when its internal properties change. The message mechanism can be used anywhere.
The message event itself is an object that inherits from ApplicationEvent
@ Datapublic class DemoEvent extends ApplicationEvent {private String type; private List msg; public DemoEvent (Object source,String type,List msg) {super (source); this.type = type; this.msg = msg;}}
There also needs to be a message event publisher to publish the message event.
@ Componentpublic class DemoPublisher {@ Autowired ApplicationContext applicationContext; public void publish (DemoEvent demoEvent) {applicationContext.publishEvent (demoEvent);}}
Then there is our listener. Any listener can do different processing according to the business. He can write it in two ways, one is to implement the ApplicationListener interface, and the other is to tag the method with @ EventListener.
@ Component@Slf4jpublic class DemoListener implements ApplicationListener {@ Override @ Async public void onApplicationEvent (DemoEvent demoEvent) {log.info ("received publisher sent to message, time" + Time.getTime ()); List msg = demoEvent.getMsg (); String type = demoEvent.getType (); try {Thread.sleep (3000);} catch (Exception e) {e.printStackTrace () } log.info ("type" + type + ", message content:" + msg);} @ Component@Slf4jpublic class DemoListener1 {@ EventListener public void onDemoEvent (DemoEvent demoEvent) {log.info ("listener1 received the message sent by publisher through annotations, time" + Time.getTime ()); String type = demoEvent.getType (); List msg = demoEvent.getMsg (); try {Thread.sleep (2000) } catch (Exception e) {e.printStackTrace ();} log.info ("listener1: type" + type + ", message content:" + msg);}}
But what we need to know is that multiple message listeners are executed synchronously, and they will block, so we need to do asynchronous listening. to implement asynchronous listening, we only need to mark the @ Async tag on the method and turn on allow async in the Springboot main program.
@ EnableAsync@SpringBootApplicationpublic class LanmdaApplication {public static void main (String [] args) {SpringApplication.run (LanmdaApplication.class, args);}}
Finally, write a test Controller.
@ Slf4j@RestControllerpublic class TestController {@ Autowired private DemoPublisher publisher; @ GetMapping ("/ test") public String testListener () {List list = new ArrayList (); Map M1 = new HashMap (); m1.put ("1", "2"); Map m2 = new HashMap (); m2.put ("3", "4"); Map m3 = new HashMap (); m3.put ("5", "6") List.add (M1); list.add (m2); list.add (m3); log.info ("start release message: + Time.getTime ()); publisher.publish (new DemoEvent (this," Test message ", list)); log.info (" end of message release: "+ Time.getTime ()); return" message released successfully ";}}
After execution, the log is as follows
2019-07-21 10 c.g.lanmda.controller.TestController 42 INFO 39.686 1756-[nio-8080-exec-1] c.g.lanmda.controller.TestController: start posting message: 10:42:39
2019-07-21 10 com.guanjian.lanmda.event.DemoListener1 42 INFO 1756-[nio-8080-exec-1] com.guanjian.lanmda.event.DemoListener1: listener1 received the message sent by publisher through the note, time 10:42:39
2019-07-21 10 INFO 41.690 INFO 1756-[nio-8080-exec-1] com.guanjian.lanmda.event.DemoListener1: listener1: type test message, message content: [{1: 2}, {3: 4}, {5: 6}]
2019-07-21 10 No task executor bean found for async processing 42V 41.695 INFO 1756-[nio-8080-exec-1] .s.a.AnnotationAsyncExecutionInterceptor: No task executor bean found for async processing: no bean of type TaskExecutor and no bean named 'Interceptor
2019-07-21 10 c.g.lanmda.controller.TestController 42V 41.697 INFO 1756-[nio-8080-exec-1] end of message release: 10:42:41
2019-07-21 10 com.guanjian.lanmda.event.DemoListener 42V 41.697 INFO 1756-[cTaskExecutor-1] com.guanjian.lanmda.event.DemoListener: received publisher to send message, time 10:42:41
2019-07-21 10 com.guanjian.lanmda.event.DemoListener 42V 44.701 INFO 1756-[type] com.guanjian.lanmda.event.DemoListener: type test message, message content: [{1: 2}, {3: 4}, {5: 6}]
These are the principles of the message event mechanism in Spring Boot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.