连接两个mq服务

有个需求需要连接两个mq
研究了半天发现spring mq 的bean是唯一的,配置不了两个
单个mq的配置方法见另一篇博客
https://blog.csdn.net/zhaojian0910/article/details/83088422
最终的方案是摆脱spring,自己来管理mq的container
代码如下
先创建一个自己的mq连接工厂

package com.utan.gateway.mq;

import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;

public class JmsConnectionFactory {
    private static MQQueueConnectionFactory mqQueueConnectionFactory(String host, int port, String channel, String queueManager, int ccsid) {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        mqQueueConnectionFactory.setHostName(host);
        try {
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setCCSID(ccsid);
            mqQueueConnectionFactory.setChannel(channel);
            mqQueueConnectionFactory.setPort(port);
            mqQueueConnectionFactory.setQueueManager(queueManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqQueueConnectionFactory;
    }

    private static UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(String username, String password, String host, int port, String channel, String queueManager, int ccsid) {
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
        userCredentialsConnectionFactoryAdapter.setUsername(username);
        userCredentialsConnectionFactoryAdapter.setPassword(password);
        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory(host, port, channel, queueManager, ccsid));
        return userCredentialsConnectionFactoryAdapter;
    }

    public static CachingConnectionFactory cachingConnectionFactory(String username, String password, String host, int port, String channel, String queueManager, int ccsid) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter(username, password, host, port, channel, queueManager, ccsid));
        cachingConnectionFactory.setSessionCacheSize(500);
        cachingConnectionFactory.setReconnectOnException(true);
        return cachingConnectionFactory;
    }
}

然后自行启动mq容器监听

		// 查询出所有的pserver
        final List<MtMessageMain> servers = mtMessageTemplateService.getAllServers();

        for(final MtMessageMain mmm : servers) {
                mqServerInit(mmm)
        }
public void mqServerInit(MtMessageMain mmm) throws IOException {
        final String mqName = mmm.getMqName();

        // 根据mq的name,找到mq的参数对象
        final Map<String, Object> mqObj = parameterResolver.resolve("FlowDemo", "suibianwan", mqName,"mq");

        // 取出本地队列(从本地队列接收消息)
        final List<Map> localQueueList = (List<Map>) mqObj.get("localQueue");

        DefaultMessageListenerContainer container;

        for(int j = 0 ; j < localQueueList.size() ; j++){
            final Map<String, Object> remoteQueue = localQueueList.get(j);
            String destinationName = (String) remoteQueue.get("name");
            container = new DefaultMessageListenerContainer();

            String ip = (String) mqObj.get("ip");
            int port = (int) mqObj.get("port");
            String channel = (String) mqObj.get("channel");
            String qm = (String) mqObj.get("qm");
            int ccsid = Integer.parseInt((String) mqObj.get("ccsid"));

            container.setConnectionFactory(JmsConnectionFactory.cachingConnectionFactory("mqm", "mqm", ip, port, channel, qm, ccsid));
            container.setMessageListener(new ReceiveMessage(destinationName, mmm, mtMessageTemplateService));
            container.setDestinationName(destinationName);

            mqMap.put("id_" + mqName + "_" + (j+1), container);

			// 一定要初始化之后再启动
            container.initialize();
            container.start();

            log.info("Mq server {} {} {} {} {}", ip, port, channel, qm, ccsid);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值