本文介绍了java websphere MQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用WebSphere MQ java编程将n个消息放入for循环中到WebSphere MQ队列。

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

我的java程序将作为独立运行程序。

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?

根据T.Rob的回复更新示例代码:

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

请检查示例代码是否正常?

Please check if sample code is fine ?

设置MQGMO_SYNCPOINT是否仅与我的程序调用有关?
(因为并行运行的类似程序也会将消息放在同一个队列中,这些消息不会受到程序的SYNCPOINT的影响。)

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支持本地和全球(XA)工作单元。只需指定选项即可使用本地工作单元。全局XA事务需要事务管理器,如keithkreissl在另一个答案中所述。

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.

对于您所描述的,POJO在同步点下进行消息传递,请指定 /WMQJavaClasses/com/ibm/mq/MQGetMessageOptions.html> MQGetMessageOptions 。当您准备提交时,请发出或调用。

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.

请注意,ggrandes提供的响应和文档是指JMS而不是Java类。 Java类使用Java等价的WMQ过程API,可以支持许多线程()甚至提供连接池()。有关正确的行为,请参阅Java文档而不是JMS文档。另外,我已经链接到WMQ V7.5文档,该文档与最新的配合使用。后来的客户端具有更多本地功能(跟踪,灵活的安装路径,MQClient.ini等),并使用后端QMgrs。强烈建议使用最新的客户端,下载是免费的。

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.

这篇关于java websphere MQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:27