JMS负载平衡(1个队列,2个队列连接工厂和1个.bindings) [英] JMS load balancing(1 Queue, 2 Queue Connection Factory and 1 .bindings)

查看:159
本文介绍了JMS负载平衡(1个队列,2个队列连接工厂和1个.bindings)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JMS将消息放入队列.

I am using JMS to put messages into the Queue.

该队列与2个QueueConnection工厂和2个队列管理器链接. 在将消息发送到队列时,我想通过2个不同的队列连接工厂将消息平均分发/发送到2个不同的队列管理器.

The Queue is linked with 2 QueueConnection Factories and 2 Queue managers. While sending the messages to the Queue I want to equally distribute/send message to 2 different Queue Managers via 2 different Queue Connection factory.

示例:

在不同的时间点, 我的服务接收到来自某个用户的消息.我需要将消息平均放入2个QCF/QueueManager中.(负载平衡)

In different point of time, My service receives messages from some user.I need to equally put the messages into 2 QCF/QueueManagers.(load balancing)

如果我收到第一条消息,则需要发送到第一台QCF1/Queue Manager,如果另一个消息到达我的服务,则必须将其发送到第二台QCF1/Queue Manager.

If I get 1st message I need to send to 1st QCF1/Queue Manager and If another msg arrives to my service I will have to send it to 2nd QCF1/Queue Manager.

因为JMS允许我一次仅使用1个QCF来创建队列联合.

Because JMS allows me to create Queue Coontion with yhe help of onle 1 QCF at a time.

这可以使用JMS完成吗?或其他实现此目的的方法?

Can this be accomplished using JMS? Or anyother way to achieve this?

使用一个QCF将消息发送到队列的方法:

import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSession;
import javax.jms.QueueSender;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Receiver
{
    @Resource(lookup = "jms/ConnectionFactory1")
    private static QueueConnectionFactory connectionFactory1;

    @Resource(lookup = "jms/ConnectionFactory2")
    private static QueueConnectionFactory connectionFactory2;

    @Resource(lookup = "jms/Queue")
    private static Queue queue;

    public void readQueueMessages() {                                                                   
        try {
            // create a queue connection
            QueueConnection queueConn = connectionFactory1.createQueueConnection();

            // create a queue session
            QueueSession queueSession = queueConn.createQueueSession(true, 0);

            // create a queue receiver
            QueueSender queueSender = queueSession.createSender(queue);

            TextMessage msg = queueSession.createTextMessage();

            // start the connection
            queueConn.start();

            msg.setText("Hi");
            queueSender.send(msg);

            queueSender.close();
            queueSession.close();
            queueConn.close();

            }
        } catch(JMSException exp) {
            // Handle this exception
        } finally {      
            if(queueConn != null) {                                                     
                // close the queue connection
                queueConn.close();
            }
        }
    }
}

推荐答案

由于您提到了负载平衡,因此最好使用IBM MQ队列管理器集群,而不是使用您自己的代码来分发消息.您将需要将队列管理器放在MQ集群中,并在QM1和QM2中定义集群队列的实例,如下所示.

Since you mention about load balancing, it would be better to use IBM MQ queue manager clustering instead of using your own code to distribute messages. You will need to put the queue managers in a MQ cluster and define an instance of cluster queue in QM1 and QM2, something like below.

当发往CLUSQ1的消息到达QM网关时,队列管理器将使用默认的循环方法将消息路由到群集中的其他队列管理器.

When a message destined to CLUSQ1 arrives on QM Gateway, the queue manager will route the message to other queue manager in the cluster using default round robin method.

阅读

Read here for more on MQ Clustering

这篇关于JMS负载平衡(1个队列,2个队列连接工厂和1个.bindings)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