如何将jms消息从托管应用程序的一台服务器发送到托管另一个应用程序的另一台服务器,两者都托管在Websphere服务器中

时间:2019-03-24 23:09:00

标签: jms ejb ibm-mq

我了解两个消息队列(接收方和响应方)是否都在同一服务器位置中,我可以使用JNDI连接工厂和队列名称 jms / myqueue_qcf1 jms / myqueue1,以连接到队列并将消息发送到jms / myqueue_qcf2,jms / myqueue2

但是在服务器间连接的情况下,是否相同 当防火墙黑白两台服务器都打开时。 MQ myqueue2在Websphere中设置为远程mq。

任何有关代码引用的帮助都是可以的。

public void myAppListener extends MessageListener{ //getting message from MQ1 - 
                                 //sent by some other application - MQ1 is Local 
                                 //in appServer1  

private static final String JMS_LC_JNDI_NAME = "jms/liftcargo_lara_qcf";
private static final String JMS_LC_QUEUE_NAME = "jms/APP.OUT.REQUEST";

public void onMessage(Message msg){
     try{
       TextMessage requestMessage = (TextMessage) msg;
       String reqMessage = requestMessage.getText();
       String correlationId  = requestMessage.getJMSCorrelationID();

       sendXMLToNextAppMQ(reqMessage , correlationId)
     }
   }

   public static void sendXMLToNextAppMQ(String message, String correlID) throws JMSException { //The MQ to which the message is forwarded to is a Remote MQ, in different server appServer2

        try {
            InitialContext context = new InitialContext();


            QueueConnectionFactory queueConnectionFactory =
                    (QueueConnectionFactory)context.lookup(JMS_LC_JNDI_NAME);
            System.out.println("Connection Factory data :: "+queueConnectionFactory.toString());

            Queue queue = (Queue) context.lookup(JMS_LC_QUEUE_NAME);
            System.out.println("Check Queue Name :: "+queue.toString());

            QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

            QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            QueueSender queueSender = session.createSender(queue);

            TextMessage message1 = session.createTextMessage();
            message1.setText(message);
            message1.setJMSType("Tunnel message from CCAM.LARA.OUT.REQUEST MQ to 
                                           LIFTCARGO.OUT.LARA.REQUEST MQ");
            message1.setJMSDestination(queue);
            message1.setJMSCorrelationID(correlID);
            queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            queueSender.send(message1);



        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (JMSException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
}
}

In Method sendXMLToNextAppMQ (i.e., tunnel the msg recieved in MQ1 in appServer1 to MQ2 in appServer2) is there any other jndi properties needed to mention to connect appServer1 to MQ2 in appServer2 (firewall is opened b/w appServer1 & appServer2)

1 个答案:

答案 0 :(得分:0)

如果目标请求队列在其他服务器上,则您的应用程序仍将具有相同的代码,但是您提供的名称将不是您所连接的队列管理器上的QLOCAL的名称,而是一个QREMOTE。如果您使用JNDI来引用队列,那么甚至不必更改队列名称,只需更改引用的实际MQ队列名称即可。

例如,如果您使用命令行JMSAdmin工具,则仅在QUEUE属性中更改名称:-

DEFINE Q(myqueue2) QUEUE(Q.FOR.REQUESTS)

然后在您的两个队列管理器上,将出现如下所示的定义:-

本地质量管理(QM1)

DEFINE QREMOTE(Q.FOR.REQUESTS) RNAME(WORK.QUEUE) RQMNAME(QM2) XMITQ(QM2)
DEFINE QLOCAL(QM2) USAGE(XMITQ) DESCR('Transmission queue for messages to QM2')
DEFINE QLOCAL(MY.REPLY.Q) DESCR('My application queue for responses')
DEFINE CHANNEL(TO.QM2) CHLTYPE(SDR) CONNAME('(qm2.machine.com(1414)') XMITQ(QM2)
DEFINE CHANNEL(TO.QM1) CHLTYPE(RCVR)

远程质量管理(QM2)

DEFINE QLOCAL(WORK.QUEUE)
DEFINE QLOCAL(QM1) USAGE(XMITQ) DESCR('Transmission queue for messages to QM1')
DEFINE CHANNEL(TO.QM2) CHLTYPE(RCVR)
DEFINE CHANNEL(TO.QM1) CHLTYPE(SDR) CONNAME('qm1.machine.com(1414)') XMITQ(QM1)

此外,在请求/回复应用程序中,最好的做法是提供应在其中将响应作为请求消息的一部分发送到的队列的名称,并对响应的应用程序进行编码以读取这些字段(ReplyTo字段) )并使用它们将回复消息发送回去,因此不需要在远程QM(在我的示例中为QM2)上额外的QREMOTE定义。

相关问题