How to transfer sockets created for another Java process

We have an application that creates many sockets that belong to its stream. By design, if this application somehow fails, all threads stop, which is not necessary. To overcome this problem, each thread must be separated from the main application, if one of the threads crashes, the others must be started. One thing in our minds is to pass the created socket to another Java process, so is this the right way?

A different approach is also welcome?

Waiting for your suggestions ...

+4
source share
6 answers

Branching:

You cannot pass a socket descriptor between Java processes using the regular API, as far as I can tell. However, this is similar to windows using the Winsock 2 API . In Posix, you should be able to unlock a child process with access to the parent socket, since forked processes inherit the parent sockets .

You can, I suppose, implement a new SocketImpl class that supports moving a socket descriptor to another process, but you need to write some JNI code for this.

Sounds pretty hairy to me, I doubt the new process from Java is a good idea!

Listeners:

Another approach may be to create a new β€œlistener” process, which is essentially a new pre-bifurcated employee. Each worker could take turns listening on a socket for connections. Workers will then need to coordinate work with a management process that manages the emergence of new processes as needed.

I agree with @Bozho, if an error in one thread can completely remove them (I think this should be a JVM exception that kills the whole application), you have a big problem. You should look at threading if possible.

+3
source

This is not true. (Sockets cannot be serialized.)

When one thread fails, its exception must be caught, registered, and this should not interfere with other threads. Therefore, either create it to stop completely, or create it so that it does not stop completely.

Or transfer all information about the socket (address / port) to another application that can open a similar socket.

+2
source

see this similar question socket passing between processes .
Unfortunately, the address space barrier cannot be exceeded.

+2
source

I agree with Bojo, you need to redesign your applications / critical threads so that an exception or error really kills your virtual machine.

To help you with this, I suggest you take a look at:

  • Thread.set By default, UncaughtExceptionHandler (...) and Thread.setUncaughtExceptionHandler (...) (see the hyperlink below), which helps identify unforeseen problems (for example, battery life).
  • Runtime.addShutdownHook (...) (see the hyperlink below), which helps to close things beautifully (for example, when an OutOfMemoryError occurs)

Hi

Cerber

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) http://java.sun.com/j2se/ 1.5.0 / docs / api / java / lang / Runtime.html # addShutdownHook (java.lang.Thread)

+1
source

Use a class that is shared by threads to store sockets. You can use a HashMap to denote each socket so that other threads can refer to the one it needs.

0
source

I want to answer those who say "just catch the exceptions and exit the thread."

You cannot catch all the exceptions. The following java jvm reasons to exit:

  • jvm statement due to errors in jvm implementation
  • some file in jni code (sigsegv, sigabrt)
  • Outofmemory
0
source

All Articles