This example shows the use of a Gateway to inject method onto a Spring Integration channel.
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/META-INF/spring/integration-context.xml"); DeepThoughtGateway gateway = ctx.getBean(DeepThoughtGateway.class); System.out.printf("%s? %s\n", THE_ULTIMATE_QUESTION, gateway.askQuestion(THE_ULTIMATE_QUESTION)); ctx.close();
public interface DeepThoughtGateway { String askQuestion(String question); }
The gateway is an interface that is implemented by Integration Framework with a proxy. All the proxy does is push the question onto an input channel and receive the answer on the reply channel, returning the answer. This simple example is what was the “ah-hah” moment for me, because it shows how a piece of client code interacts with the channels and messages and yet has no knowledge of this fact. The gateway is like any other collaborator from the perspective of the calling code.
Here is the config:
<bean id="service" class="com.wordpress.springeip.ExampleService" /> <channel id="questionChannel" /> <channel id="answerChannel" /> <gateway service-interface="com.wordpress.springeip.DeepThoughtGateway" id="deepThoughtGateway" default-request-channel="questionChannel" default-reply-channel="answerChannel" /> <service-activator id="exampleServiceActivator" ref="service" input-channel="questionChannel" output-channel="answerChannel" method="getTheAnswerTo" />
We have an input channel that accepts the question and another channel that handles the reply. The service-activator
wires the service bean that provides the answer. The gateway
is wired to push the question onto the questionChannel and return the answer received on the reply channel.
started org.springframework.integration.gateway.SimpleMessagingGateway@ef137d - AbstractEndpoint started deepThoughtGateway - AbstractEndpoint No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created. - DefaultConfiguringBeanFactoryPostProcessor No bean named 'taskScheduler' has been explicitly defined. Therefore, a default SimpleTaskScheduler will be created. - DefaultConfiguringBeanFactoryPostProcessor started exampleServiceActivator - AbstractEndpoint started org.springframework.integration.endpoint.EventDrivenConsumer#0 - AbstractEndpoint started org.springframework.integration.scheduling.SimpleTaskScheduler@170888e - SimpleTaskScheduler started org.springframework.integration.endpoint.EventDrivenConsumer@b4e29b - AbstractEndpoint What is the answer to the Ultimate Question of Life the Universe and Everything? 42 stopped org.springframework.integration.endpoint.EventDrivenConsumer@b4e29b - AbstractEndpoint stopped org.springframework.integration.gateway.SimpleMessagingGateway@ef137d - AbstractEndpoint stopped deepThoughtGateway - AbstractEndpoint stopped exampleServiceActivator - AbstractEndpoint stopped org.springframework.integration.endpoint.EventDrivenConsumer#0 - AbstractEndpoint stopped org.springframework.integration.scheduling.SimpleTaskScheduler@170888e - SimpleTaskScheduler shutting down TaskExecutor - SimpleTaskScheduler
This is my setup at home:
- Computer: Dell pentium PC
- OS: Ubuntu 10.04 LTS – the Lucid Lynx
- JDK: java-6-sun-1.6.0.20
- IDE: Spring Source STS 2.3.2
- SCM: Subversion
- Build: maven 2.2.1
- Maven repository: Artifactory on localhost
The basic maven pom is shown below for project setup with Spring 3.0.3 and Spring Integration 1.0.4. Often times you will see repository references to point to spring’s maven repositories. I’m using Artifactory as a mirror so I don’t have to clutter my poms with that stuff.
Also note the testResource setup to tell maven to copy non-java files from src/test/java to target. I find it convenient to have the test context files right there with the test class.
The code is in SVN at http://springeip.googlecode.com/svn/trunk/springeip-setup.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.wordpress.springeip.setup</groupId> <version>1.0.0.SNAPSHOT</version> <artifactId>springeip-setup</artifactId> <packaging>jar</packaging> <name>springeip-setup</name> <url>https://springeip.wordpress.com</url> <properties> <spring.framework.version>3.0.3.RELEASE</spring.framework.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>org.springframework.integration</artifactId> <version>1.0.4.RELEASE</version> <exclusions> <exclusion> <artifactId>org.springframework.aop</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>org.springframework.context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>org.springframework.transaction</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> </build> </project>