天天看點

Spring Integration --- Message Channel

什麼是Message

Spring Integration --- Message Channel

Message是在各個Component之間傳輸的消息。Message包括兩個部分,Header和Playload,Header包含了Message的中繼資料,使用者可以把一些額外資訊添加到Header裡面,Payload是消息的實體,是消息傳輸的實際内容,一般是一個java object

什麼是Channel

Spring Integration --- Message Channel

Channel用來連接配接component,例如把生産者和消費者給連接配接起來,生産者把生産出來的Message放到Channel中,消費者通過主動或被動的方式取出Channel裡面的Message

講多無用,用代碼解析

一個Hello World例子

Spring Integration --- Message Channel

首先看看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>
           

首先在spring裡定義了一個名字為“names”的channel,然後定義了一個消費者<service-activator>,該消費者會調用“helloService”bean的sayHello方法來消費channel裡面的消息

接下來看看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);
  }
}
           

代碼不難了解,首先拿到spring的上下文,得到channel的執行個體;然後利用MessageBuilder來協助建立一個payload為“World”字元串,Header為預設的Message;最後把該message發送到channel上。

當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);
  }
}
           

最後看看maven使用到的依賴

<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方法

Spring Integration --- Message Channel

可以看到我們把“World”傳到channel裡,等到的是“Hello World”

繼續閱讀