| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package jnpf.handler;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.lock.LockTemplate;
- import jnpf.consts.ProjectEventConst;
- import jnpf.module.ProjectEventInstance;
- import jnpf.util.JsonUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.context.ApplicationEventPublisher;
- import org.springframework.context.ApplicationEventPublisherAware;
- import org.springframework.messaging.Message;
- import java.util.function.Consumer;
- /**
- * 自定义事件监听, MQ渠道, 收到通知后发送Spring事件(RedisEventInstance)
- */
- @Slf4j
- public class ProjectEventMQMessageHandler implements Consumer<Message<?>>, ApplicationEventPublisherAware {
- private ApplicationEventPublisher applicationEventPublisher;
- private final LockTemplate lockTemplate;
- public ProjectEventMQMessageHandler(LockTemplate lockTemplate) {
- this.lockTemplate = lockTemplate;
- log.info("初始化自定义事件MQ监听器");
- }
- @Override
- public void accept(Message<?> o) {
- if(log.isDebugEnabled()){
- log.debug("事件监听收到MQ消息:{}", JSON.toJSONString(o));
- }
- // 是否存在自定义事件的标识
- if(o.getHeaders().get(ProjectEventConst.DEFAULT_CHANNEL_PREFIX) == null){
- if(log.isDebugEnabled()){
- log.debug("事件监听忽略MQ消息:{}", JSON.toJSONString(o));
- }
- return;
- }
- Object payload = o.getPayload();
- ProjectEventInstance instance;
- if(payload instanceof byte[]){
- instance = JSON.parseObject((byte[])payload, ProjectEventInstance.class);
- }else if(payload instanceof String){
- instance = JSON.parseObject((String)payload, ProjectEventInstance.class);
- }else{
- instance = JsonUtil.getJsonToBean(payload, ProjectEventInstance.class);
- }
- applicationEventPublisher.publishEvent(instance);
- }
- @Override
- public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
- this.applicationEventPublisher = applicationEventPublisher;
- }
- }
|