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.