简体   繁体   English

Websphere MQ消息无限过程

[英]Websphere MQ Message Infinite Process

I am new to Message Queue. 我是Message Queue的新手。 I am attemting to get messages from MQ Queue by using below code. 我正在尝试使用下面的代码从MQ Queue获取消息。

I am creating one connection and getting every message from the queue using that connection. 我正在创建一个连接并使用该连接从队列中获取每条消息。 Is it right way of doing like this and whether i need to commit the connection. 这样做是否正确,是否需要提交连接。

Infinite for-loop is right way to receive messages all the times from the queue, is it right? 无限for循环是从队列中一直接收消息的正确方法,是不是? Kindly advice me. 请建议我。

try {
    createMQConnection(); // getting mq connection
    createMQSession(); // getting mq session
    createMQDestination(); // getting mq destination

    for ( ; ; ) { // infinite loop to receive message from Queue
        consumer = session.createConsumer(mqQueue);
        jmsTextMessage = (JMSTextMessage) consumer.receive(100);
        // Calling application method to process the requested message from queue
    }
} catch (Exception e) {
    throw e;
} finally {
    // closing consumer
    // closing session
    // closing connection
}

Is it right way of doing like this 是这样做的正确方法

Define "right." 定义“正确”。 Depending on the business requirement, this could be right or it could be horrible. 根据业务需求,这可能是正确的,也可能是可怕的。 For example, with a 100ms wait time and a depth of zero messages the code will time out, throw a 2033 error (MQRC_NO_MSG_AVAILABLE), close the session, and exit. 例如,如果等待时间为100毫秒,消息深度为零,则代码将超时,抛出2033错误(MQRC_NO_MSG_AVAILABLE),关闭会话并退出。 Is that what you want? 那是你要的吗?

Generally there is a try / catch block enclosing the GET and that handles transient errors like RC=2033 if the intent is that the program remains running even when the queue is empty. 通常,有一个try / catch块包含GET并处理瞬态错误,如RC=2033如果意图是程序保持运行,即使队列为空。 In that case though, it's customary to set the timeout to 10 seconds or so. 但在这种情况下,通常将超时设置为10秒左右。 With 100ms timeout, the app as written would absolutely hammer the listener if it were modified to stay running. 如果被修改为保持运行,那么写入的应用程序将完全敲响监听器。

Also, the exception handling does not show any code for printing the linked exception. 此外,异常处理不显示用于打印链接的异常的任何代码。 JMS exceptions are multi-level data constructs in which the transport provider's native error code is in the linked part of the exception. JMS异常是多级数据结构,其中传输提供程序的本机错误代码位于异常的链接部分中。 If the error handling doesn't look at the linked exception it cannot even tell the difference between MQRC=2033 (no messages) versus MQRC=2035 (authorization error). 如果错误处理没有查看链接的异常,它甚至无法区分MQRC=2033 (无消息)与MQRC=2035 (授权错误)之间的区别。 One of these is transient and should be survived by the program, the other is always fatal. 其中一个是短暂的,应该由程序存活,另一个总是致命的。 At the very least, the code should either print the linked exception or else print a message stating no linked exception was found. 至少,代码应该打印链接的异常,或者打印一条消息,说明没有找到链接的异常。

So with regard to the loop and processing design, it's not possible to answer for values of "right" without knowing requirements. 因此,关于循环和处理设计,在不知道要求的情况下,不可能回答“正确”的值。 With regard to exception handling, definitely not right as no linked exception processing is present. 关于异常处理,绝对正确,因为没有链接的异常处理。

...and whether I need to commit the connection. ......以及我是否需要提交连接。

Depends. 要看。 Is it OK to lose or duplicate messages? 丢失或复制邮件是否可以? If so then transactions are not needed. 如果是,则不需要交易。 Use of transacted sessions protects against loss of messages but not dupes. 使用事务处理会话可以防止丢失消息,但不会丢失欺骗。 Use of XA 2-Phase Commit protects against both loss of messages and against dupes. 使用XA 2阶段提交可以防止丢失消息和防止丢失。 The idea is to pick the class of service (generally referred to as "At most once," "At least once," or "Once and only once") that meets the business requirement and code accordingly. 我们的想法是选择满足业务需求和代码的服务类别(通常称为“最多一次”,“至少一次”或“一次且仅一次”)。

Infinite for-loop is right way to receive messages all the times from the queue, is it right? 无限for循环是从队列中一直接收消息的正确方法,是不是?

That's one way to do it. 这是一种方法。 For high availability and high throughput, typically there are two or more instances of the application listening on the same queue. 对于高可用性和高吞吐量,通常有两个或更多应用程序实例侦听同一队列。 That way if one app server instance goes down (whether planned or unplanned) the other instance(s) continues to serve the queue. 这样,如果一个应用服务器实例发生故障(无论是计划的还是未计划的),其他实例将继续为队列服务。 In general all of these instances listen on the queue with about a 10-second timeout. 通常,所有这些实例都会在队列上侦听大约10秒的超时。

Also customary is that the GET on the queue specifies MQGMO_FAIL_IF_QUIESCING which allows the QMgr to interrupt the application when the MQ Admin tries to shut the QMgr down. 通常还有队列中的GET指定MQGMO_FAIL_IF_QUIESCING ,它允许QMgr在MQ Admin尝试关闭QMgr时中断应用程序。 If this option is not specified, the only to shut the QMgr down is to ask it to forcibly break the outstanding connections and this should only be done as a last resort. 如果未指定此选项,则仅关闭QMgr是要求它强制中断未完成的连接,这应该只作为最后的手段来完成。

It's also possible to cause MQ to trigger the app to start when a message arrives on the queue. 当消息到达队列时,也可以使MQ触发应用程序启动。 That's not typically done when the app runs on a JEE server but is very useful for stand=-alone apps. 当应用程序在JEE服务器上运行时,通常不会这样做,但对于stand = -alone应用程序非常有用。

I would go for a multi-threaded application where each thread does the following: 我会选择一个多线程应用程序,其中每个线程执行以下操作:

1) Connects to queue manager. 1)连接到队列管理器。

2) Create a consumer for a queue. 2)为队列创建使用者。

3) Setup a message listener to receive messages asynchronously. 3)设置消息监听器以异步方式接收消息。 If this does not suit then go for synchronous message receive using the receive() method. 如果这不适合,则使用receive()方法进行同步消息接收。

4) Do clean up when message consumption is done. 4)消息消费完成后进行清理。

Advantage of this: Threads receiving messages from their respective queue and are not blocked for any reason. 优点:线程从各自的队列接收消息,不会因任何原因被阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM