1. 获取Bean对象
applicationContext.getBean(beanName, bean.class);
public interface SendMsgHandle { void sendMsg(String msg); } @Service("statusUpdateMsgHandler") @Slf4j public class StatusUpdateMsgHandler implements SendMsgHandle{ @Override public void sendMsg(String msg) { log.info("receive msg: {}", msg); } } SendMsgHandle handler = webApplicationContext.getBean("statusUpdateMsgHandler", SendMsgHandle.class); handler.sendMsg(JSON.toJSonString("test"));
// obj为传递的事件对象 applicationContext.publishEvent(Object obj); // 订阅消息事件:使用注解@EventListener,该注解默认是同步执行的,若要实现异步执行,需加@Async。方法参数为事件对象,指明要监听订阅的事件。 public void execute(Object obj) {} ====================================== 分隔符 ====================================== // 1.创建要发送的内容对象 OrderDTO orderDTO = OrderDTO.builder().no("6853738696568484").build(); // 发送创建订单成功相关消息 applicationContext.publishEvent(orderDTO); // 2.监听接收 @Component @Slf4j public class OrderDTOListener { @EventListener @Async("async_thread_pool_bean_name") public void execute(OrderDTO orderDTO) { // 发送创建订单成功相关消息 if (Objects.nonNull(orderDTO) && StringUtils.isNotEmpty(orderDTO.getNo())){ String jsonData = JSONObject.toJSonString(orderDTO); log.info("接收: {}", jsonData); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)