Spring Boot Weblogic JMS Example (2024)
In this tutorial, we'll demonstrate how to create a Spring Boot JMS application with Weblogic as broker.
In this example, we will refer Spring Boot JMS + ActiveMQ Example and update the code for Weblogic broker.
Q: What is JMS?
The Java Message Service (JMS) API is a Java API that allows applications to produce, publish, receive, and read messages in a secure, asynchronous, and loosely connected manner.
The core elements that make up a JMS application are connections, JMS Broker (middleware), Queues and Topics, and Producers and Consumers.
Q: What is JMS Broker?
JMS Broker is the mediator between the producer and the consumer. Producer send message to broker through Queue/Topic messaging. When the broker receives a message, it transmits it to consumers who have subscribed to the Topic or Queue.
A client application can send and receive messages using below two messaging patterns (or domains) using these fundamental objects.
Point-to-Point domain
A Queue may have multiple receiver, however when a client sent a message to a queue destination from which only one receiver can receive it. No other receiver with access to that destination will be able to receive that message. The receiver form a line and waits in line to receive new messages from the Queue. This is known as P2P (Point-to-Point) messaging.Publish/Subscribe domain
In this pattern, a Topic send message from producer to many consumers at once, this is commonly knows as Publish-and-Subscribe (Pub/Sub) broadcast messaging.Create Spring Boot application
Create Spring Boot application from Spring Initializr.
Project Structure
Add dependencies
We can get the wlthin3client
jar from the weblogic server libs folder and can upload to
our repository and point in pom.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-boot-jms-topic-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-jms-topic-example</name>
<description>Spring Boot Weblogic JMS Topic Example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>wls</groupId>
<artifactId>wlthin3client</artifactId>
<version>12.2.1.4.0</version>
<scope>system</scope>
<systemPath>C:/repo/wls/wlthin3client/12.2.1.4.0/wlthin3client.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Take a look at our suggested posts:
Application Properties
Add Weblogic broker connection details. By default it provide point-to-pont communication. Make pub-sub to true for Topic.
spring.jms.pub-sub-domain=true
weblogic.jms.url=t3://techgeeknextJMSServer:9127
weblogic.jms.username=testUser
weblogic.jms.password=test123
weblogic.jms.topic=EmpTopic
weblogic.jms.topic.subscriber=EmpSub
weblogic.jms.clientId=client123
Spring Boot Weblogic JMS Listener Configuration
package com.techgeeknext.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.support.destination.JndiDestinationResolver;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.jndi.JndiTemplate;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Properties;
@Configuration
@EnableJms
public class JMSConfig {
@Value("${weblogic.jms.url}")
private String jmsUrl;
@Value("${weblogic.jms.username}")
private String jmsUser;
@Value("${weblogic.jms.password}")
private String jmsPassword;
@Value("${weblogic.jms.clientId}")
private String clientId;
@Bean
public DefaultJmsListenerContainerFactory empJmsContFactory()
throws NamingException {
DefaultJmsListenerContainerFactory containerFactory =
new DefaultJmsListenerContainerFactory();
JndiDestinationResolver jndiDestinationResolver =
new JndiDestinationResolver();
jndiDestinationResolver.setJndiEnvironment(getJNDIProperties());
containerFactory.setDestinationResolver(jndiDestinationResolver);
containerFactory.setPubSubDomain(true);
containerFactory.setConnectionFactory(connectionFactory());
containerFactory.setSubscriptionDurable(true);
return containerFactory;
}
@Bean
public CachingConnectionFactory connectionFactory() throws NamingException{
CachingConnectionFactory factory = new CachingConnectionFactory();
//get the jndi connection values
ConnectionFactory connectionFactory = (ConnectionFactory)jndiConnectionFactory().getObject();
factory.setTargetConnectionFactory(connectionFactory);
factory.setClientId(clientId);
return factory;
}
public JndiObjectFactoryBean jndiConnectionFactory() throws NamingException {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
jndiObjectFactoryBean.setJndiName("techgeeknextFactory");
jndiObjectFactoryBean.afterPropertiesSet();
return jndiObjectFactoryBean;
}
private Properties getJNDIProperties(){
final Properties jndiProps = new Properties();
jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
jndiProps.setProperty(Context.PROVIDER_URL, jmsUrl);
if (jmsUser != null && !jmsUser.isEmpty()) {
jndiProps.setProperty(Context.SECURITY_PRINCIPAL, jmsUser);
}
if (jmsPassword != null && !jmsPassword.isEmpty()) {
jndiProps.setProperty(Context.SECURITY_CREDENTIALS, jmsPassword);
}
return jndiProps;
}
@Bean
public JndiTemplate jndiTemplate() {
JndiTemplate jndiTemplate = new JndiTemplate();
jndiTemplate.setEnvironment(getJNDIProperties());
return jndiTemplate;
}
}
Spring Boot Weblogic JMS Listener
package com.techgeeknext.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class EmployeeListener {
@JmsListener(destination = "${weblogic.jms.topic}",
containerFactory = "empJmsContFactory",
subscription = "${weblogic.jms.topic.subscriber}")
public void onMessage(String message) {
log.info("Message listener: " + message);
}
}
Test Spring Boot Weblogic JMS Example
- Start the Weblogic JMS Server and publish the message.
- Start the Spring Boot Application by running
spring-boot:run
or by running main class. JMS Listener/Receiver output
We can see that listener have received the message from the publisher.. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.2.RELEASE) 19:44:21.746 INFO 38024 --- [main] c.t.SpringBootJmsTopicExampleApplication : Starting SpringBootJmsTopicExampleApplication (D:\spring-boot-jms-topic-example\target\classes started D:\spring-boot-jms-topic-example) 19:44:21.751 INFO 38024 --- [main] c.t.SpringBootJmsTopicExampleApplication : No active profile set, falling back to default profiles: default 19:44:24.207 INFO 38024 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 19:44:24.233 INFO 38024 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 19:44:24.234 INFO 38024 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37] 19:44:24.384 INFO 38024 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 19:44:24.384 INFO 38024 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2561 ms 19:44:24.836 INFO 38024 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 19:44:25.168 INFO 38024 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 19:44:25.570 INFO 38024 --- [main] c.t.SpringBootJmsTopicExampleApplication : Started SpringBootJmsTopicExampleApplication in 4.488 seconds (JVM running for 5.231) 19:44:39.946 INFO 38024 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 19:44:39.946 INFO 38024 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 19:44:39.958 INFO 38024 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 11 ms 19:44:40.320 INFO 38024 --- [enerContainer-1] c.t.listener.EmployeeListener : Message listener: Received Weblogic JMS Message
Download Source Code
The full source code for this article can be found below.
- Download it here - Spring Boot Weblogic JMS Example