Spring Integration with Gateways
Join the DZone community and get the full member experience.
Join For FreeContext setting
In the first article, we created a simple java application where
- A message was sent over a channel,
- It was intercepted by a service i.e. POJO and modified.
- It was then sent over a different channel
- The modified message was read from the channel and displayed.
So, let's create another test with gateway throw in for our HelloWorld service (refer to the first article of this series for more context). Let's start with the Spring configuration for the test.
<?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:p="http://www.springframework.org/schema/p" xmlns:int="http://www.springframework.org/schema/integration" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd"> <int:channel id="inputChannel"></int:channel> <int:channel id="outputChannel"> <int:queue capacity="10" /> </int:channel> <int:service-activator input-channel="inputChannel" output-channel="outputChannel" ref="helloService" method="greet" /> <bean id="helloService" class="org.academy.integration.HelloWorld" /> <int:gateway service-interface="org.academy.integration.Greetings" default-request-channel="inputChannel" default-reply-channel="outputChannel"></int:gateway> </beans>
File: /src/main/java/org/academy/integration/Greetings.java
package org.academy.integration; public interface Greetings { public void send(String message); public String receive(); }And then we add the implementation of this interface. Wait. There is no implementation. And we do not need any implementation. Spring uses something called GatewayProxyFactoryBean to inject some basic code to this gateway which allows it to read the simple string based message, without us needing to do anything at all. That's right. Nothing at all.
Note - You will need to add more code for most of your production scenarios - assuming you are not using Spring Integration framework to just push around strings. So, don't get used to free lunches. But, while it is here, let's dig in.
package org.academy.integration; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class HelloWorld1Test { private final static Logger logger = LoggerFactory .getLogger(HelloWorld1Test.class); @Autowired Greetings greetings; @Test public void test() { greetings.send("World"); assertEquals(greetings.receive(), "Hello World"); logger.debug("Spring Integration with gateways."); } }
Now, simply type "mvn -e clean install" (or use m2e plugin) and you should be able to run the unit test and confirm that given string "World" the HelloWorld service indeed returns "Hello World" over the entire arrangement of channels and messages.
Again, something optional but I highly recommend, is to run "mvn -e clean install site". This - assuming you have correctly configured some code coverage tool (cobertura in my case) will give you a nice HTML report showing the code coverage. In this case it would be 100%. I have blogged a series on code quality which deals this subject in more detail, but to cut long story short, it is very important for me to ensure that whatever coding practice / framework I use and recommend use, complies to some basic code quality standards. Being able to unit test and measure that is one such fundamental check that I do. Needless to say, Spring in general (including Spring integration) passes that check with flying colours.
Conclusion
That's it for this article. Happy coding.
Published at DZone with permission of Partha Bhattacharjee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments