什么是Message
Message是在各个Component之间传输的消息。Message包括两个部分,Header和Playload,Header包含了Message的元数据,用户可以把一些额外信息添加到Header里面,Payload是消息的实体,是消息传输的实际内容,一般是一个java object
什么是Channel
Channel用来连接component,例如把生产者和消费者给连接起来,生产者把生产出来的Message放到Channel中,消费者通过主动或被动的方式取出Channel里面的Message
讲多无用,用代码解析
一个Hello World例子
首先看看context.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. --> <beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/integration" 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"> <channel id="names"/> <service-activator input-channel="names" ref="helloService" method="sayHello"/> <beans:bean id="helloService" class="siia.helloworld.channel.MyHelloService"/> </beans:beans>
接下来看看HelloWorldExample的代码
public class HelloWorldExample { public static void main(String args[]) { String cfg = "siia/helloworld/channel/context.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(cfg); MessageChannel channel = context.getBean("names", MessageChannel.class); Message<String> message = MessageBuilder.withPayload("World").build(); channel.send(message); } }
当channel接受到消息后,会主动通知(还有被动的方式,以后会解析)service-activator,service-activator会调用MyHelloService实例的sayHello方法,并把消息作为实参传给sayHello。MyHelloService的sayHello逻辑很简单,就是把消息的payload打印出来。
public interface HelloService { void sayHello(String name); }
public class MyHelloService implements HelloService { @Override public void sayHello(String name) { System.out.println("Hello " + name); } }
<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"> <parent> <artifactId>siia-parent</artifactId> <groupId>com.manning.siia</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>hello-world</artifactId> <name>hello-world</name> <description>Supporting code for Chapter 1</description> <dependencies> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> </dependency> </dependencies> </project>
运行Main方法
可以看到我们把“World”传到channel里,等到的是“Hello World”