JBoss series: A misunderstanding when using JMS session

Source: Internet
Author: User
Tags jbpm

Overview

We usually use the JMS session to connect to the JMS message queue, create a producer to send messages to the queue, or create a consumer to consume messages from the queue, BaseJMSTaskServer. the start () method in java is an example. Pay attention to BaseJMSTaskServer. java uses the following JMS-related APIs:

import javax.jms.MessageConsumer;import javax.jms.Queue;import javax.jms.QueueConnection;import javax.jms.QueueConnectionFactory;import javax.jms.QueueSession;import javax.jms.Session;

According to the design of the JMS standard, these APIs are designed to be used by a single thread. We must create and use them in a single thread and close them. The misunderstanding is that many people ignore this point in their work and create a JMS session to be used concurrently by multiple threads. This usage is incorrect. I will first pick out the relevant definitions in JMS specifications as follows. Here are two typical examples and how to avoid misunderstandings.

JMS specifications

JMS session objects are single threaded and can only be used by single thread that created them. An attempt to use them from another thread wocould result in an exception or some very strange errors.

Typical Example 1

JBPM provides interaction with Task Service through JMS (a deep understanding of jBPM Human Task-execute Human tasks using JMS as a transmission medium), JMSTaskClientConnector. java is used by third-party programs. Many people design the jmstaskclientconneinstance as the singleton mode, and jmstaskclientconneis used concurrently in multiple threads. An error occurs, causing jBPM to not work properly.

Example 2

Spring provides spring-JMS encapsulation for jms. Many of them have misunderstandings about the use of JmsTemplate. Common Errors are as follows:

for(;;){....getJmsExecuteTemplate().sendQueueString("XXXX");...}

This also causes concurrent use of the JMS session and application exceptions.

Avoid mistakes

Javax. jms. the ServerSessionPool interface is provided for the JEE container. The JEE container implements this interface. This interface takes into account the concurrent and multi-threaded scenario. For example, the ServerSessionPool implementation is used to deploy the message-driven Bean to the JEE container, it takes into account thread security and so on.
Therefore, it is easy to avoid mistakes. You can submit your concurrent requests to the JEE container for processing, or implement javax. jms. ServerSessionPool by yourself to complete thread security and other considerations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.