I found similar question, but I dont know how to apply the answer to my program. I dont understand how to open session and send all messages in one transaction.
For now I call JmsTemplate.convertAndSend("queue", "message")
in a for loop.
There are samples shipped with IBM MQ. Please refer them as starting point. The following example is modified from SimplePTP.java sample to send messages in a loop and then commit. There is createSession call with first parameter as true
to start a transaction and there is session.commit
to commit the transaction after all messages are sent.
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, "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");
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "SimplePTP (JMS)");
// Create JMS objects
connection = cf.createConnection();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("queue:///Q1");
producer = session.createProducer(destination);
// Start the connection
connection.start();
for (int i=0; i < 10; i++) {
long uniqueNumber = System.currentTimeMillis() % 1000;
TextMessage message = session.createTextMessage("SimplePTP: Your lucky number today is " + uniqueNumber);
// And, send the message
producer.send(message);
System.out.println("Sent message:\n" + message);
}
session.commit();
}
catch (JMSException jmsex) {
}