正在显示
5 个修改的文件
包含
76 行增加
和
8 行删除
@@ -76,7 +76,7 @@ public class RabbitController { | @@ -76,7 +76,7 @@ public class RabbitController { | ||
76 | @RequestParam(value = "SEQN", required = false) String SEQN, | 76 | @RequestParam(value = "SEQN", required = false) String SEQN, |
77 | @RequestParam(value = "VSHT") String VSHT, | 77 | @RequestParam(value = "VSHT") String VSHT, |
78 | @RequestParam(value = "SERV") String SERV, | 78 | @RequestParam(value = "SERV") String SERV, |
79 | - @RequestParam(value = "content") String content) | 79 | + @RequestParam(value = "content") String content) throws Exception |
80 | { | 80 | { |
81 | // 1、获取数据 | 81 | // 1、获取数据 |
82 | XmlData xmlData = XmlData.builder() | 82 | XmlData xmlData = XmlData.builder() |
@@ -103,8 +103,8 @@ public class RabbitController { | @@ -103,8 +103,8 @@ public class RabbitController { | ||
103 | return ResultJson.error(CustomExceptionType.BINDING_ERROR); | 103 | return ResultJson.error(CustomExceptionType.BINDING_ERROR); |
104 | } | 104 | } |
105 | // 4、mq发送消息,数据库中保存消息 | 105 | // 4、mq发送消息,数据库中保存消息 |
106 | - ResultJson result = directUtils.sendMessage(); | 106 | + ResultJson result = directUtils.sendMessage(sentData); |
107 | 107 | ||
108 | - return ResultJson.success("发送成功", sentData); | 108 | + return result; |
109 | } | 109 | } |
110 | } | 110 | } |
@@ -105,7 +105,7 @@ public class XmlData implements Serializable { | @@ -105,7 +105,7 @@ public class XmlData implements Serializable { | ||
105 | 105 | ||
106 | private String password; | 106 | private String password; |
107 | 107 | ||
108 | - private String hostIp; | 108 | + private String serverIp; |
109 | 109 | ||
110 | - private Integer hostPort; | 110 | + private Integer serverPort; |
111 | } | 111 | } |
@@ -7,8 +7,11 @@ package com.sunyo.wlpt.message.bus.service.exception; | @@ -7,8 +7,11 @@ package com.sunyo.wlpt.message.bus.service.exception; | ||
7 | */ | 7 | */ |
8 | 8 | ||
9 | public enum CustomExceptionType { | 9 | public enum CustomExceptionType { |
10 | + MESSAGE_SUCCESS("10200", "消息发送成功"), | ||
11 | + | ||
10 | BINDING_ERROR("10501", "配置信息,未进行绑定!"), | 12 | BINDING_ERROR("10501", "配置信息,未进行绑定!"), |
11 | 13 | ||
14 | + | ||
12 | SENDER_ERROR("10401", "报文格式错误,发送者不能为空!"), | 15 | SENDER_ERROR("10401", "报文格式错误,发送者不能为空!"), |
13 | CONTENT_ERROR("10402", "报文格式错误,消息内容不能为空!"), | 16 | CONTENT_ERROR("10402", "报文格式错误,消息内容不能为空!"), |
14 | SERVER_ERROR("10403", "报文格式错误,服务器名称不能为空!"), | 17 | SERVER_ERROR("10403", "报文格式错误,服务器名称不能为空!"), |
1 | package com.sunyo.wlpt.message.bus.service.rabbit.utils; | 1 | package com.sunyo.wlpt.message.bus.service.rabbit.utils; |
2 | 2 | ||
3 | import com.rabbitmq.client.*; | 3 | import com.rabbitmq.client.*; |
4 | +import com.sunyo.wlpt.message.bus.service.domain.XmlData; | ||
5 | +import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType; | ||
4 | import com.sunyo.wlpt.message.bus.service.response.ResultJson; | 6 | import com.sunyo.wlpt.message.bus.service.response.ResultJson; |
5 | import com.sunyo.wlpt.message.bus.service.utils.IdUtils; | 7 | import com.sunyo.wlpt.message.bus.service.utils.IdUtils; |
6 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
@@ -222,9 +224,66 @@ public class DirectUtils { | @@ -222,9 +224,66 @@ public class DirectUtils { | ||
222 | } | 224 | } |
223 | 225 | ||
224 | 226 | ||
225 | - public ResultJson sendMessage() | 227 | + public ResultJson sendMessage(XmlData xmlData) throws Exception |
226 | { | 228 | { |
227 | - return new ResultJson<>(); | 229 | + /** |
230 | + * 可以在这里根据类型的不同,进行不同的消息发送 | ||
231 | + */ | ||
232 | + return directProducer(xmlData); | ||
233 | + } | ||
234 | + | ||
235 | + public ResultJson directProducer(XmlData xmlData) throws Exception | ||
236 | + { | ||
237 | + // 1、创建ConnectionFactory | ||
238 | + // (String hostIp, int hostPort, String vHostName, String userName, String password) throws Exception | ||
239 | + // | ||
240 | + Connection connection = getConnection(xmlData.getServerIp(), xmlData.getServerPort(), | ||
241 | + xmlData.getVirtualHostName(), xmlData.getSender(), xmlData.getPassword()); | ||
242 | + // 2、 通过Connection创建一个新的Channel | ||
243 | + Channel channel = connection.createChannel(); | ||
244 | + // 3、开启消息的确认机制(confirm:保证消息能够发送到 exchange) | ||
245 | + channel.confirmSelect(); | ||
246 | + // 4、避免消息被重复消费 | ||
247 | + AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() | ||
248 | + // 指定消息是否需要持久化,1:需要持久化;2:不需要持久化 | ||
249 | + .deliveryMode(1) | ||
250 | + // 设置全局唯一消息机制id(雪花id) | ||
251 | + .messageId(IdUtils.generateId()) | ||
252 | + .build(); | ||
253 | + // 5、开启 return 机制(保证消息,从 Exchange 分发到 Queue ) | ||
254 | + channel.addReturnListener(new ReturnListener() { | ||
255 | + @Override | ||
256 | + public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException | ||
257 | + { | ||
258 | + // 当消息没有从 Exchange 分发到 Queue 时,才会执行 | ||
259 | + log.error(new String(body, "UTF8") + "->没有从 Exchange 分发到Queue中"); | ||
260 | + } | ||
261 | + }); | ||
262 | + | ||
263 | + // 6、发送消息,并指定 mandatory 参数为true | ||
264 | + channel.basicPublish(xmlData.getExchangeName(), xmlData.getRoutingKeyName(), true, properties, | ||
265 | + xmlData.getSendContent().getBytes()); | ||
266 | + log.info("消息生产者,目标交换机:{};路由键:{};发送信息:{}", xmlData.getExchangeName(), xmlData.getRoutingKeyName(), | ||
267 | + xmlData.getSendContent().getBytes()); | ||
268 | + // 7、添加一个异步 confirm 确认监听,用于发送消息到Broker端之后,回送消息的监听 | ||
269 | + channel.addConfirmListener(new ConfirmListener() { | ||
270 | + // 发送成功 | ||
271 | + @Override | ||
272 | + public void handleAck(long deliveryTag, boolean multiple) throws IOException | ||
273 | + { | ||
274 | + log.info("消息发送成功,标识:{};是否是批量:{}", deliveryTag, multiple); | ||
275 | + } | ||
276 | + | ||
277 | + // 发送失败 | ||
278 | + @Override | ||
279 | + public void handleNack(long deliveryTag, boolean multiple) throws IOException | ||
280 | + { | ||
281 | + log.error("消息发送失败,标识:{};是否是批量:{}", deliveryTag, multiple); | ||
282 | + } | ||
283 | + }); | ||
284 | + // finally,关闭连接 | ||
285 | + closeConnectionAndChanel(channel, connection); | ||
286 | + return ResultJson.success(CustomExceptionType.MESSAGE_SUCCESS); | ||
228 | } | 287 | } |
229 | } | 288 | } |
230 | 289 |
@@ -139,9 +139,15 @@ public class XmlUtils { | @@ -139,9 +139,15 @@ public class XmlUtils { | ||
139 | // 路由键不存在 | 139 | // 路由键不存在 |
140 | return ResultJson.error(CustomExceptionType.ROUTING_KEY_NO_EXIST); | 140 | return ResultJson.error(CustomExceptionType.ROUTING_KEY_NO_EXIST); |
141 | } | 141 | } |
142 | + | ||
142 | // 获取密码 | 143 | // 获取密码 |
143 | xmlData.setPassword(userList.get(0).getPassword()); | 144 | xmlData.setPassword(userList.get(0).getPassword()); |
144 | - ResultJson<XmlData> result = new ResultJson<>("200","通过格式与数据校验",xmlData); | 145 | + // 获取服务器ip |
146 | + xmlData.setServerPort(serverList.get(0).getServerPort()); | ||
147 | + // 获取服务器port | ||
148 | + xmlData.setServerIp(serverList.get(0).getServerIp()); | ||
149 | + ResultJson<XmlData> result = new ResultJson<>("200", "通过格式与数据校验", xmlData); | ||
150 | + | ||
145 | return result; | 151 | return result; |
146 | } | 152 | } |
147 | } | 153 | } |
-
请 注册 或 登录 后发表评论