简体   繁体   English

JMS负载平衡(1个队列,2个队列连接工厂和1个.bindings)

[英]JMS load balancing(1 Queue, 2 Queue Connection Factory and 1 .bindings)

I am using JMS to put messages into the Queue. 我正在使用JMS将消息放入队列。

The Queue is linked with 2 QueueConnection Factories and 2 Queue managers. 队列与2个QueueConnection工厂和2个队列管理器链接。 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个不同的队列连接工厂将消息平均分发/发送到2个不同的队列管理器。

Example: 例:

In different point of time, My service receives messages from some user.I need to equally put the messages into 2 QCF/QueueManagers. 在不同的时间点,我的服务从某个用户接收消息。我需要将消息平均放入2个QCF / QueueManager中。 (load balancing) (负载均衡)

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. 如果收到第一条消息,则需要发送到第一台QCF1 / Queue Manager,如果另一个消息到达我的服务,则必须将其发送到第二台QCF1 / Queue Manager。

Because JMS allows me to create Queue Coontion with yhe help of onle 1 QCF at a time. 因为JMS允许我一次仅使用1个QCF来创建Queue Coontion。

Can this be accomplished using JMS? 可以使用JMS完成吗? Or anyother way to achieve this? 还是通过其他方式实现这一目标?

Approach to send message to Queue using one QCF: 使用一个QCF将消息发送到Queue的方法:

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

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. 既然您提到了负载平衡,那么最好使用IBM MQ队列管理器集群,而不是使用您自己的代码来分发消息。 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. 您将需要将队列管理器放在MQ集群中,并在QM1和QM2中定义集群队列的实例,如下所示。

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. 当发往CLUSQ1的消息到达QM网关时,队列管理器将使用默认的轮询方法将消息路由到群集中的其他队列管理器。

Read here for more on MQ Clustering 在此处阅读有关MQ集群的更多信息

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM