领先一步
VMware 提供培训和认证,助您加速进步。
了解更多Spring Integration 1.0 GA 版本在两个月前于SpringOne Americas 发布,从那时起我就一直想写一篇最新的“入门”博客文章。嗯,年初总是非常忙碌,所以我的目标是提供一个包含10个步骤的动手示例。每个步骤大约需要一分钟……除非你停下来思考;)。那么,事不宜迟,我们开始吧!
您可以在这里下载最新版本(我写这篇文章时是1.0.1):http://www.springsource.com/download/community?project=Spring%20Integration
下载完成后,解压缩文件。您将看到一个包含Spring Integration项目JAR文件的“dist”目录。您还将看到一个包含依赖项的“lib”目录。
我将在示例中使用Eclipse,但当然您也可以在其他IDE中完成。您也可以使用Maven或Ivy,但这里的示例非常简单,只需创建一个目录并添加JAR文件就足够了。
创建一个新的“Java Project”(在“Package Explorer”视图中,右键单击,然后选择“New -> Java Project”),并在其中创建一个“lib”目录。然后,将以下JAR文件从Spring Integration的“dist”和“lib”目录复制到项目的“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>
现在我们添加一个元素。这定义了一个由Java内存队列支持的消息通道,一次最多可容纳10条消息。
<si:channel id="channel">
<si:queue capacity="10"/>
</si:channel>
Spring Integration组件只是嵌入在任何Spring 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)“organize imports”。)
现在,我们可以从上下文中访问通道并发送消息。由于我们还没有任何订阅者(将在接下来的几个步骤中介绍),我们将从同一通道接收。这将证明一切都已正确配置。修改*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服务,该服务将成为我们消息的订阅者。此示例将简单地将字符串转换为大写并添加一些感叹号。
public class Shouter {
public String shout(String s) {
return s.toUpperCase().concat("!!!");
}
}
服务可以像普通Bean一样添加。
<bean id="shouter" class="blog.Shouter"/>
接下来,我们将添加一个“Service Activator”来将服务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()*方法中轮询输出,不如通过添加通道适配器直接将结果发送到文件。首先,您可以从输出通道中删除队列子元素,因为不需要轮询。
<si:channel id="output"/>
添加通道适配器。您可以指定任何现有目录,在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的良好介绍。要点是,您可以轻松创建一个使用非侵入性Spring编程模型的专用集成层。当您开始添加多个端点时,真正的价值就会显现出来。那时,您可能需要添加过滤器、转换器和路由器。
Spring Integration提供的功能远不止本示例所演示的。要了解更多信息,请查看“org.springframework.integration.samples”模块中的各种项目(源代码在发行版的“src”目录中可用),并阅读参考文档。当然,您可以在Spring Integration主页上找到更多信息。