package cn.emoney.codetest;
import android.text.TextUtils;
import android.util.Base64;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Return;
import com.rabbitmq.client.ReturnListener;
import com.rabbitmq.client.RpcClient;
import com.rabbitmq.client.RpcClientParams;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Overview: MessageQueueNew
* Type: SingleInstance
* Function: 处理消息队列的订阅和发送)
* Author: 代志鹏Roc
* Date: 2019/11/20 18:00:00
*/
public enum MessageQueueNew {
INSTANCE;//实例 (利用Java枚举实现最优单例)
private static final int NOT_PERSISTENT = 1;
private static final int PERSISTENT = 2;
//RMQ客户端,由SingleInstance保证该client全局唯一
public RpcClient mRpcClient = null;
MessageQueueNew() throws IOException {
RpcClientParams tParams = new RpcClientParams();
mRpcClient = new RpcClient(tParams);
}
public static MessageQueueNew getInstance() {
return INSTANCE;
}
/**
* 回调通知实体定义,回调标准化
*/
public static class RpcResult extends Return {
/**
* @param replyCode 错误码
* @param replyText 错误信息
* @param exchange 交换机名
* @param routingKey 路由Key
* @param properties 队列参数
* @param body 消息实体
*/
public RpcResult(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) {
super(replyCode, replyText, exchange, routingKey, properties, body);
}
}
/**
* 接口 消息回调通知
*/
public interface PublishListener {
void onResult(RpcResult rpcResult);
}
/**
* @param queueName 发送队例名
* @param strMsg 消息内容
* @param exchangeName 交换机名
* @param exchangeType 交换机类型
* @param resCallback 回调句柄
*/
public void publishMsg(final String queueName, final String strMsg, final String exchangeName, final String exchangeType, PublishListener resCallback) throws IOException {
//1. 获取通道
Channel tChannel = mRpcClient.getChannel();
//2. 设置队列参数
//2.1 获取队列
String tReplyQueueName = tChannel.queueDeclare().getQueue();
//2.2 添加交换机
if (TextUtils.isEmpty(exchangeName)) {
tChannel.exchangeDeclare(exchangeName, exchangeType, true);
tChannel.queueBind(tReplyQueueName, exchangeName, tReplyQueueName);
}
//2.3 构造基本参数builder,配置基本发送参数
AMQP.BasicProperties.Builder tPropertiesBuilder = new AMQP.BasicProperties.Builder()
.priority(1) //0最低,9最高。0-4作为一般的优先级,5-9作为加速优先级
.deliveryMode(MessageQueueNew.PERSISTENT) // NOT_PERSISTENT:非持久化, PERSISTENT:持久化
.replyTo(tReplyQueueName)
.correlationId(Guid.createId());
AMQP.BasicProperties tProperties = tPropertiesBuilder.build();
//2.4 str -> bytes,加工消息
byte[] bMsg = processMsg(strMsg);
//3. 注册订阅回调 -> 转发给调用方
tChannel.addReturnListener(new ReturnListener() {
@Override
public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
if (resCallback != null) {
RpcResult tRpcResult = new RpcResult(replyCode, replyText, exchange, routingKey, properties, body);
resCallback.onResult(tRpcResult);
}
}
});
//4. 发送消息
tChannel.basicPublish(exchangeName, queueName, tProperties, bMsg);
return;
}
/**
* @param strOriginMsg 原始消息
* @return 加工为B64的Bytes
*/
private byte[] processMsg(String strOriginMsg) {
byte[] bMsg = Base64.encodeToString(strOriginMsg.getBytes(StandardCharsets.UTF_8), Base64.NO_PADDING).getBytes(StandardCharsets.UTF_8);
return bMsg;
}
}
MessageQueueNew
最新推荐文章于 2023-05-09 15:23:47 发布