Hi I was wondering about thread safety in spring boot applications.
I have configured JMS listener that reads messages concurrently and then I call a mapper to convert the request into a desirable format and then send message.
So, Coming to mapper now that I am listening messages concurrently do I need to ensure thread safety in my code?
CodePudding user response:
Application servers use several concepts to make multithreading easy for the average programmer.
The most important concept is thead confinement. To understand the concept, read the answers to this question.
In your situation, there is more than one thread involved, but there is also some API that enables you to handle data from one thread to the other in a safe way. Most of the time there are thread pools envolved, see Introduction to Thread Pools in Java.
In JMS there are some objects that are not allowed to be used in different threads (e.g. the Session object), some are (e.g. the Connection, the JmsTemplate). Read the answers to "Why JMS Session Object is not thread safe?". The consequences are listed here.
I recommend reading the Spring documentation on this topic.