Skip to main content

Soap over JMS with Spring boot, CXF and Active MQ

Soap over JMS comes with several benefits like, asynchronous processing, one time delivery, assured delivery. This paradigm is mainly used internally in an organization (service providers and clients).
In this tutorial we are going to use Spring boot and Apache CXF to develop a Soap service and then use Active MQ to make it work as "Soap Over JMS".

Creating Soap service using Spring boot and Apache CXF

It is very easy to develop a Soap service with Spring boot. We will follow contract first approach to develop the service. We will create a XML schema & use jaxb2 plugin to generate necessary schema classes and then implement the service using them. Please refer below post where it explains in detail, how to develop a Soap service using Spring boot and CXF.

Implementing Soap over JMS

To implement the Soap over JMS we need to integrate JMS with soap service where our service listens to a request queue and replies to a response queue which is coming as JMSReplyTo header in JMS message. In JMS message we put the Soap message which is received by Soap service and processed.
Soap over JMS

We will follow below steps to add JMS features in existing Soap service which we had created in above section.

Install Active MQ

You can download the Active MQ binaries from below URL.
Once downloaded, please extract it to desired location and execute below command in "bin" folder to start it. I have downloaded and running it in MAC OS X.
./activemq start
Now open the link http://localhost:8161/admin/ in a browser to check if Active MQ is started. In same link you can browse all the objects and other JMS related stuffs provided by Active MQ.

Maven dependencies

Add below dependencies for Active MQ API and CXF API to JMS integration with Soap.


Properties Configuration

Add below properties in

Active MQ Configurations

Create given configuration class where we are creating below Spring beans.
  • mqConnectionFactory
  • This bean creates a connection factory to connect with local Active MQ.
  • jmsListenerContainerFactory
  • This bean is a message listener which will be used to listen the response queue with our Soap client.
  • JmsTemplate
  • This bean is used to use the connection factory to send the Soap request message with our Soap client.
public class ActiveMqConfig {
    private String activeMqUrl;

    private String userName;

    private String password;

