Thursday, August 6, 2015

[Java] IBM Websphere MQ Client or Sender

Below is a sample code to connect to IBM Websphere MQ Server.


MQQueueConnection connection = null;
String msg = "Simple message";

MQQueueConnectionFactory cf = new MQQueueConnectionFactory();

cf.setHostName("LOCALHOST");
cf.setPort(1414);
// MQ manager
cf.setQueueManager("MYMQMGR"); 
// Use this to connect to MQ server
cf.setChannel("SYSTEM.DEF.SVRCONN"); 

cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE,   

                         WMQConstants.WMQ_CM_CLIENT);

//User is normally the profile use to login to the machine.

connection = (MQQueueConnection) cf.createQueueConnection(<user>,<password>);

String queueName = "queue://MY.LOCAL.Q";



MQQueueSession session = (MQQueueSession)   
     connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

// Choose the queue


MQQueue queue = (MQQueue) session.createQueue(queueName);

// Create the sender
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
// Set the priority
sender.setPriority(9);
TextMessage message = (TextMessage) session.createTextMessage();
message = (TextMessage) session.createTextMessage(msg);

// Start the connection

connection.start();
sender.send(message);
LOG.info("Message :\n" + message.getText());
sender.close();
session.close();
connection.close();

LOG.info("Message Sent OK.\n");

No comments:

Post a Comment