先人一步
VMware 提供培训和认证,助您加速进步。
了解更多Spring Integration 1.0 GA 版本于两个月前在 SpringOne Americas 发布。自那以后,我一直想写一篇新的、最新的“入门”博客文章。嗯,年初总是很忙,所以我的目标是提供一个包含10个步骤的实操示例。每个步骤大约需要一分钟……除非你停下来思考;)。那么,话不多说,开始吧!
您可以从这里获取最新版本(撰写本文时的版本为 1.0.1):http://www.springsource.com/download/community?project=Spring%20Integration
下载完成后,解压文件。您将看到一个 'dist' 目录,其中包含构成 Spring Integration 项目的 JAR 文件。您还将看到一个 'lib' 目录,其中包含依赖项。
我将使用 Eclipse 作为示例,当然您也可以在其他 IDE 中完成。您也可以使用 Maven 或 Ivy,但这里的示例非常简单,只需创建一个目录并添加 JAR 文件即可。
创建一个新的“Java Project”(在“Package Explorer”视图中,右键单击,然后选择“New -> Java Project”),并在其中创建一个“lib”目录。然后,将 Spring Integration 的“dist”和“lib”目录中的以下 JAR 文件复制到项目中的“lib”目录。来自 dist:
在 Eclipse 中刷新“lib”目录(按 F5),并将这些 JAR 添加到构建路径(选择 JAR 文件,右键单击,然后选择“Build Path -> Add to Build Path”)。最后,在“src”目录中创建一个“blog”包。
如果您正在使用 Spring IDE 或 SpringSource Tool Suite,您可以添加 Spring 项目特性,然后右键单击“blog”包来创建新的 bean 定义文件。否则,只需创建以下内容并将其命名为“config.xml”即可
<?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:si="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-1.0.xsd">
</beans>
现在我们将添加一个单独的元素。这定义了一个消息通道(Message Channel),它由 Java 内存队列支持,并且一次最多可以容纳 10 条消息(Message)。
<si:channel id="channel">
<si:queue capacity="10"/>
</si:channel>
Spring Integration 组件可以简单地嵌入到任何 Spring ApplicationContext 中。因此,我们所需要做的就是基于此配置文件创建一个 ApplicationContext。如果在 web 应用程序中运行,您可以使用 Spring 的 ContextLoaderListener 进行引导;如果您在 dm Server 中运行,它也会为您处理此事。对于这个简单的示例,我们只需在一个名为 Bootstrap 的类中(位于“blog”包中)创建一个 main() 方法
public class Bootstrap {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("blog/config.xml");
}
}
(在任何时候,若要在 Eclipse 中快速解决导入问题,您可以使用 Ctrl+Shift+O 进行“组织导入”...或在 Mac 上使用 Command+Shift+O)。
现在,我们可以从上下文中访问通道并发送一条消息。由于我们还没有任何订阅者(将在接下来的几个步骤中添加),我们也将从同一个通道接收。这将证明一切都已正确配置。修改 main() 方法,使其如下所示
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
PollableChannel channel = (PollableChannel) context.getBean("channel");
channel.send(new StringMessage("Spring Integration rocks"));
Message<?> reply = channel.receive();
System.out.println("received: " + reply);
}
当您运行它时,您应该会看到该消息的 toString() 方法的结果输出到控制台。
Spring Integration 的目标是无侵入性。这意味着我们现在将逐步修改示例,以便您的代码与框架之间没有直接耦合。然而,我们首先要做的就是添加一个 POJO Service,它将成为我们消息的订阅者。这个示例将简单地将字符串转换为大写并添加一些感叹号
public class Shouter {
public String shout(String s) {
return s.toUpperCase().concat("!!!");
}
}
该 Service 可以作为一个普通的 bean 添加
<bean id="shouter" class="blog.Shouter"/>
接下来,我们将添加一个“Service Activator”来将 Service bean 连接到输入和输出通道。我们需要将现有的“channel”重命名为“output”,然后为输入添加一个简单的非缓冲通道。完整的配置应如下所示
<si:channel id="input"/>
<si:channel id="output">
<si:queue capacity="10"/>
</si:channel>
<si:service-activator input-channel="input"
output-channel="output"
ref="shouter"
method="shout"/>
<bean id="shouter" class="blog.Shouter"/>
修改 main() 方法,使其向输入通道发送消息并从输出通道接收。请注意,我们也在修改依赖查找,并且通道被转换为不同的类型(“input”通道是非缓冲的,因此不像输出通道那样是 PollableChannel)
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
MessageChannel input = (MessageChannel) context.getBean("input");
PollableChannel output = (PollableChannel) context.getBean("output");
input.send(new StringMessage("Spring Integration rocks"));
Message<?> reply = output.receive();
System.out.println("received: " + reply);
}
与在 main() 方法中轮询输出不同,我们可以通过添加 Channel Adapter 将结果直接发送到文件。首先,您可以从输出通道中删除 queue 子元素,因为不再需要轮询
<si:channel id="output"/>
添加 Channel Adapter。您可以指定任何现有目录,在 Windows 上您应该包含驱动器盘符(例如 "C:/tmp")
<file:outbound-channel-adapter channel="output" directory="/tmp"/>
接下来,您可以更新 Bootstrap 的 main() 方法,使其仅发送
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
MessageChannel input = (MessageChannel) context.getBean("input");
input.send(new StringMessage("Spring Integration rocks"));
}
您还需要将 'file' 命名空间添加到 XSD 配置中。顶层 'beans' 元素应如下所示
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
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-1.0.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
运行示例,您应该会在一个扩展名为 “.msg” 的文件中看到输出(您可以添加一个文件名生成策略,但这超出了本文的范围)。
最后一步将实现完全无侵入性的目标。与将消息发送到消息通道不同,将字符串作为参数发送到一个简单的接口会更加简洁。在“blog”包中创建以下接口
public interface Gateway {
void send(String s);
}
然后,将以下元素添加到您的配置中
<si:gateway id="gateway" service-interface="blog.Gateway" default-request-channel="input"/>
最后,在 main 方法中使用 Gateway 而不是通道。现在,您只需传递一个字符串
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
Gateway gateway = (Gateway) context.getBean("gateway");
gateway.send("Spring Integration rocks");
}
希望这能为您提供一个不错的 Spring Integration 入门介绍。主要 takeaway 是您可以轻松创建一个使用无侵入性 Spring 编程模型的专用集成层。当您开始添加多个端点时,其真正的价值就显而易见了。那时,您可能需要添加过滤器、转换器和路由器。
Spring Integration 提供的功能远不止本示例所展示的。要了解更多信息,请查看“org.springframework.integration.samples”模块中的各种项目(源代码位于分发包的“src”目录中),并阅读 参考文档。当然,您可以在 Spring Integration 主页上找到更多信息。