1.新增mq相关

This commit is contained in:
cuizhibin 2025-04-22 17:34:24 +08:00
parent be07477cc7
commit ef3ae576ba
10 changed files with 196 additions and 6 deletions

View File

@ -35,7 +35,6 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -143,8 +142,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>

32
data-bus/pom.xml Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.dite.znpt</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>org.dite.znpt</groupId>
<artifactId>data-bus</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.dite.znpt</groupId>
<artifactId>core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,67 @@
package com.dite.znpt.data.bus.config;
import cn.hutool.extra.spring.SpringUtil;
import com.dite.znpt.data.bus.config.enums.MQTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
@Slf4j
@Component
public class MQConfig {
@Autowired
private ConnectionFactory connectionFactory;
@PostConstruct
public void registerListeners() {
for (MQTypeEnum typeEnum : MQTypeEnum.values()) {
String queueName = typeEnum.getType() + ".queue";
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(message -> {
String body = new String(message.getBody());
log.debug("队列: " + queueName + " 收到消息: " + body);
try {
SpringUtil.getBean(typeEnum.getImpl()).handle(body);
} catch (Exception e) {
log.error("mq队列处理出错", e);
}
});
// 启动监听
container.start();
}
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("data.topic.exchange");
}
@Bean
public List<Queue> declareQueues() {
return Arrays.stream(MQTypeEnum.values())
.map(type -> new Queue(type + ".queue", true))
.toList();
}
@Bean
public List<Binding> declareBindings(TopicExchange topicExchange) {
return Arrays.stream(MQTypeEnum.values())
.map(type -> BindingBuilder
.bind(new Queue(type + ".queue"))
.to(topicExchange)
.with(type + ".*"))
.toList();
}
}

View File

@ -0,0 +1,20 @@
package com.dite.znpt.data.bus.config;
import com.fasterxml.jackson.core.JsonProcessingException;
/**
* @author cuizhibin
* @date 2025/04/22 17:03
* @description mq处理接口
*/
public interface MQHandle {
/**
* 功能描述mq处理类
*
* @param msg 消息
* @author cuizhibin
* @date 2025/04/22 17:03
**/
void handle(String msg) throws JsonProcessingException;
}

View File

@ -0,0 +1,9 @@
package com.dite.znpt.data.bus.config.entity;
import lombok.Data;
@Data
public class CommonMessage<T> {
private String operation;
private T payload;
}

View File

@ -0,0 +1,4 @@
package com.dite.znpt.data.bus.config.entity.req;
public class ProjectDTO {
}

View File

@ -0,0 +1,28 @@
package com.dite.znpt.data.bus.config.enums;
import com.dite.znpt.data.bus.config.MQHandle;
import com.dite.znpt.data.bus.config.handles.ProjectHandleImpl;
import lombok.Getter;
/**
* @author cuizhibin
* @date 2025/04/22 17:05
* @description mq类型枚举
*/
@Getter
public enum MQTypeEnum {
PROJECT("project", "项目", ProjectHandleImpl.class),
CREW("crew", "机组", ProjectHandleImpl.class),
PARTS("parts", "部件", ProjectHandleImpl.class),
;
private final String type;
private final String desc;
private final Class<? extends MQHandle> impl;
MQTypeEnum(String type, String desc, Class<? extends MQHandle> impl) {
this.type = type;
this.desc = desc;
this.impl = impl;
}
}

View File

@ -0,0 +1,31 @@
package com.dite.znpt.data.bus.config.handles;
import cn.hutool.json.JSONUtil;
import com.dite.znpt.data.bus.config.MQHandle;
import com.dite.znpt.data.bus.config.entity.CommonMessage;
import com.dite.znpt.data.bus.config.entity.req.ProjectDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
/**
* @author cuizhibin
* @date 2025/04/22 17:11
* @description 项目处理
*/
@Service
public class ProjectHandleImpl implements MQHandle {
/**
* 功能描述mq处理类
*
* @param msg 消息
* @author cuizhibin
* @date 2025/04/22 17:03
**/
@Override
public void handle(String msg) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
CommonMessage<ProjectDTO> projectMsg = objectMapper.readValue(msg,
objectMapper.getTypeFactory().constructParametricType(CommonMessage.class, ProjectDTO.class));
}
}

View File

@ -14,6 +14,7 @@
<module>core</module>
<module>sip</module>
<module>web</module>
<module>data-bus</module>
</modules>
<packaging>pom</packaging>
@ -43,8 +44,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>

View File

@ -27,7 +27,6 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- sip协议栈 -->