From 34a3d04f1d748624dd1bb7a9e237e3f39d7baa73 Mon Sep 17 00:00:00 2001 From: tonyjj <815175699@qq.com> Date: Wed, 8 Feb 2023 12:12:24 +0000 Subject: [PATCH] =?UTF-8?q?!40=20=E6=B7=BB=E5=8A=A0spring=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E9=A9=B1=E5=8A=A8=EF=BC=8C=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?ApplicationEvent=20*=20=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=95=B4=E5=90=88SpringEvent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringEventBusReceiver.java | 33 +++++++++++++++ .../SpringEventBusReceiverListener.java | 42 +++++++++++++++++++ .../constans/MessageQueuePipeline.java | 2 + .../springeventbus/SpringEventBusEvent.java | 18 ++++++++ .../SpringEventBusSendMqServiceImpl.java | 39 +++++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiver.java create mode 100644 austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiverListener.java create mode 100644 austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusEvent.java create mode 100644 austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusSendMqServiceImpl.java diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiver.java b/austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiver.java new file mode 100644 index 0000000..868497f --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiver.java @@ -0,0 +1,33 @@ +package com.java3y.austin.handler.receiver.springeventbus; + +import com.java3y.austin.common.domain.TaskInfo; +import com.java3y.austin.handler.receiver.service.ConsumeService; +import com.java3y.austin.support.constans.MessageQueuePipeline; +import com.java3y.austin.support.domain.MessageTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 描述: + * + * @author tony + * @date 2023/2/6 11:18 + */ +@Component +@ConditionalOnProperty(name = "austin.mq.pipeline", havingValue = MessageQueuePipeline.SPRING_EVENT_BUS) +public class SpringEventBusReceiver { + + @Autowired + private ConsumeService consumeService; + + public void consume(List lists) { + consumeService.consume2Send(lists); + } + + public void recall(MessageTemplate messageTemplate) { + consumeService.consume2recall(messageTemplate); + } +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiverListener.java b/austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiverListener.java new file mode 100644 index 0000000..e4f329d --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/handler/receiver/springeventbus/SpringEventBusReceiverListener.java @@ -0,0 +1,42 @@ +package com.java3y.austin.handler.receiver.springeventbus; + +import com.alibaba.fastjson.JSON; +import com.java3y.austin.common.domain.TaskInfo; +import com.java3y.austin.support.constans.MessageQueuePipeline; +import com.java3y.austin.support.domain.MessageTemplate; +import com.java3y.austin.support.mq.springeventbus.SpringEventBusEvent; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Service; + +/** + * 描述: + * + * @author tony + * @date 2023/2/6 11:19 + */ +@Service +@ConditionalOnProperty(name = "austin.mq.pipeline", havingValue = MessageQueuePipeline.SPRING_EVENT_BUS) +public class SpringEventBusReceiverListener implements ApplicationListener { + + @Autowired + private SpringEventBusReceiver springEventBusReceiver; + + @Value("${austin.business.topic.name}") + private String sendTopic; + @Value("${austin.business.recall.topic.name}") + private String recallTopic; + + @Override + public void onApplicationEvent(SpringEventBusEvent event) { + String topic = event.getTopic(); + String jsonValue = event.getJsonValue(); + if (topic.equals(sendTopic)) { + springEventBusReceiver.consume(JSON.parseArray(jsonValue, TaskInfo.class)); + } else if (topic.equals(recallTopic)) { + springEventBusReceiver.recall(JSON.parseObject(jsonValue, MessageTemplate.class)); + } + } +} diff --git a/austin-support/src/main/java/com/java3y/austin/support/constans/MessageQueuePipeline.java b/austin-support/src/main/java/com/java3y/austin/support/constans/MessageQueuePipeline.java index e31b81e..247b4eb 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/constans/MessageQueuePipeline.java +++ b/austin-support/src/main/java/com/java3y/austin/support/constans/MessageQueuePipeline.java @@ -12,4 +12,6 @@ public interface MessageQueuePipeline { String ROCKET_MQ = "rocketMq"; String RABBIT_MQ = "rabbitMq"; + String SPRING_EVENT_BUS ="springEventBus"; + } diff --git a/austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusEvent.java b/austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusEvent.java new file mode 100644 index 0000000..08f2807 --- /dev/null +++ b/austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusEvent.java @@ -0,0 +1,18 @@ +package com.java3y.austin.support.mq.springeventbus; + +import lombok.Data; +import org.springframework.context.ApplicationEvent; + +/** + * 描述:消息 + * + * @author tony + * @date 2023/2/6 19:59 + */ +@Data +public class SpringEventBusEvent extends ApplicationEvent { + public String topic; + public String jsonValue; + public String tagId; + +} diff --git a/austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusSendMqServiceImpl.java b/austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusSendMqServiceImpl.java new file mode 100644 index 0000000..05f8e1b --- /dev/null +++ b/austin-support/src/main/java/com/java3y/austin/support/mq/springeventbus/SpringEventBusSendMqServiceImpl.java @@ -0,0 +1,39 @@ +package com.java3y.austin.support.mq.springeventbus; + +import com.java3y.austin.support.constans.MessageQueuePipeline; +import com.java3y.austin.support.mq.SendMqService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +/** + * 描述: + * + * @author tony + * @date 2023/2/6 11:11 + */ +@Slf4j +@Service +@ConditionalOnProperty(name = "austin.mq.pipeline", havingValue = MessageQueuePipeline.SPRING_EVENT_BUS) +public class SpringEventBusSendMqServiceImpl implements SendMqService { + + @Autowired + private ApplicationContext applicationContext; + + + @Override + public void send(String topic, String jsonValue, String tagId) { + SpringEventBusEvent springEventBusEvent = new SpringEventBusEvent(); + springEventBusEvent.setTopic(topic); + springEventBusEvent.setJsonValue(jsonValue); + springEventBusEvent.setTagId(tagId); + applicationContext.publishEvent(springEventBusEvent); + } + + @Override + public void send(String topic, String jsonValue) { + send(topic,jsonValue,null); + } +}