java websphere mq,java websphere MQ

这篇博客讨论了如何使用WebSphere MQ Java API在循环中发送消息到队列,并确保在出现异常时能回滚所有消息,正常运行则提交所有消息。通过设置MQGMO_SYNCPOINT选项实现本地事务,当准备提交时调用MQQManager.commit()或MQQManager.backout()。同时,提到了WMQ支持本地和全局(XA)工作单元,以及使用最新WMQ Java客户端的建议。
摘要由CSDN通过智能技术生成

My aim is to put n number of messages in a for loop to a WebSphere MQ queue using WebSphere MQ java programming.

My java program will run as a standalone program.

If any exception in between , I need to rollback all the messages.

If no exception then I should commit all the messages .

The outside world should not see my messages in the queue until I complete fully.

How do I achieve this?

Updated with sample code as per reply from T.Rob:

Please check if sample code is fine ?

Does setting MQGMO_SYNCPOINT is only related to my program's invocation ?

(because similar programs running parallely will also be putting messages on the same queue and those messages should not gett affected by my program's SYNCPOINT.)

public void sendMsg() {

MQQueue queue = null;

MQQueueManager queueManager = null;

MQMessage mqMessage = null;

MQPutMessageOptions pmo = null;

System.out.println("Entering..");

try {

MQEnvironment.hostname = "x.x.x.x";

MQEnvironment.channel = "xxx.SVRCONN";

MQEnvironment.port = 9999;

queueManager = new MQQueueManager("XXXQMANAGER");

int openOptions = MQConstants.MQOO_OUTPUT;

queue = queueManager.accessQueue("XXX_QUEUENAME", openOptions, null, null, null);

pmo = new MQPutMessageOptions();

pmo.options = CMQC.MQGMO_SYNCPOINT;

String input = "testing";

System.out.println("sending messages....");

for (int i = 0; i < 10; i++) {

input = input + ": " + i;

mqMessage = new MQMessage();

mqMessage.writeString(input);

System.out.println("Putting message: " + i);

queue.put(mqMessage, pmo);

}

queueManager.commit();

System.out.println("Exiting..");

} catch (Exception e) {

e.printStackTrace();

try {

System.out.println("rolling back messages");

if (queueManager != null)

queueManager.backout();

} catch (MQException e1) {

e1.printStackTrace();

}

} finally {

try {

if (queue != null)

queue.close();

if (queueManager != null)

queueManager.close();

} catch (MQException e) {

e.printStackTrace();

}

}

}

解决方案

WMQ supports both local and global (XA) units of work. The local units of work are available simply by specifying the option. Global XA transactions require a transaction manager, as mentioned by keithkreissl in another answer.

For what you described, a POJO doing messaging under syncpoint, specify MQC.MQGMO_SYNCPOINT in your MQGetMessageOptions. When you are ready to commit, issue the MQQManager.commit() or MQQManager.backout() call.

Note that the response and doc provided by ggrandes refers to the JMS and not Java classes. The Java classes use Java equivalents of the WMQ procedural API, can support many threads (doc) and even provide connection pooling (doc). Please refer to the Java documentation rather than the JMS documentation for the correct behavior. Also, I've linked to the WMQ V7.5 documentation which goes with the latest WMQ Java V7.5 client. The later clients have a lot more local functionality (tracing, flexible install path, MQClient.ini, etc.) and work with back-level QMgrs. It is highly recommended to be using the latest client and the download is free.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值