InterviewSolution
| 1. |
What does Spring Boot offer with respect to Messaging using ConnectionFactory? |
|
Answer» Spring Boot offers a number of features with respect to Messaging. To start with let’s talk about javax.jms.ConnectionFactory. 1. Using ConnectionFactory This provides an Interface to send & receive MESSAGES. Spring uses JNDI to LOOK for the ConnectionFactory which you can configure by using the following property in APPLICATION.properties : spring.jms.jndi-name=java:/MyConnectionFactoryAlternatively, if ActiveMQ Starter is used, Spring Boot will autoconfigure the factory. You can customize the configuration using following properties : spring.activemq.broker-url=tcp://localhost:9876 spring.activemq.user=activeemq spring.activemq.password=password spring.activemq.pool.enabled=true spring.activemq.pool.max-connections=50 spring.jms.cache.session-cache-size=10How do you send & receive Messages? To send a message , you can inject org.springframework.jms.core.JmsTemplate into your application bean and use it for sending messages. For example, @COMPONENT public class MyMessageBean { private final JmsTemplate jmsTemplate; @Autowired public MyMessageBean(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } }You can convert a simple method into Message Listener by using annotation org.springframework.jms.annotation.JMSListener (destination =”myQueue”) @Component public class SomeBean { @JmsListener(destination=”somequeue”) public void processMessage(String content) { } }To override default Connection Factory, you can make use of DefaultJmsListenerContainerFactoryConfigurer as follows: @Bean public DefaultJmsListenerContainerFactory myFactory( DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); configurer.configure(factory, connectionFactory()); factory.setMessageConverter(myMessageConverter()); return factory; }Now your listener would be as follows : @Component public class SomeBean { @JmsListener(destination=”somequeue” containerFactory=”myFactory”) public void processMessage(String content) { } } |
|