STS has a number of starting templates for creating a Spring application. The templates create a Mavenbased application that includes unit test support


To create a Spring project, select New ➤ Spring Template Project from the File menu








which will create a basic Spring project including an integration test to load the Spring context.






Next, specify sts-project as the project name and com.apress.prospringintegration as the package name. This will build a Maven project called sts-project with the base package
com.apress.prospringintegration











A basic Maven project will be created with the project structure, All of the basic classes and configuration files will be created, including the root Maven pom.xml file, the Spring configuration file app-context.xml, and the integration unit test file ExampleConfiguratio nTests.java,which leverages the Spring testing harness that loads the Spring context.









Next, we will create an example similar to the first Spring project using STS, in which a message will published to a channel. An outbound JMS adapter will forward the message to the ActiveMQ JMS broker. A message-driven JMS channel adapter will receive the message and forward it to a service activator, which will log the message.


The first step is to add the Maven dependencies to the pom.xml file, Add the
Spring Integration core and JMS dependencies to support our example. Also, add the ActiveMQ client and the CGLIB library to support context scanning to simplify the XML configuration


STS pom.xml Maven Confiuration File



<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>

<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jms</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>





Creating the Integration


One of the best features of using STS with Spring Integration is the design editor for wiring up the channels and endpoints used in the application. The design editor supports two-way round-tripping between the XML source file and the graphic design editor. Any changes in either the design or Spring XML configuration file will be synchronized with the other representation. The design editor also supports importing Spring configuration files created by other editors or IDEs. In order to use the STS integration design editor, the required Spring Integration namespaces must be added. You can do this by first double-clicking the app-context.xml Spring configuration file in the Project Explorer. Then select
the Namespaces tab








select the int and int-jms Spring Integration namespaces, as well as the Spring context, in order to support context scanning. This will result in some additional tabs appearing in the design editor, including integration-graph, which is the Spring Integration design editor.










The next step is to add the JMS outbound channel adapter and the JMS message-driven channel adapter. Select the jms tab on the left side to bring up the JMS components. Drag and drop the outbound and message-driven JMS channel adapters onto the design pane. Name the outbound adapter jmsOut and the message-driven adapter jmsIn. In addition, set the destination-name properties to test.queue for both of the components so they will use the same JMS queue.










The last component to add for this example is the service activator used to receive the incoming JMS message and log it. Select the Endpoints tab on the left, and drag and drop the service-activator endpoint to the design pane. Name the service-activator endpoint messageHandler. In addition, set the ref property to jmsHandler to reference the Spring service activator, which we will created shortly.











The last step is to wire up the channels to the endpoints. Select the connection icon on the left side. This will allow creating connections between the channels and endpoints. Drag a connection between the input channel and the jmsOut outbound channel adapter. Then drag a connection between the jmsIn message-driven adapter and the output channel. Finally, drag a connection between the output channel and the messageHandler service activator.










You will need to make some modifications to the app-context.xml file. First, remove the service bean since it is not being used. Then add component scanning to support annotations for the service activator class by setting the base package to com.apress.prospringintegration. This will allow any class in this package to be scanned for annotation support. Component scanning will be used for the service activator and the JMS connection factory.




Spring Integration Configuration file app-context.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<description>Example configuration to get you started.</description>


<context:component-scan
base-package="com.apress.prospringintegration"></context:component-scan>


<int:channel id="input"></int:channel>


<int:channel id="output"></int:channel>


<int-jms:outbound-channel-adapter id="jmsOut" channel="input"
destination-name="test.queue">


</int-jms:outbound-channel-adapter>


<int-jms:message-driven-channel-adapter id="jmsIn" channel="output"
destination-name="test.queue"/>


<int:service-activator id="messageHandler" input-channel="output"
ref="jmsHandler"> </int:service-activator>


</beans>





The ActiveMQ configuration will be done using the Spring Java configuration support. Using a Java class, annotations, and component scanning, you can configure the ActiveMQ connection factory



The Spring bean is named connectionFactory, which is the default connection
factory bean name for the JMS channel adapters. The broker URL is set to vm://localhost to enable the embedded ActiveMQ JMS broker. This is done to simplify the example.


ActiveMQ Connection Factory Configuration



package com.apress.prospringintegration;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.stereotype.Component;
import javax.jms.ConnectionFactory;
@Component
public class JmsConfiguration {
@Bean(name = "connectionFactory")
public ConnectionFactory getConnectionFactory() {
ActiveMQConnectionFactory targetConnectionFactory = new ActiveMQConnectionFactory();
targetConnectionFactory.setBrokerURL("vm://localhost");
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory(targetConnectionFactory);
connectionFactory.setSessionCacheSize(10);
connectionFactory.setCacheProducers(false);

return connectionFactory;
}
}





The final piece of the message chain is to create the service activator class. This class will receive the message from the output channel and log it to the console. Again, component scanning and annotations are used to identify the Spring bean and service activator method.


Service Activator Class



package com.apress.prospringintegration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Component;
@Component
public class JmsHandler {
@ServiceActivator
public void handleMessage(String message) {
System.out.println("Received message: " + message);
}
}



Testing the Integration


In order to test the Spring Integration example, the Spring test harness created by the STS Spring template will be used , The input channel is autowired and the MessageBuilder utility class is used to send a message to the input channel. The test harness loads the Spring context, runs the test cases, and then exits. A delay is added to ensure that the JMS message is received by the messagedriven adapter.


Spring Integration Test Class



package com.apress.prospringintegration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleConfigurationTests {


@Autowired
private MessageChannel input;


@Test

public void testSendingMessage() throws Exception {
input.send(MessageBuilder.withPayload("Pro Spring Integration Example")
.build());
Thread.sleep(5000);
}
}



You can execute the text code by right-clicking the ExampleConfigurationTest class in the Package Explorer window and selecting Run As ➤ JUnit Test. The unit test will run and the message payload “Pro Spring Integration Example” will be logged in the console window.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -