基于Spring Boot應(yīng)用ApplicationEvent案例場景
記錄:276
場景:利用Spring的機制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent。
版本:Spring Boot 2.6.3
一、案例場景
1.發(fā)起restful請求,根據(jù)請求參數(shù)發(fā)布不同的事件。
2.事件監(jiān)聽者,監(jiān)聽到事件后,做預(yù)定操作。
3.本例是事件同步處理機制,即發(fā)布事件后,會同步監(jiān)聽事件。
二、使用類
org.springframework.context.ApplicationEvent,spring的事件對象。
org.springframework.context.ApplicationListener,事件監(jiān)聽者接口。
org.springframework.context.ApplicationEventPublisher,事件發(fā)布者接口。
三、代碼
1.事件對象
事件對象實現(xiàn)ApplicationEvent。
1.1 ExampleApplicationEvent
ExampleApplicationEvent,一個抽象類。繼承ApplicationEvent,自定義拓展微服務(wù)中需求的一些屬性。
public abstract class ExampleApplicationEvent extends ApplicationEvent { private static final String eventSource = "Example"; private String eventType = null; private Object eventData = null; public ExampleApplicationEvent(Object eventData) { super(eventSource); this.eventData = eventData; } public ExampleApplicationEvent(Object eventData, String eventType) { super(eventSource); this.eventData = eventData; this.eventType = eventType; } public String getEventType() { return eventType; } public Object getEventData() { return eventData; } }
1.2 ExampleLocalApplicationEvent
ExampleLocalApplicationEvent,是抽象類ExampleApplicationEvent的實現(xiàn)類,在此處按需拓展屬性。
public class ExampleLocalApplicationEvent extends ExampleApplicationEvent { public ExampleLocalApplicationEvent(Object eventData) { super(eventData); } public ExampleLocalApplicationEvent(Object eventData, String eventType) { super(eventData, eventType); } }
1.3 EventTypeEnum
EventTypeEnum,自定義事件類型枚舉,按需擴展。
public enum EventTypeEnum { CHANGE("change", "變更事件"), ADD("add", "新增事件"); private String id; private String name; public String getId() { return id; } public String getName() { return name; } EventTypeEnum(String id, String name) { this.id = id; this.name = name; } public static EventTypeEnum getEventTypeEnum(String id) { for (EventTypeEnum var : EventTypeEnum.values()) { if (var.getId().equalsIgnoreCase(id)) { return var; } } return null; } }
2.事件監(jiān)聽者
事件監(jiān)聽者包括接口和抽象類。
2.1 IEventListener
IEventListener,一個接口,繼承ApplicationListener接口。
@SuppressWarnings("rawtypes") public interface IEventListener extends ApplicationListener { }
2.2 AbstractEventListener
AbstractEventListener,一個抽象類,實現(xiàn)IEventListener接口。并提供抽象方法便于實現(xiàn)類擴展和代碼解耦。
public abstract class AbstractEventListener implements IEventListener { @Override public void onApplicationEvent(ApplicationEvent event){ if(!(event instanceof ExampleApplicationEvent)){ return; } ExampleApplicationEvent exEvent = (ExampleApplicationEvent) event; try{ onExampleApplicationEvent(exEvent); }catch (Exception e){ e.printStackTrace(); } } protected abstract void onExampleApplicationEvent(ExampleApplicationEvent event); }
2.3 OrderEventListener
OrderEventListener,實現(xiàn)類AbstractEventListener抽象類。監(jiān)聽事件,并對事件做處理,是一個業(yè)務(wù)類。
@Slf4j @Component public class OrderEventListener extends AbstractEventListener { @Override protected void onExampleApplicationEvent(ExampleApplicationEvent event) { log.info("OrderEventListener->onSpApplicationEvent,監(jiān)聽事件."); Object eventData = event.getEventData(); log.info("事件類型: " + EventTypeEnum.getEventTypeEnum(event.getEventType())); log.info("事件數(shù)據(jù): " + eventData.toString()); } }
3.事件發(fā)布者
事件監(jiān)聽者包括接口和實現(xiàn)類。
3.1 IEventPublisher
IEventPublisher,自定義事件發(fā)布接口,方便擴展功能和屬性。
public interface IEventPublisher { boolean publish(ExampleApplicationEvent event); }
3.2 LocalEventPublisher
LocalEventPublisher,事件發(fā)布實現(xiàn)類,此類使用@Component,spring的IOC容器會加載此類。此類調(diào)用ApplicationEventPublisher的publishEvent發(fā)布事件。
@Slf4j @Component("localEventPublisher") public class LocalEventPublisher implements IEventPublisher { @Override public boolean publish(ExampleApplicationEvent event) { try{ log.info("LocalEventPublisher->publish,發(fā)布事件."); log.info("事件類型: " + EventTypeEnum.getEventTypeEnum(event.getEventType())); log.info("事件數(shù)據(jù): " + event.getEventData().toString()); SpringUtil.getApplicationContext().publishEvent(event); }catch (Exception e){ log.info("事件發(fā)布異常."); e.printStackTrace(); return false; } return true; } }
4.Restful請求觸發(fā)事件
使用Restful請求觸發(fā)事件發(fā)生。
4.1 EventController
EventController,接收Restful請求。
@Slf4j @RestController @RequestMapping("/event") public class EventController { @Autowired private LocalEventPublisher eventPublisher; @PostMapping("/f1") public Object f1(@RequestBody Object obj) { log.info("EventController->f1,接收參數(shù),obj = " + obj.toString()); Map objMap = (Map) obj; OrderInfo orderInfo = new OrderInfo(); orderInfo.setUserName((String) objMap.get("userName")); orderInfo.setTradeName((String) objMap.get("tradeName")); orderInfo.setReceiveTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")); String flag = (String) objMap.get("flag"); if (StringUtils.equals("change", flag)) { eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo, EventTypeEnum.CHANGE.getId())); } else if (StringUtils.equals("add", flag)) { eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo, EventTypeEnum.ADD.getId())); } else { eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo)); } log.info("EventController->f1,返回."); return ResultObj.builder().code("200").message("成功").build(); } }
4.2 OrderInfo
OrderInfo,數(shù)據(jù)對象,放入事件對象中傳遞。
@Data @NoArgsConstructor public class OrderInfo { private String userName; private String tradeName; private String receiveTime; }
4.3 ResultObj
ResultObj,restful返回通用對象。
@Data @NoArgsConstructor @AllArgsConstructor @Builder public class ResultObj { private String code; private String message; }
5.測試
5.1 請求信息
URL請求: http://127.0.0.1:8080/server/event/f1
入?yún)ⅲ?/p>
{ "userName": "HangZhou", "tradeName": "Vue進階教程", "flag": "add" }
返回值:
{ "code": "200", "message": "成功" }
5.2 日志
輸出日志:
以上,感謝。
到此這篇關(guān)于基于Spring Boot應(yīng)用ApplicationEvent的文章就介紹到這了,更多相關(guān)Spring Boot應(yīng)用ApplicationEvent內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-data-redis自定義實現(xiàn)看門狗機制
redission看門狗機制是解決分布式鎖的續(xù)約問題,本文主要介紹了spring-data-redis自定義實現(xiàn)看門狗機制,具有一定的參考價值,感興趣的可以了解一下2024-03-03Jetty啟動項目中引用json-lib相關(guān)類庫報錯ClassNotFound的解決方案
今天小編就為大家分享一篇關(guān)于Jetty啟動項目中引用json-lib相關(guān)類庫報錯ClassNotFound的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Springboot FeignClient調(diào)用Method has too m
本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題
這篇文章主要介紹了SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06SpringSecurity?用戶帳號已被鎖定的問題及解決方法
這篇文章主要介紹了SpringSecurity?用戶帳號已被鎖定,本文給大家分享問題原因及解決方式,需要的朋友可以參考下2023-12-12java實現(xiàn)ftp上傳 如何創(chuàng)建文件夾
這篇文章主要為大家詳細介紹了java實現(xiàn)ftp上傳的相關(guān)資料,教大家如何創(chuàng)建文件夾?具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04