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.
https://www.thetechnojournals.com/2020/01/soap-services-with-spring-boot-and.html

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.
http://activemq.apache.org/download.html
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.
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-jms</artifactId>
            <version>3.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

Properties Configuration

Add below properties in application.properties.
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin

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.
@Configuration
public class ActiveMqConfig {
    @Value("${spring.activemq.broker-url}")
    private String activeMqUrl;

    @Value("${spring.activemq.broker-url}")
    private String userName;

    @Value("${spring.activemq.broker-url}")
    private String password;

    @Bean
    public JmsTemplate jmsTemplate(){
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(mqConnectionFactory());
        return template;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(mqConnectionFactory());
        return factory;
    }

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

        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory();
        mqConnectionFactory.setBrokerURL(activeMqUrl);
        mqConnectionFactory.setUserName(userName);
        mqConnectionFactory.setPassword(password);

        factory.setTargetConnectionFactory(mqConnectionFactory);
        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.
@Configuration
public class JmsSoapEndpointsConfig {

    @Bean
    public Endpoint jmsEndpoint(SpringBus bus, JMSConfigFeature jmsConfigFeature) {
        EndpointImpl endpoint = new EndpointImpl(bus, new HelloServiceEndpoint());
        endpoint.getFeatures().add(jmsConfigFeature);
        endpoint.publish("jms://");
        return endpoint;
    }
    @Bean
    public JMSConfigFeature jmsConfigFeature(ConnectionFactory mqConnectionFactory){
        JMSConfigFeature feature = new JMSConfigFeature();

        JMSConfiguration jmsConfiguration = new JMSConfiguration();
        jmsConfiguration.setConnectionFactory(mqConnectionFactory);
        jmsConfiguration.setTargetDestination("test.req.queue");
        jmsConfiguration.setMessageType("text");

        feature.setJmsConfig(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 https://www.thetechnojournals.com/2019/10/spring-boot-application-events.html). This code sends the message to request queue and provides response queue details using JMSReplyTo header.
    @Autowired
    private JmsTemplate template;

    @EventListener(ApplicationReadyEvent.class)
    public void applicationReadyEvent()throws JMSException {
        String msg = "<soapenv:Envelope \n" +
                "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
                "xmlns:ser=\"http://services.ttj.com/\"\n" +
                "xmlns:hs=\"http://services.ttj.com/hello-service\">\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" +
                "</soapenv:Envelope>";

        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="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns3:sayHelloRequestResponse xmlns:ns3="http://services.ttj.com/" xmlns:ns2="http://services.ttj.com/hello-service"><message>Hello Black swan!!!</message></ns3:sayHelloRequestResponse></soap:Body></soap:Envelope>

Source Code

Complete source code is available at below Git location.
https://github.com/thetechnojournals/spring-tutorials/tree/master/CxfSoapService

Links to other related Posts

Soap Request SSL signature validation in Spring boot

https://www.thetechnojournals.com/2020/02/xml-validation-using-xsd-schema.html

Comments

Popular Posts

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=&

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 environment. name - 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 "property.name" 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

Multiple data source with Spring boot, batch and cloud task

Here we will see how we can configure different datasource for application and batch. By default, Spring batch stores the job details and execution details in database. If separate data source is not configured for spring batch then it will use the available data source in your application if configured and create batch related tables there. Which may be the unwanted burden on application database and we would like to configure separate database for spring batch. To overcome this situation we will configure the different datasource for spring batch using in-memory database, since we don't want to store batch job details permanently. Other thing is the configuration of  spring cloud task in case of multiple datasource and it must point to the same data source which is pointed by spring batch. In below sections, we will se how to configure application, batch and cloud task related data sources. Application Data Source Define the data source in application properties or yml con

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