As part of this example I will be creating a JMS queue using spring JMS API, which will be leveraging the JMS capability from the Oracle Weblogic Server. In this post I will be demonstrating only the publisher code which will create a text message and publish it on to a JMS queue. I will be using eclipse as my IDE with below versions for libraries.
1. spring boot - 1.5.10.RELEASE
2. weblogic-t3thinclient ( mvn install this jar locally from WL_HOME)
Firstly, we will create a Spring boot starter Project with JMS checkbox checked. This option will include all the required jars.
Follow along the below steps to create a Spring boot project in eclipse with the required classes.
1. Create a spring starter project in eclipse
2. Don't forget to include the JMS libraries while creating the project
3. This will be our resulting project structure.
4. Create the Spring boot configuration through Java class JmsConfig.java
@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 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 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 SpringWeblogicJmsPublisherApplication createSender(@Autowired final JmsTemplate jmsTemplate) { SpringWeblogicJmsPublisherApplication sender = new SpringWeblogicJmsPublisherApplication(); sender.setJmsTemplate(jmsTemplate); sender.setDestination("/com/jms/dev/distqueue"); return sender; } }
In this configuration class, first I am creating a JNDITemplate using the weblogic initial context factory as we have to use the JMS queues configured on weblogic server, then we will create a JndiObjectFactoryBean which will serve as a ConnectionFactory for us, based upon which we will create a JmsTemplate for the queue /com/jms/dev/cf .
5. Create a class SpringWeblogicJmsPublisherApplication with below code
@SpringBootApplication public class SpringWeblogicJmsPublisherApplication { private static final Logger LOG = LoggerFactory.getLogger(SpringWeblogicJmsPublisherApplication.class); private String destination; public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringWeblogicJmsPublisherApplication.class, args); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); jmsTemplate.convertAndSend(destination, "Hello To Weblogic 12.1.3 Queue and JMS API", new CorrelationIdPostProcessor("Custom-Corrleation-Id")); LOG.info("Message sent ..!!!"); } private class CorrelationIdPostProcessor implements MessagePostProcessor { private final String correlationId; public CorrelationIdPostProcessor(final String correlationId) { this.correlationId = correlationId; } @Override public Message postProcessMessage(Message message) throws JMSException { message.setJMSCorrelationID(correlationId); return message; } } }
We have annotated this class with @SpringBootApplication A single
@SpringBootApplication
annotation can be used to enable three features @EnableAutoConfiguration
, @ComponentScan
, @Configuration
next we are getting the jmsTemplate instance from our spring application context to send a text message to the specified destination(wired from config class).In the next post, I will demonstrate the the subscriber code to receive the JMS message which we sent.
If you any questions regarding creation of queues on weblogic, please comment down below and I will get back to you.
Thanks.
No comments:
Post a Comment