MessageQueueNew

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值