更进一步
VMware 提供培训和认证,以加速您的进步。
了解更多在 Spring Integration 2.0 Milestone 3 中引入的 UDP 和 TCP 通道适配器提供了两个或多个 Spring Integration 应用程序之间,或者 Spring Integration 应用程序与其他平台之间的轻量级通信。
继 Oleg 关于贷款经纪人的博客之后,我使用相同的示例来展示如何使用 M3 中提供的新 UDP 适配器。 假设贷款经纪公司的 CEO 听到了一些客户的抱怨,说几家银行的报价离谱。 他要求 CIO 监控一段时间内银行返回的报价。
由于贷款经纪人应用程序是使用 Spring Integration 构建的,因此很容易连接报价、过滤它们,并使用 M3 中的新 ip 适配器将其发送到另一个应用程序。 这可以通过最少的重新配置来完成; 下图显示了如何使用多播将高利率报价发送到简单的 Spring Roo 应用程序以及 Groovy 和 Perl 脚本,或者实际上发送到支持 IP 的任何平台。
首先,让我们显式添加聚合通道并连接报价...
<int:channel id="quotesAggregationChannel">
<int:interceptors>
<int:wire-tap channel="loanSharkDetectorChannel"/>
</int:interceptors>
</int:channel>
这会将每个报价发送到 loanSharkDetectorChannel (以及聚合器)。
下一步是设置过滤器和通道来检测高利贷者...
<int:channel id="loanSharkDetectorChannel" />
<int:filter id="loanSharkFilter"
input-channel="loanSharkDetectorChannel"
output-channel="loanSharkChannel"
expression="payload.rate > 5.2"/>
<int:channel id="loanSharkChannel" />
<int:transformer ref="udpTransformer"
input-channel="loanSharkChannel"
output-channel="sharkOutChannel"/>
<int:channel id="sharkOutChannel" />
过滤器会丢弃非高利贷者(那些报价 <= 5.2% 的; 转换器 只是将 LoanQuote 转换为包含银行名称和利率的字符串,以逗号分隔。
最后,我们添加 UDP 通道适配器; 选择多播,以便我们可以将报价发送到多个目的地...
<int-ip:outbound-channel-adapter id="udpOut"
channel="sharkOutChannel"
protocol="udp"
host="225.6.7.8"
multicast="true"
port="11111"/>
此适配器会将数据多播到多播地址 225.6.7.8 上的端口 11111。
现在让我们看看接收端。 首先,我使用以下脚本创建了一个简单的 Roo 应用程序...
project --topLevelPackage com.springframework.integration.loanbroker.loanshark
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity --class ~.domain.LoanShark
field string --fieldName name
field number --type java.lang.Long --fieldName counter
field number --type java.lang.Double --fieldName averageRate
finder add --finderName findLoanSharksByName
controller scaffold --class ~.web.SharkController
logging setup --level DEBUG
dependency add --artifactId org.springframework.integration.ip --groupId org.springframework.integration --version 2.0.0.M3
perform eclipse
(注意,在发出 perform eclipse 之前,我将 pom.xml 中的 Spring 版本更改为 3.0.1.RELEASE-A)。
请参阅 Roo 文档,了解这些步骤如何生成初始 Roo 应用程序。
最后,我向 Roo 应用程序添加了一些 Spring Integration。
<int-ip:inbound-channel-adapter id="udpIn"
channel="channel"
multicast="true"
protocol="udp"
multicast-address="225.6.7.8"
port="11111"/>
<int:channel id="channel" />
<int:transformer ref="transformer"
input-channel="channel"
output-channel="transformedChannel"/>
<int:channel id="transformedChannel" />
<int:service-activator
id="activator"
input-channel="transformedChannel"
ref="accumulator" />
<bean id="accumulator" class="com.springframework.integration.loanbroker.loanshark.biz.Accumulator" />
<bean id="transformer" class="com.springframework.integration.loanbroker.loanshark.biz.SharkTransformer" />
通道适配器接收数据包 ([bankName],[rate]); 转换器 将数据转换为 SharkQuote 对象,该对象传递到 累加器。 这会查找银行,必要时创建它,递增计数器并计算该银行的平均利率。
这是 Roo 应用程序的屏幕截图; 点击查看更大的图像。
这个 Groovy 脚本也看到了数据包...
socket = new MulticastSocket(11111)
mcast = InetAddress.getByName("225.6.7.8")
socket.joinGroup(mcast)
buffer = (' ' * 1024) as byte[]
while(true) {
incoming = new DatagramPacket(buffer, buffer.length)
socket.receive(incoming)
s = new String(incoming.data, 0, incoming.length)
println ("**Shark** " + s)
}
... 这个 Perl 脚本也是如此...
#!/usr/bin/perl -w
use strict;
use IO::Socket::Multicast;
my $socket = IO::Socket::Multicast->new(LocalPort=>11111, ReuseAddr=>1)
or die "Can't start UDP server: $@";
$socket->mcast_add('225.6.7.8');
my ($datagram,$flags);
while ($socket->recv($datagram,1024,$flags)) {
print "**Shark** $datagram\n";
}
$socket->close();
您可能需要安装 IO::Socket::Multicast 才能使此脚本正常工作。
以上任一脚本都会创建此输出...
结论
这只是对新 IP 适配器的简要介绍。 该代码(对 Loan Broker 的更新以及 Loan Shark Roo 应用程序)可在 svn 中找到。
https://src.springsource.org/svn/spring-integration/trunk/spring-integration-samples/loan-broker/
https://src.springsource.org/svn/spring-integration/trunk/spring-integration-samples/loanshark/
M3 中还提供了 TCP 适配器; 在 此处 阅读有关它们的信息。
我们正在考虑为双向通信创建 TCP/IP 网关。 如果这是您需要的东西,请投票支持 JIRA 问题。