Class PerClientServerManager

java.lang.Object
org.apache.tika.pipes.core.PerClientServerManager
All Implemented Interfaces:
Closeable, AutoCloseable, ServerManager

public class PerClientServerManager extends Object implements ServerManager
Manages a dedicated PipesServer process for a single PipesClient.

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 Details

    • SOCKET_CONNECT_TIMEOUT_MS

      public static final int SOCKET_CONNECT_TIMEOUT_MS
      See Also:
  • Constructor Details

    • PerClientServerManager

      public PerClientServerManager(PipesConfig pipesConfig, Path tikaConfigPath, int clientId)
  • Method Details

    • getPort

      public int getPort()
      Description copied from interface: ServerManager
      Returns the port number the server is listening on. May return -1 if the server hasn't been started yet.
      Specified by:
      getPort in interface ServerManager
      Returns:
      the server port, or -1 if not started
    • getTempDirectory

      public Path getTempDirectory()
      Description copied from interface: ServerManager
      Returns the path to the temporary directory used by the server. May return null if the server hasn't been started yet.
      Specified by:
      getTempDirectory in interface ServerManager
      Returns:
      the temp directory path, or null if not started
    • isRunning

      public boolean isRunning()
      Description copied from interface: ServerManager
      Checks if the server process is currently running.
      Specified by:
      isRunning in interface ServerManager
      Returns:
      true if the server process is running
    • incrementFilesProcessed

      public void incrementFilesProcessed(long maxFilesPerProcess)
      Description copied from interface: ServerManager
      Increments 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 to ServerManager.ensureRunning() will restart the server.

      Specified by:
      incrementFilesProcessed in interface ServerManager
      Parameters:
      maxFilesPerProcess - the maximum files before restart (0 means unlimited)
    • needsRestart

      public boolean needsRestart()
      Description copied from interface: ServerManager
      Checks 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:
      needsRestart in interface ServerManager
      Returns:
      true if the server has been marked for restart
    • markServerForRestart

      public void markServerForRestart()
      Description copied from interface: ServerManager
      Marks 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 to ServerManager.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:
      markServerForRestart in interface ServerManager
    • handleCrashAndGetExitCode

      public int handleCrashAndGetExitCode()
      Description copied from interface: ServerManager
      Handles 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:
      handleCrashAndGetExitCode in interface ServerManager
      Returns:
      the exit code if available, or -1 if the process is still running or unavailable
    • ensureRunning

      Description copied from interface: ServerManager
      Ensures 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:
      ensureRunning in interface ServerManager
      Throws:
      IOException - if the server cannot be started
      ServerInitializationException - if the server fails to initialize
      InterruptedException - if interrupted while waiting for server startup
      TimeoutException - if the server doesn't start within the configured timeout
    • connect

      public Socket connect(int socketTimeoutMs) throws IOException, ServerInitializationException
      Description copied from interface: ServerManager
      Establishes 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:
      connect in interface ServerManager
      Parameters:
      socketTimeoutMs - the socket timeout in milliseconds
      Returns:
      a connected Socket ready for communication
      Throws:
      IOException - if connection fails
      ServerInitializationException - if the server died before connecting
    • shutdown

      public void shutdown() throws InterruptedException
      Description copied from interface: ServerManager
      Shuts down the server process and cleans up resources. After calling this method, ServerManager.ensureRunning() can be called to restart.
      Specified by:
      shutdown in interface ServerManager
      Throws:
      InterruptedException - if interrupted while waiting for shutdown
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getServerSocket

      public ServerSocket getServerSocket()
      Returns the ServerSocket for accepting client connections. Used by PipesClient to accept the server's incoming connection.
      Returns:
      the server socket
    • getProcess

      public Process getProcess()
      Returns the server process.
      Returns:
      the process, or null if not started