简体   繁体   中英

Put and Get Messages to IBM MQ from java code

I'm learning IBM MQ. I need to put and get messages to IBM MQ from java code using the best practices.

I did this question but I don't know if it's the best way: How to put and get IBM MQ messages without application server

Could you give me some tip about that, please?

IBM provides sample code with the MQ install, you should look into those.

There are samples for using MQ classes for Java and JMS as well. The source for these samples are located under "MQ install dir"\Tools on Windows.

public static void main(String[] args) {

Connection connection = null;
Session session = null;
Destination destination = null;
Destination tempDestination = null;
MessageProducer producer = null;
MessageConsumer consumer = null;

try {

  JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
  JmsConnectionFactory cf = ff.createConnectionFactory();

  cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost"); 
  cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
  cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN");
  cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
  cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM1");

  connection = cf.createConnection();
  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  destination = session.createQueue("queue:///Q1");
  producer = session.createProducer(destination);

  long uniqueNumber = System.currentTimeMillis() % 1000;
  TextMessage message = session.createTextMessage("SimpleRequestor: Your lucky number yesterday was " + uniqueNumber);
  connection.start();
  producer.send(message);

  }catch (JMSException jmsex) {
 jmsex.printStackTrace();
}
}

To me this seems like a duplicate of the question you asked half an hour previously. You seem quite desperate to get an answer so lets see if this helps move you forward:

The basic principles of message queue handling are the same regardless of the implementation. Given that is the case, and given that you are learning, I think you would benefit from looking at this RabbitMQ tutorial: RabbitMQ tutorial

RabbitMQ is free and easy to install on your local machine, so you can have a play with it, and understand it more easily. The tutorial is suitable for a newbie, with a good explanation throughout.

This should give you a good idea of the approaches used, and the best practice.

My understanding is that RabbitMQ is also much more widely used than IBM MQ, so you would be able to get much more support when getting to grips with it.

Once you have learnt the techniques for RabbitMQ, I would hope you can apply them to IBM MQ, which should enable you to answer your own original question.

Good Luck !

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