Class NetworkServer
- Direct Known Subclasses:
EchoServer
This class lets you write a Unix-like multi-process network daemon. The first process acts as the frontend. This creates a new socket, then fork several worker processes, which inherits this socket.
Worker threads will all accept connections on this port, so even when one of the worker processes die off, your clients won't notice that there's a problem.
The user of this class needs to override this class and implement abstract methods.
Several protected methods can be also overridden to customize the behaviors.
See EchoServer source code as an example.
This class also inherits from Daemon to support the daemonization.
From your main method, call into run() method. Depending on whether the current process
is started as a front end or a worker process, the run method behave accordingly.
- Author:
- Kohsuke Kawaguchi
-
Nested Class Summary
Nested classes/interfaces inherited from class com.sun.akuma.Daemon
Daemon.WithoutChdir -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ServerSocketCreates a boundServerSocketthat will be shared by all worker processes.protected abstract voidforkWorkers(JavaVMArguments args) Forks the worker thread with the given JVM args.protected voidforkWorkerThreads(JavaVMArguments arguments, int n) Called by the front-end code to fork a number of worker processes into the background.protected voidfrontend()Front-end.voidrun()Entry point.protected booleanDetermine if we should daemonize ourselves.protected voidworker()protected abstract voidworker(ServerSocket ss) Worker thread main code.Methods inherited from class com.sun.akuma.Daemon
all, chdirToRoot, closeDescriptors, daemonize, daemonize, getCurrentExecutable, init, init, isDaemonized, selfExec, writePidFile
-
Field Details
-
arguments
Java arguments.
-
-
Constructor Details
-
NetworkServer
-
-
Method Details
-
run
Entry point. Should be called from your main method.- Throws:
Exception
-
shouldBeDaemonized
protected boolean shouldBeDaemonized()Determine if we should daemonize ourselves. -
frontend
Front-end.- Throws:
Exception
-
forkWorkers
Forks the worker thread with the given JVM args. The implementation is expected to modify the arguments to suit their need, then call intoforkWorkerThreads(JavaVMArguments, int).- Throws:
Exception
-
forkWorkerThreads
Called by the front-end code to fork a number of worker processes into the background. This method never returns.- Throws:
Exception
-
createServerSocket
Creates a boundServerSocketthat will be shared by all worker processes. This method is called in the frontend process.- Throws:
Exception
-
worker
- Throws:
Exception
-
worker
Worker thread main code.- Parameters:
ss- The server socket that the frontend process created.- Throws:
Exception
-