Class PerClientServerManager
- All Implemented Interfaces:
Closeable,AutoCloseable,ServerManager
This is the default mode where each PipesClient has its own server process, providing isolation at the cost of memory overhead.
Connection model: The client creates a ServerSocket and the server connects TO it. This is the reverse of typical client-server patterns but allows the client to control the port assignment.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionPerClientServerManager(PipesConfig pipesConfig, Path tikaConfigPath, int clientId) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()connect(int socketTimeoutMs) Establishes a connection to the server and returns a connected Socket.voidEnsures the server is running, starting or restarting it if necessary.intgetPort()Returns the port number the server is listening on.Returns the server process.Returns the ServerSocket for accepting client connections.Returns the path to the temporary directory used by the server.intHandles a crash by checking the process exit code and marking for restart.voidincrementFilesProcessed(long maxFilesPerProcess) Increments the count of files processed and marks for restart if limit reached.booleanChecks if the server process is currently running.voidMarks the server for restart due to a fatal error (OOM, timeout, etc.).booleanChecks if the server has been marked for restart.voidshutdown()Shuts down the server process and cleans up resources.
-
Field Details
-
SOCKET_CONNECT_TIMEOUT_MS
public static final int SOCKET_CONNECT_TIMEOUT_MS- See Also:
-
-
Constructor Details
-
PerClientServerManager
-
-
Method Details
-
getPort
public int getPort()Description copied from interface:ServerManagerReturns the port number the server is listening on. May return -1 if the server hasn't been started yet.- Specified by:
getPortin interfaceServerManager- Returns:
- the server port, or -1 if not started
-
getTempDirectory
Description copied from interface:ServerManagerReturns the path to the temporary directory used by the server. May return null if the server hasn't been started yet.- Specified by:
getTempDirectoryin interfaceServerManager- Returns:
- the temp directory path, or null if not started
-
isRunning
public boolean isRunning()Description copied from interface:ServerManagerChecks if the server process is currently running.- Specified by:
isRunningin interfaceServerManager- Returns:
- true if the server process is running
-
incrementFilesProcessed
public void incrementFilesProcessed(long maxFilesPerProcess) Description copied from interface:ServerManagerIncrements the count of files processed and marks for restart if limit reached.This tracks progress toward the maxFilesProcessedPerProcess limit. When the limit is reached,
ServerManager.needsRestart()will return true and the next call toServerManager.ensureRunning()will restart the server.- Specified by:
incrementFilesProcessedin interfaceServerManager- Parameters:
maxFilesPerProcess- the maximum files before restart (0 means unlimited)
-
needsRestart
public boolean needsRestart()Description copied from interface:ServerManagerChecks if the server has been marked for restart.This allows clients to detect that a restart is pending before attempting to use an existing connection that might be stale.
- Specified by:
needsRestartin interfaceServerManager- Returns:
- true if the server has been marked for restart
-
markServerForRestart
public void markServerForRestart()Description copied from interface:ServerManagerMarks the server for restart due to a fatal error (OOM, timeout, etc.).This is called by clients when they receive a fatal error status from the server. It signals that the server process is stopping, even if
ServerManager.isRunning()might still return true briefly. The next call toServerManager.ensureRunning()will wait for the process to fully exit and then restart.In per-client mode, this is typically a no-op since the client owns the server. In shared mode, this is important for coordinating restarts among multiple clients.
- Specified by:
markServerForRestartin interfaceServerManager
-
handleCrashAndGetExitCode
public int handleCrashAndGetExitCode()Description copied from interface:ServerManagerHandles a crash by checking the process exit code and marking for restart.In per-client mode, waits briefly for the process to exit and checks the exit code to determine if this was an OOM or TIMEOUT. In shared mode, just marks for restart (exit code checking is not reliable since multiple clients share the process).
- Specified by:
handleCrashAndGetExitCodein interfaceServerManager- Returns:
- the exit code if available, or -1 if the process is still running or unavailable
-
ensureRunning
public void ensureRunning() throws IOException, InterruptedException, TimeoutException, ServerInitializationExceptionDescription copied from interface:ServerManagerEnsures the server is running, starting or restarting it if necessary.In shared mode, this method is synchronized to prevent multiple clients from attempting to restart the server simultaneously.
- Specified by:
ensureRunningin interfaceServerManager- Throws:
IOException- if the server cannot be startedServerInitializationException- if the server fails to initializeInterruptedException- if interrupted while waiting for server startupTimeoutException- if the server doesn't start within the configured timeout
-
connect
Description copied from interface:ServerManagerEstablishes a connection to the server and returns a connected Socket.The behavior differs by implementation:
- Per-client mode: Accepts incoming connection from the dedicated server
- Shared mode: Connects out to the shared server
This method should be called after
ServerManager.ensureRunning().- Specified by:
connectin interfaceServerManager- Parameters:
socketTimeoutMs- the socket timeout in milliseconds- Returns:
- a connected Socket ready for communication
- Throws:
IOException- if connection failsServerInitializationException- if the server died before connecting
-
shutdown
Description copied from interface:ServerManagerShuts down the server process and cleans up resources. After calling this method,ServerManager.ensureRunning()can be called to restart.- Specified by:
shutdownin interfaceServerManager- Throws:
InterruptedException- if interrupted while waiting for shutdown
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
getServerSocket
Returns the ServerSocket for accepting client connections. Used by PipesClient to accept the server's incoming connection.- Returns:
- the server socket
-
getProcess
Returns the server process.- Returns:
- the process, or null if not started
-