2

Can we send the Java.net.Socket Object over queue and received it make some operation and close the Socket object after sending the data to client.

I Tried with Serialization and deserialization, but I am not sure whether socket is supported for serialization and deserialization.

thanks, Hrushi

5
  • How is that going to work? How can a process different from the one that opened the socket close it? Commented Aug 26, 2019 at 11:58
  • Actual I want to use same Socket object, to send the reply back over socket. But code flow is different, this is the reason I am using Queue, to send the data as well as Socket over queue. Once I received it, would make some operation and send the data back over socket using same socket object and close it. do you have any possible approach for such scenarios. Commented Aug 26, 2019 at 12:11
  • 1
    You cannot serialize a socket. You can only pass sockets to child processes, not over Kafka. You will have to bring the data back to the original process that holds the socket, so that it can send it out. Commented Aug 26, 2019 at 12:28
  • ohhk. So I may need to invoke callback function to send the data back over socket and close the socket, once another process read data and process it. Commented Aug 26, 2019 at 12:44
  • Kafka Callback wont be useful here. I found solution (it may be weird ), having Static Map to store the Socket Object with some unique key. Sending the Unique key in another code flow through Kafka queue, and again retrieving object from kafka queue to send the reply over socket on same call. Commented Aug 27, 2019 at 9:44

1 Answer 1

1

Since the Java.net.Socket class doesn't implement Serializable, a Producer can't write it to Kafka for a Consumer to use. The reason why it's not Serializable is the same reason why you probably don't want to do this anyway: Once you have constructed a Socket, you really can't pass the object itself outside of a JVM and still be able to call the socket.

Having said that, you could write the arguments you use to construct a Socket (e.g. write a Kafka message that contains a host and a port) and then let the consumer construct its own socket with that information and then interact with the socket.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.