I'm building a REST API application with Spring Boot 2.1.6. I want to use JMS messaging in my app with Active MQ package (org.apache.activemq
). I have MyEventController
class which receives all kinds of events through http requests. I then want to send the information about the events as a JMS message to a topic so that the message consumer will update the database with the information from the events.
The reason I want to use JMS here is to not hold the Spring thread which handle http request and have the consumer open a separate thread to do potentially a lot of time consuming updates to the database. However I'm wondering if JMSTemplate
stays always one thread. Because if a new thread is opened for each http request then the solution is not so scalable.
This is my code for producer:
@RestController
public class MyEventController {
@Autowired
private DBHandler db;
@Autowired
private JmsTemplate jmsTemplate;
@RequestMapping(method=GET, path=trackingEventPath)
public ResponseEntity<Object> handleTrackingEvent(
@RequestParam(name = Routes.pubId) String pubId,
@RequestParam(name = Routes.event) String event) {
jmsTemplate.convertAndSend("topic1", "info@example.com");
return new ResponseEntity<>(null, new HttpHeaders(), HttpStatus.OK);
}
consumer:
@Component
public class JSMListener {
@JmsListener(destination = "topic1", containerFactory = "topicListenerFactory")
public void receiveTopicMessage(String event) {
// do something...
}
}