简体   繁体   中英

Send message from MQ to EMS(JMS) queue

I want to send messages in MQ queue to EMS queues on different server. I am not sure how can I do this using Java. How can i assure that no message is lost while sending the message.

I am able to consume messages from MQ using java.

try {
    // Create a connection factory
    JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
    JmsConnectionFactory cf = ff.createConnectionFactory();

    // Set the properties
    cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, HOST);
    cf.setIntProperty(WMQConstants.WMQ_PORT, PORT);
    cf.setStringProperty(WMQConstants.WMQ_CHANNEL, CHANNEL);
    cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
    cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, QMGR);
    cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JmsPutGet (JMS)");
    cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
    cf.setStringProperty(WMQConstants.USERID, APP_USER);
    cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);

    // Create JMS objects
    context = cf.createContext();
    destination = context.createQueue("queue:///" + QUEUE_NAME);

    long uniqueNumber = System.currentTimeMillis() % 1000;
    TextMessage message = context.createTextMessage("Your lucky number today is " + uniqueNumber);

    producer = context.createProducer();
    producer.send(destination, message);
    System.out.println("Sent message:\n" + message);

    consumer = context.createConsumer(destination); // autoclosable
    String receivedMessage = consumer.receiveBody(String.class, 15000); // in ms or 15 seconds

    System.out.println("\nReceived message:\n" + receivedMessage);

    recordSuccess();
} catch (JMSException jmsex) {
    recordFailure(jmsex);
}

System.exit(status);

Your code is almost correct, but if you want to switch JMS providers in your program you must also switch the JMS ConnectionFactory. This is the central (provider-specific) class to create consumers and messages from.

So when creating the context, producer and message you need to use TIBCO's com.tibco.tibjms.naming.TibjmsInitialContextFactory , for example like this:

try {

  // 1) Create a MQ connection factory
  JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
  JmsConnectionFactory cf = ff.createConnectionFactory();

  // 2) Set the properties
  cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, HOST);
  cf.setIntProperty(WMQConstants.WMQ_PORT, PORT);
  cf.setStringProperty(WMQConstants.WMQ_CHANNEL, CHANNEL);
  cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
  cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, QMGR);
  cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JmsPutGet (JMS)");
  cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
  cf.setStringProperty(WMQConstants.USERID, APP_USER);
  cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);

  // 3) Create MQ consumer
  JMSContext mqContext = cf.createContext();
  destination = mqContext.createQueue("queue:///" + QUEUE_NAME);
  consumer = mqContext.createConsumer(destination); // autoclosable

  // 4)  wait for message from MQ (or null)
  String receivedText = mqContext.receiveBody(String.class, 15000); // in ms or 15 seconds
  System.out.println("\nReceived message:\n" + receivedText);

  // 5) Create TIBCO EMS ConnectionFactory and an EMS MessageProducer
  TibjmsConnectionFactory emsCF = new com.tibco.tibjms.TibjmsConnectionFactory( "tcp://1.2.3.4:7222");
  Connection emsConnection = emsCF.createConnection(user, password);
  Session emsSession = emsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  MessageProducer emsProducer = emsSession.createProducer( emsSession.createQueue(QUEUE_NAME) );

  // 6) Create EMS TextMessage from MQ TextMessage
  TextMessage emsMsg = emsSession.createTextMessage( receivedText );

  // 7) publish to EMS
  emsProducer.send(emsMsg);

  // 8) cleanup
  emsConnection.close();

  recordSuccess();
} catch (JMSException jmsex) {
   recordFailure(jmsex);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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