How to create an asynchronous singleton socket server with spring-integration?

membersound

I want to achieve the following using spring-integration: having a singleton open socket that constantly receives and writes data, asyncrhon!

This means I have to open a socket that constantly reads from the single socket, dispatches each message for async processing, and return the responses over the socket also async.

How can I achieve that asynchron pattern?

Especially: how can I use Serializer/Deserializer? As far as I understood, a serializer is only invoked on a new socket connection, so in my case only once at start of the first message?

@Configuration
public class SocketConfig {
    @Bean
    public TcpConnectionFactoryFactoryBean tcpFactory(MyConverter converter) {
        TcpConnectionFactoryFactoryBean fact = new TcpConnectionFactoryFactoryBean();
        fact.setType("server");
        fact.setPort(PORT);
        fact.setUsingNio(true); //should I use true or false?
        fact.setSingleUse(false); //keep socket constantly open
        fact.setSerializer(converter);
        fact.setDeserializer(converter);
        return fact;
    }

    @Bean
    public TcpInboundGateway serverGateway(
            @Qualifier("tcpFactory") TcpConnectionFactoryFactoryBean factory,
            @Qualifier("serverChannel") MessageChannel serverChannel) throws Exception {
        TcpInboundGateway g = new TcpInboundGateway();
        g.setConnectionFactory(factory.getObject());
        g.setRequestChannel(serverChannel);
        return g;
    }

}

@MessageEndpoint
public class SocketEndpoint {

    @ServiceActivator(inputChannel = "serverChannel")
    public Object run(Object obj) {

    }
}


@Service
public class MyConverter implements Serializer<Object>, Deserializer<Object> {
    //read from socket
    @Override
    public Object deserialize(InputStream inputStream) {
    }

    //send back to socket
    @Override
    public void serialize(Object message, OutputStream outputStream) {
    }
}
Gary Russell

A gateway is used for individual request/response pairs.

If you need to send multiple responses for a single request, you must use collaborating channel adapters as described in the documentation.

Collaborating adapters can also be used (server-side or client-side) for totally asynchronous communication (rather than with request/reply semantics).

On the server side, care must be taken to populate the ip_connectionId header because it is used to correlate the message to a connection. Messages that originate at the inbound adapter will automatically have the header set. If you wish to construct other messages to send, you will need to set the header. The header value can be captured from an incoming message.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to retry socket server creation with spring-integration?

From Dev

How to catch errors in spring-integration socket server?

From Dev

Spring Integration - How to implement an asynchronous TCP Socket requests/responses over the same connection?

From Dev

How do I create an asynchronous socket in Python?

From Java

Spring Integration and TCP server socket - how can I send a message to a client?

From Dev

How to create birt runtime object as singleton in spring

From Dev

How to create immutable and singleton class in Spring boot?

From Dev

Asynchronous socket Server C#, how to get data from AsyncCallback

From Dev

Asynchronous Server Socket in .Net Core - How do I return the result?

From Dev

Spring Integration - How to create the adapters programmatically?

From Java

How to Create Integration Test for Spring Kafka Listener

From Dev

Microsofts Asynchronous Server Socket Example

From Dev

How to create a working TCP Server socket in spring boot and how to handle the incoming message?

From Java

How to implement MQTT server using Spring Integration?

From Dev

Create simple asynchronous server

From Dev

How to create a socket connection between this client and server

From Dev

Spring integration asynchronous communication synchronous response

From Java

Spring Integration complete asynchronous transformation before the next

From Dev

Spring Integration 4 asynchronous request/response

From Dev

How server spawns new thread for a singleton object in spring framework

From Dev

TCP socket server to handle multiple clients connections concurrently using spring integration

From Dev

Asynchronous Server Socket missing the first buffer stream

From Dev

spring-integration: How to create a Spring Reactor Flux from WebFlux integration flow?

From Java

How to create a singleton class

From Dev

How to create a singleton observable?

From Dev

How is spring reactive asynchronous?

From Dev

How to create a Spring Reactor Flux from Http integration flow?

From Dev

Spring Integration: How to dynamically create subdir on sftp using IntegrationFlow

From Java

How to create Spring Integration Flow from two MessageProducerSpec?

Related Related

  1. 1

    How to retry socket server creation with spring-integration?

  2. 2

    How to catch errors in spring-integration socket server?

  3. 3

    Spring Integration - How to implement an asynchronous TCP Socket requests/responses over the same connection?

  4. 4

    How do I create an asynchronous socket in Python?

  5. 5

    Spring Integration and TCP server socket - how can I send a message to a client?

  6. 6

    How to create birt runtime object as singleton in spring

  7. 7

    How to create immutable and singleton class in Spring boot?

  8. 8

    Asynchronous socket Server C#, how to get data from AsyncCallback

  9. 9

    Asynchronous Server Socket in .Net Core - How do I return the result?

  10. 10

    Spring Integration - How to create the adapters programmatically?

  11. 11

    How to Create Integration Test for Spring Kafka Listener

  12. 12

    Microsofts Asynchronous Server Socket Example

  13. 13

    How to create a working TCP Server socket in spring boot and how to handle the incoming message?

  14. 14

    How to implement MQTT server using Spring Integration?

  15. 15

    Create simple asynchronous server

  16. 16

    How to create a socket connection between this client and server

  17. 17

    Spring integration asynchronous communication synchronous response

  18. 18

    Spring Integration complete asynchronous transformation before the next

  19. 19

    Spring Integration 4 asynchronous request/response

  20. 20

    How server spawns new thread for a singleton object in spring framework

  21. 21

    TCP socket server to handle multiple clients connections concurrently using spring integration

  22. 22

    Asynchronous Server Socket missing the first buffer stream

  23. 23

    spring-integration: How to create a Spring Reactor Flux from WebFlux integration flow?

  24. 24

    How to create a singleton class

  25. 25

    How to create a singleton observable?

  26. 26

    How is spring reactive asynchronous?

  27. 27

    How to create a Spring Reactor Flux from Http integration flow?

  28. 28

    Spring Integration: How to dynamically create subdir on sftp using IntegrationFlow

  29. 29

    How to create Spring Integration Flow from two MessageProducerSpec?

HotTag

Archive