    public JmsTemplate jmsTemplate(){
        JmsTemplate template = new JmsTemplate();
        return template;

    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        return factory;

    public SingleConnectionFactory mqConnectionFactory(){
        SingleConnectionFactory factory = new SingleConnectionFactory();

        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory();

        return factory;

JMS Integration with Soap Endpoints

Below configuration class is created which defines the JMS configuration to use the defined connection factory and request queue. Also we define the endpoint to publish the service using JMS endpoint.
public class JmsSoapEndpointsConfig {

    public Endpoint jmsEndpoint(SpringBus bus, JMSConfigFeature jmsConfigFeature) {
        EndpointImpl endpoint = new EndpointImpl(bus, new HelloServiceEndpoint());
        return endpoint;
    public JMSConfigFeature jmsConfigFeature(ConnectionFactory mqConnectionFactory){
        JMSConfigFeature feature = new JMSConfigFeature();

        JMSConfiguration jmsConfiguration = new JMSConfiguration();

        return feature;

Soap over JMS client

We are implementing this soap client to just test our service using JMS queues when application is up and running.
To send the Soap request using JMS below code is required. We are using EventListener to run this code at startup (To know more on Spring events refer This code sends the message to request queue and provides response queue details using JMSReplyTo header.
    private JmsTemplate template;

    public void applicationReadyEvent()throws JMSException {
        String msg = "<soapenv:Envelope \n" +
                "xmlns:soapenv=\"\"\n" +
                "xmlns:ser=\"\"\n" +
                "xmlns:hs=\"\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ser:sayHelloRequest>\n" +
                "         <arg0>\n" +
                "            <hs:userName>Black swan</hs:userName>\n" +
                "         </arg0>\n" +
                "      </ser:sayHelloRequest>\n" +
                "   </soapenv:Body>\n" +

        Session session = template.getConnectionFactory().createConnection()
                .createSession(false, Session.AUTO_ACKNOWLEDGE);

        TextMessage message = session.createTextMessage(msg);
        message.setJMSReplyTo(new ActiveMQQueue("test.response.queue"));

        template.convertAndSend("test.req.queue", message);

        System.out.println("Jms Message sent");
To receive the Soap service response we need to use JMS listener which is listening on response queue and will print the message once it receives the response. Below code defines the method to process the message using JMSListener on response queue.
    @JmsListener(destination = "test.response.queue")
    public void receiveMessage(final Message message) throws JMSException {
        String messageData = null;
        System.out.println("Received message-> " + message);
        String response = null;
        if(message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage)message;
            messageData = textMessage.getText();
            System.out.println("Message data-> "+messageData);

Running & Testing Application

To run this application we need to execute below maven command in root directory. Before running the application please make sure that Active MQ server is up and running.
Once application is started, it will execute the Soap client code and you will see below output in console.

Request execution message:

Jms Message sent

Response received and printed to console:

Received message-> ActiveMQTextMessage {commandId = 18, responseRequired = true, messageId = ID:Ashoks-MacBook-Pro.local-53071-1579434139156-1:1:6:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:Ashoks-MacBook-Pro.local-53071-1579434139156-1:1:6:1, destination = queue://test.response.queue, transactionId = null, expiration = 0, timestamp = 1579434139891, arrival = 0, brokerInTime = 1579434139892, brokerOutTime = 1579434139893, correlationId = ID:Ashoks-MacBook-Pro.local-53071-1579434139156-1:1:5:1:1, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@1cd9167a, marshalledProperties = org.apache.activemq.util.ByteSequence@17248241, dataStructure = null, redeliveryCounter = 0, size = 0, properties = {SOAPJMS_bindingVersion=1.0, SOAPJMS_contentType=text/xml; charset=UTF-8, SOAPJMS_isFault=false}, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = <soap:Envelope xmlns:soap="http://schemas.xml...ap:Envelope>}
Message data-> <soap:Envelope xmlns:soap=""><soap:Body><ns3:sayHelloRequestResponse xmlns:ns3="" xmlns:ns2=""><message>Hello Black swan!!!</message></ns3:sayHelloRequestResponse></soap:Body></soap:Envelope>

Source Code

Complete source code is available at below Git location.

Links to other related Posts

Soap Request SSL signature validation in Spring boot


Popular Posts

SpringBoot - @ConditionalOnProperty example for conditional bean initialization

@ConditionalOnProperty annotation is used to check if specified property available in the environment or it matches some specific value so it can control the execution of some part of code like bean creation. It may be useful in many cases for example enable/disable service if specific property is available. Below are the attributes which can be used for property check.
havingValue - Provide the value which need to check against specified property otherwise it will check that value should not be false.matchIfMissing - If true it will match the condition and execute the annotated code when property itself is not available in - Name of the property to be tested. If you want to test single property then you can directly put the property name as string like "" and if you have multiple properties to test then you can put the names like {"prop.name1","prop.name2"}prefix - It can be use when you want to apply some prefix to all prop…

Asynchronous REST service implementation in Spring boot

In this tutorial we will see how to create an asynchronous REST service endpoint using Spring boot application.
Asynchronous service works in a way that it will not block the client request and do the processing in separate thread. When work is complete the response returned to the client so our service will be able to handle more client requests at the same time, compare to synchronous processing model.
Let's understand how it is working in synchronous mode. In such server/client application at server side it has a pool of threads which are serving the request. If a request received by a thread then it will be blocked until it send the response back to client. In this case if processing doesn't take much time it will be able to process it quickly and accept other client requests but there could be one situation when all threads are busy and not able to accept the new client requests.

To overcome of such problems, asynchronous processing model introduced for REST services. In…

Entity to DTO conversion in Java using Jackson

It's very common to have the DTO class for a given entity in any application. When persisting data, we use entity objects and when we need to provide the data to end user/application we use DTO class. Due to this we may need to have similar properties on DTO class as we have in our Entity class and to share the data we populate DTO objects using entity objects. To do this we may need to call getter on entity and then setter on DTO for the same data which increases number of code line. Also if number of DTOs are high then we need to write lot of code to just get and set the values or vice-versa.
To overcome this problem we are going to use Jackson API and will see how to do it with minimal code only.
Maven dependency <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> Entity class Below is our ent…

Setting up kerberos in Mac OS X

Kerberos in MAC OS X Kerberos authentication allows the computers in same domain network to authenticate certain services with prompting the user for credentials. MAC OS X comes with Heimdal Kerberos which is an alternate implementation of the kerberos and uses LDAP as identity management database.
Here we are going to learn how to setup a kerberos on MAC OS X which we will configure latter in our application.
Installing Kerberos In MAC we can use Homebrew for installing any software package. Homebrew makes it very easy to install the kerberos by just executing a simple command as given below.
brew install krb5 Once installation is complete, we need to set the below export commands in user's profile which will make the kerberos utility commands and compiler available to execute from anywhere.
Open user's bash profile:
vi ~/.bash_profile Add below lines:
export PATH=/usr/local/opt/krb5/bin:$PATH export PATH=/usr/local/opt/krb5/sbin:$PATH export LDFLAGS="-L/usr/lo…