Spring boot JMS template Embedded ActiveMQ
Embedded ActiveMQ
When you deploy broker in same JVM. This will help you avoid network hop, load balancing and other network issue.
Spring boot provides you support to create embedded ActiveMQ.
Steps to configure embedded broker ActiveMQ with spring boot
1. Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
2. Properties file configuration JMS & ActiveMQ
Inside application.properties file we can provide JMS & ActiveMQ configuration.We also need to provide broker url.
spring.jms.listener.acknowledge-mode=auto
spring.jms.listener.auto-startup=true
spring.jms.listener.concurrency=5
spring.jms.listener.max-concurrency=10
spring.jms.pub-sub-domain=false
spring.jms.template.default-destination=empty
spring.jms.template.priority=100
spring.jms.template.qos-enabled=true
spring.jms.template.receive-timeout=100000000
spring.jms.template.time-to-live=36000000
spring.activemq.broker-url=vm://embedded?broker.persistent=false&broker.useShutdownHook=false
spring.activemq.close-timeout=15000
spring.activemq.in-memory=true
spring.activemq.non-blocking-redelivery=false
spring.activemq.password=
spring.activemq.user=
spring.activemq.send-timeout=0
spring.activemq.packages.trust-all=false
spring.activemq.packages.trusted=
spring.activemq.pool.block-if-full=true
spring.activemq.pool.block-if-full-timeout=-1
spring.activemq.pool.create-connection-on-startup=true
spring.activemq.pool.enabled=false
spring.activemq.pool.expiry-timeout=0
spring.activemq.pool.idle-timeout=30000
spring.activemq.pool.max-connections=1
spring.activemq.pool.maximum-active-session-per-connection=500
spring.activemq.pool.reconnect-on-exception=true
spring.activemq.pool.time-between-expiration-check=-1
spring.activemq.pool.use-anonymous-producers=true
3. Configuration class
Inside your configuration class we need to provide the bean configuration for JMS
@EnableJms
@Configuration
public class ApplicationConfiguration{
@Bean
public JmsListenerContainerFactory<?> queueListenerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setMessageConverter(messageConverter());
return factory;
}
@Bean
public MessageConverter messageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
4. Define que name as constant
We can define que name as constant in any Java class (ApplicationConstants.java)
public static final String messageQue = "message-que";
5. Message receiver class
Create a message receiver ( a JmsListener class)
@JmsListener(destination = messageQue )
public void messageReceiver(@Payload MessageData data, @Headers MessageHeaders headers, Message message,
Session session) {
//// your code will go here
}
MessageData : This will the object that you will send to message, this can be String or any other object of your choice.
destination = messageQue : This will be que name , we can choose any que name and sender will send data to the following que.
6. Message sender class
Create a message sender
Create an object of JmsTemplate and using this object we can send the desired message to que.
The implementation will be as follows :
@Service
public class MessageSender {
@Autowired
private JmsTemplate jmsTemplate;
public void sendToQue(MessageData message) {
jmsTemplate.convertAndSend(messageQue, message,m -> {
return m;
});
}
Now we are done with the configuration and you can send the data from your service class using the method sendToQue of MessageSender class.
Labels: Embedded ActiveMQ, Embedded broker, Embedded broker ActiveMQ, EmbeddedActiveMQ with Spring boot, in memory activemq, JMSTemplate, spring boot activemq, Spring boot embedded broker, spring boot jms
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home