我想要一个流读取器的 Spring Integration 实现。另一个应用程序(java 之外)将数据流(由美元符号分隔)发送到端口 9999。此服务器监听。
首先,我通过使用 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 入站网关吗?还是接收 channel 适配器?还是出站是因为我在请求流??
EDIT 在 Gary 的评论之后:
<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" />
最佳答案
当服务器发送对入站请求的回复时,将使用入站网关。入站 channel 适配器 (<int-ip:tcp-inbound-channel-adapter)
仅用于单向集成 - 客户端仅发送数据,不接收回复。
你需要一个 server connection factory , 配置为使用 ByteArraySingleTerminatorSerializer
配置了你的 $
分隔符,在 deserializer
中属性(property)。
请使用latest documentation不是您问题中的旧版本。
关于java - 如何在 Spring Integration 中实现这个 TCP 流读取器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29842413/