|
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
|
|