Spring Integration Channels :Lab-1
Here we will understand publisher and subscriber interaction b/w channels using java project with scenarios.
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.intertech.si</groupId> <artifactId>lab1-channels</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Spring Integration Lab 1</name> <description>Lab 1 - using channels</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <springframework.version>4.0.4.RELEASE</springframework.version> <spring.integration.version>4.0.0.RELEASE</spring.integration.version> <spring.integration.xml.version>4.0.0.RELEASE</spring.integration.xml.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>${spring.integration.version}</version> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-stream</artifactId> <version>${spring.integration.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies></project>
Spring integration channel registration
spring-integration-channels.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:int="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int-stream="http://www.springframework.org/schema/integration/stream" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd"> <!-- message producer / a Spring Integration wrapped Java Standard input stream --> <int-stream:stdin-channel-adapter id="producer" channel="messageChannel" /> <!-- a pair of message consumers / a pair of Spring Integration wrapped Java Standard output streams --> <int-stream:stdout-channel-adapter id="consumer1" channel="messageChannel" append-newline="true" /> <int-stream:stdout-channel-adapter id="consumer2" channel="messageChannel" append-newline="true" /> <int:poller id="defaultPoller" default="true" max-messages-per-poll="5" fixed-rate="200" /> <!-- a pub/sub message channel --> <int:publish-subscribe-channel id="messageChannel" /></beans>
Main container loader class which will load all the beans
Startup.java
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Startup { @SuppressWarnings({ "resource", "unused" }) // @SuppressWarnings({ "resource" }) public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "/META-INF/spring/si-components.xml"); while (true) { } }}
Here If we run Sratup.java class then we have to enter message which means we are publishing message using console which is producer and If you see xml there are 2 consumers which are consuming messgae.
So if i enter deepak on console then deppak will be prited twice because there are 2 subscribers(consumers) here.
Output:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Contanier started…..
Deepak
Deepak
Deepak
Please find below GIT URL for source code of this Lab