更進一步
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 問題。