创建基础消息体,开始针对消息类通讯整合
This commit is contained in:
parent
2c412b32a7
commit
ac40c0f92f
@ -0,0 +1,172 @@
|
|||||||
|
package com.ruoyi.common.core.domain;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
|
|
||||||
|
public class Message {
|
||||||
|
|
||||||
|
/** 消息唯一标识符 */
|
||||||
|
private String messageId;
|
||||||
|
/** 发送者标识 */
|
||||||
|
private String sender;
|
||||||
|
/** 接收者标识 */
|
||||||
|
private String receiver;
|
||||||
|
/** 消息时间戳 */
|
||||||
|
private Instant timestamp;
|
||||||
|
/** 消息类型(如命令、聊天、日志、事件等) */
|
||||||
|
private String type;
|
||||||
|
/** 消息主题或事件名称 */
|
||||||
|
private String subject;
|
||||||
|
/** 消息数据负载 */
|
||||||
|
private Map<String, Object> payload;
|
||||||
|
/** 元数据,用于存储额外的信息 */
|
||||||
|
private Map<String, Object> metadata;
|
||||||
|
/** 消息状态(如成功、失败、重试等) */
|
||||||
|
private String status;
|
||||||
|
/** 重试次数 */
|
||||||
|
private int retryCount;
|
||||||
|
/** 最大重试次数 */
|
||||||
|
private int maxRetries;
|
||||||
|
/** 重试间隔 */
|
||||||
|
private String retryInterval;
|
||||||
|
|
||||||
|
// 构造函数
|
||||||
|
public Message() {
|
||||||
|
this.messageId = UUID.randomUUID().toString();
|
||||||
|
this.timestamp = Instant.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Message create() {
|
||||||
|
return new Message();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessageId() {
|
||||||
|
return messageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setMessageId(String messageId) {
|
||||||
|
this.messageId = messageId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSender() {
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setSender(String sender) {
|
||||||
|
this.sender = sender;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getReceiver() {
|
||||||
|
return receiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setReceiver(String receiver) {
|
||||||
|
this.receiver = receiver;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Instant getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setTimestamp(Instant timestamp) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSubject() {
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setSubject(String subject) {
|
||||||
|
this.subject = subject;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getPayload() {
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setPayload(Map<String, Object> payload) {
|
||||||
|
this.payload = payload;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getMetadata() {
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setMetadata(Map<String, Object> metadata) {
|
||||||
|
this.metadata = metadata;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setStatus(String status) {
|
||||||
|
this.status = status;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRetryCount() {
|
||||||
|
return retryCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setRetryCount(int retryCount) {
|
||||||
|
this.retryCount = retryCount;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxRetries() {
|
||||||
|
return maxRetries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setMaxRetries(int maxRetries) {
|
||||||
|
this.maxRetries = maxRetries;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRetryInterval() {
|
||||||
|
return retryInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message setRetryInterval(String retryInterval) {
|
||||||
|
this.retryInterval = retryInterval;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
|
||||||
|
.append("messageId", getMessageId())
|
||||||
|
.append("sender", getSender())
|
||||||
|
.append("receiver", getReceiver())
|
||||||
|
.append("timestamp", getTimestamp())
|
||||||
|
.append("type", getType())
|
||||||
|
.append("subject", getSubject())
|
||||||
|
.append("payload", getPayload())
|
||||||
|
.append("metadata", getMetadata())
|
||||||
|
.append("status", getStatus())
|
||||||
|
.append("retryCount", getRetryCount())
|
||||||
|
.append("maxRetries", getMaxRetries())
|
||||||
|
.append("retryInterval", getRetryInterval())
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -1,28 +1,17 @@
|
|||||||
package com.ruoyi.middleware.rabbitmq;
|
package com.ruoyi.middleware.rabbitmq;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.Message;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RabbitListener(queues = "TestDirectQueue") // 监听的队列名称 TestDirectQueue
|
@RabbitListener(queues = "TestDirectQueue") // 监听的队列名称 TestDirectQueue
|
||||||
public class DirectReceiver {
|
public class DirectReceiver {
|
||||||
|
|
||||||
@RabbitHandler
|
@RabbitHandler
|
||||||
public void process(Map map) {
|
public void process(Message map) {
|
||||||
System.out.println("DirectReceiver m消费者收到消息 : " + map.toString());
|
System.out.println("DirectReceiver m消费者收到消息 : " + map.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@RabbitHandler
|
|
||||||
public void process(String map) {
|
|
||||||
System.out.println("DirectReceiver s消费者收到消息 : " + map.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@RabbitHandler
|
|
||||||
public void process(Byte[] b) {
|
|
||||||
System.out.println("DirectReceiver s消费者收到消息 : " + b.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,10 +1,6 @@
|
|||||||
package com.ruoyi.middleware.rabbitmq;
|
package com.ruoyi.middleware.rabbitmq;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -13,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.ruoyi.common.annotation.Anonymous;
|
import com.ruoyi.common.annotation.Anonymous;
|
||||||
|
import com.ruoyi.common.core.domain.Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author : JCccc
|
* @Author : JCccc
|
||||||
@ -24,22 +21,21 @@ import com.ruoyi.common.annotation.Anonymous;
|
|||||||
public class SendMessageController {
|
public class SendMessageController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
|
RabbitTemplate rabbitTemplate; // 使用RabbitTemplate,这提供了接收/发送等等方法
|
||||||
|
|
||||||
@GetMapping("/sendDirectMessage")
|
@GetMapping("/sendDirectMessage")
|
||||||
@Anonymous
|
@Anonymous
|
||||||
public String sendDirectMessage() {
|
public String sendDirectMessage() {
|
||||||
String messageId = String.valueOf(UUID.randomUUID());
|
// 将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
|
||||||
String messageData = "test message, hello!";
|
rabbitTemplate.convertAndSend(
|
||||||
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
"TestDirectExchange",
|
||||||
Map<String,Object> map=new HashMap<>();
|
"TestDirectRouting",
|
||||||
map.put("messageId",messageId);
|
Message.create()
|
||||||
map.put("messageData",messageData);
|
.setPayload(Map.of("message", "你好"))
|
||||||
map.put("createTime",createTime);
|
.setReceiver("接收者")
|
||||||
//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
|
.setSender("发送者")
|
||||||
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
|
);
|
||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user