Marcel
Marcel

Reputation: 421

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?

Upvotes: 1

Views: 25135

Answers (3)

JitenS
JitenS

Reputation: 51

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

Upvotes: 1

Attila Repasi
Attila Repasi

Reputation: 1830

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.

Upvotes: 2

MarkA
MarkA

Reputation: 1226

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 !

Upvotes: -1

Related Questions