Reputation: 11
I can't clearly understand how to read a TCP stream in Spring Integration. I have an external service that broadcasts some data to clients that are connected to it (TCP, Java socket). I've already tried many configurations within Spring Integration. My application successfully connects to my external service (I can confirm it from service's logs), but I still can't catch stream. However, when I manually send some message TO SERVICE (through MessageChannel), my beans work correctly. That does mean that my configuration is configured to catch write events. I could not find explicit guides or manuals for configuring it for receiving a TCP stream. I have this xml configuration in my project:
<?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:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:int="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/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="accumulator"
class="ru.utilex.trueguard.Accumulator" />
<int-ip:tcp-connection-factory id="client"
type="client"
host="192.168.2.28"
port="8383"
so-timeout="10000"/>
<int:channel id="input" />
<int:channel id="toSA" />
<int-ip:tcp-outbound-channel-adapter id="inboundClient"
client-mode="true"
channel="input"
auto-startup="true"
connection-factory="client"/>
<int:object-to-string-transformer id="serverBytes2String"
input-channel="input"
output-channel="toSA"/>
<int:service-activator
input-channel="toSA"
ref="accumulator"
method="read"/>
</beans>
My accumulator class that should be the endpoint of my receiving channel:
public class Accumulator {
public void read(String buffer) {
System.out.println("We have caught this message: " + buffer);
}
}
Upvotes: 1
Views: 368
Reputation: 174769
You need an inbound channel adapter.
Be aware that with your connection factory configuration, it is expecting messages on the stream to be delimited with CRLF (0x0d0a).
If your service uses some other mechanism to demarcate messages, you will need a different deserializer.
The messages emitted by the inbound adapter will have byte[]
payloads; if you need String, you should insert a object-to-string-transformer between the adapter and your service.
Upvotes: 1