我想要一個流閱讀器的Spring集成實現。另一個應用程序(在java之外)發送數據流(由美元符號分隔)到端口9999。此服務器偵聽。如何在Spring集成中實現這個TCP流讀取器?
首先,我確保通過與telnet 127.0.0.1 9999
連接的流來傳輸流。
然後我用下面的方法創建了一個簡單的java應用程序。目前這個工作正常。
public void readStream() throws IOException{
Scanner s = null;
try {
Socket skt = new Socket("localhost", 9999);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(
skt.getInputStream()));
s = new Scanner(bufferedReader);
s.useDelimiter("[$]");
System.out.println(s);
while (s.hasNext()) {
System.out.println("----------------------");
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
現在,我想在Spring Integration框架中實現這個。我看着https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-server和http://docs.spring.io/autorepo/docs/spring-integration/2.0.0.M3/spring-integration-reference/html/stream.html。然而我很困惑從哪裏開始? 連接到發送應用程序需要什麼? (我對Spring Framework真的很陌生。)
對我來說困難在於術語。我應該創建一個TCP入站網關嗎?或接收通道適配器?或者是因爲我在請求流嗎?
EDIT加里的意見後:
<bean class="org.springframework.integration.ip.tcp.serializer.ByteArraySingleTerminatorSerializer" id="deserializer1">
<constructor-arg type="byte" value="$"/>
</bean>
<int-ip:tcp-connection-factory id="server" type="server" port="9999"
deserializer="deserializer1"
/>
<int-ip:tcp-inbound-channel-adapter id="adapter" connection-factory="server" request-channel="channel1"/>
<int:channel id="channel1" />
是'服務器'_my_應用程序,還是發送數據流的_other_應用程序? – codesmith
服務器是您的應用程序。 –
謝謝加里,我添加了一些代碼;你是這個意思嗎? – codesmith