简体   繁体   中英

Spring TCP send data over incoming connection

How can I configure Spring to receive a TCP connection and then send data over it? I have data stored in a RabbitMQ queue, and I need to send this data over an incoming connection.

I assume client is working as client-mode=true according to the documentation . I have found an example on how to behave as a client in this use-case, but I can't find an example for the server side. Could anyone point me in the right direction?

I am not sure if this is a standard use-case, it seems backwards to me that the client is starting the connection and not the server, but I have little experience with TCP.

You don't need client mode; that's for when you want an inbound adapter to initiate the socket open. Use a TcpSendingMessageHandler with a server connection factory, to listen on the port.

Add an ApplicationListener bean that listens for a TcpConnectionOpenEvent see TCP Connection Events .

The event will have the connection id, which you must capture and set in a message header ( IpHeaders.CONNECTION_ID ) for any messages sent, so the adapter knows where to send the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM