In my previous post I demonstrated the publisher code to create and send a text message over a weblogic JMS queue.
In this post, we will be seeing the subscriber code required to receive the message sent by the sender. For this, We will create a spring starter project and the project structure should be like :
In my previous post I explained the steps to create a Spring starter project with required JMS libs.
Let's create the required classes stey by step.
1. We will be creating a configuration class.
The configuration class is similar to what we saw for the Producer Application. So I will not be dissecting this class.
2. Next, we will create a spring bean
The
If you have any questions regarding the setup or any issues during implementation, please comment down below.
Thanks.
In this post, we will be seeing the subscriber code required to receive the message sent by the sender. For this, We will create a spring starter project and the project structure should be like :
In my previous post I explained the steps to create a Spring starter project with required JMS libs.
Let's create the required classes stey by step.
1. We will be creating a configuration class.
JMSConfig
@Configuration @EnableJms public class JMSConfig { @Bean public JndiTemplate createJndiTemplate() { JndiTemplate jndi = new JndiTemplate(); Properties jndiProperties = new Properties(); jndiProperties.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory"); jndiProperties.setProperty("java.naming.provider.url","t3://localhost:7001"); jndi.setEnvironment(jndiProperties); return jndi; } @Bean(name="CF") public JndiObjectFactoryBean createJndiObjectFactoryBean(@Autowired final JndiTemplate jndiTemplate) { JndiObjectFactoryBean factory = new JndiObjectFactoryBean(); factory.setJndiTemplate(jndiTemplate); factory.setJndiName("/com/jms/dev/cf"); factory.setProxyInterface(ConnectionFactory.class); return factory; } @Bean(name="Queue") public JndiObjectFactoryBean createQueue(@Autowired final JndiTemplate jndiTemplate) { JndiObjectFactoryBean factory = new JndiObjectFactoryBean(); factory.setJndiTemplate(jndiTemplate); factory.setJndiName("/com/jms/dev/distqueue"); factory.setProxyInterface(Destination.class); return factory; } @Bean public JndiDestinationResolver createJndiDestinationResolver(@Autowired final JndiTemplate jndiTemplate) { JndiDestinationResolver destResolver = new JndiDestinationResolver(); destResolver.setJndiTemplate(jndiTemplate); destResolver.setCache(true); return destResolver; } @Bean public JmsTemplate createJmsTemplate(@Autowired final ConnectionFactory connectionFactory, @Autowired final DestinationResolver destinationResolver) { JmsTemplate template = new JmsTemplate(); template.setConnectionFactory(connectionFactory); template.setDestinationResolver(destinationResolver); return template; } @Bean public JMSMessageReceiver createReceiver(@Autowired final JmsTemplate template) { JMSMessageReceiver receiver = new JMSMessageReceiver(); receiver.setTemplate(template); return receiver; } @Bean public DefaultMessageListenerContainer createListener(@Autowired final JMSMessageReceiver mylistener, @Autowired final JndiTemplate jndiTemplate, @Autowired ConnectionFactory conFactory, @Autowired Destination destination) { DefaultMessageListenerContainer listener = new DefaultMessageListenerContainer(); listener.setConcurrentConsumers(15); listener.setMessageListener(mylistener); listener.setDestination(destination); listener.setConnectionFactory(conFactory); return listener; } }
The configuration class is similar to what we saw for the Producer Application. So I will not be dissecting this class.
2. Next, we will create a spring bean
JMSMessageReceiver
to receive the messages sent by the producerpublic class JMSMessageReceiver { @Autowired private JmsTemplate template; @Value("/com/jms/dev/distqueue") private String destination; private static final Logger LOGGER = LoggerFactory.getLogger(JMSMessageReceiver.class); public void receiveMessage() throws JMSException { String message = (String)template.receiveAndConvert(destination); Message msg = template.receive(destination); LOGGER.info("\n\n\n======Received Message : "+message+" at : "+LocalDateTime.now()); LOGGER.info("======Correlation Id ======"+msg.getJMSCorrelationID()+"\n\n\n"); } public void setTemplate(JmsTemplate template) { this.template = template; }}
The
JMSMessageReceiver
bean has jmsTemplate and destination instances autowired from JmsConfig
class, the receiveMessage() method listens for the message on the queue "com/jms/dev/distqueue", the template.receive() method is a blocking operation and waits till the receivetimeout has been burst. Once we receive the message we are just logging it.If you have any questions regarding the setup or any issues during implementation, please comment down below.
Thanks.
No comments:
Post a Comment