Class JobAgentConnection

java.lang.Object
net.sf.basedb.util.jobagent.JobAgentConnection
All Implemented Interfaces:
AutoCloseable

public class JobAgentConnection
extends Object
implements AutoCloseable
This class is used by client applications to communicate with job agents. It used to send commands to job agents. For example, it can send start, stop and info commands.
Version:
2.0
Author:
nicklas
See Also:
JobAgentServerConnection
Last modified
$Date: 2018-12-12 15:41:10 +0100 (on, 12 dec 2018) $
  • Field Details

    • HEADER_REGEXP

      private static final Pattern HEADER_REGEXP
      Regexp used to parse a typical answer.
    • server

      private final String server
    • port

      private final int port
    • timeout

      private final int timeout
  • Constructor Details

    • JobAgentConnection

      public JobAgentConnection​(int port, int timeout)
      Create a connection to the local job agent running on the specified port.
      Parameters:
      port - The port the job agent is listening on
      timeout - The timeout in milliseconds
    • JobAgentConnection

      public JobAgentConnection​(String server, int port, int timeout)
      Create a connection to the job agent running on the specified server and listening on the specified port.
      Parameters:
      server - The server the job agent is running on, or null to connect to the local host
      port - The port the job agent is listening on
      timeout - The timeout in milliseconds
  • Method Details

    • parseAnswer

      public static Map<String,​List<String>> parseAnswer​(String answer)
      Utility method for parsing a 'typical' answer from a job agent. The typical answer is a string that contains a key-value pair on each line, separated by a colon. This method allows multiple entries for the same key.
      Parameters:
      answer - The answer
      Returns:
      A map with the key as index and a list with all values
      Since:
      2.16
    • parseSimpleAnswer

      public static Map<String,​String> parseSimpleAnswer​(String answer)
      Utility method for parsing a 'typical' answer from a job agent. The typical answer is a string that contains a key-value pair on each line, separated by a colon. This method allows only a single entry for the same key.
      Parameters:
      answer - The answer
      Returns:
      A map with the key as index to the value
      Since:
      2.16
    • sendPing

      public String sendPing() throws IOException
      Send the ping command to the job agent. The only answer to this command is OK.
      Returns:
      OK if successful
      Throws:
      IOException - If there is an error
    • sendStart

      public String sendStart() throws IOException
      Send a start request to the job agent.
      Returns:
      OK if successful
      Throws:
      IOException - If there is an error
    • sendStop

      public String sendStop() throws IOException
      Throws:
      IOException
    • sendPause

      public String sendPause() throws IOException
      Throws:
      IOException
    • getInfo

      public JobAgentInfo getInfo​(boolean full) throws IOException
      Send an info or status request to the job agent. The difference is that a status request doesn't return information about cpu, memory or executing jobs.
      Parameters:
      full - If TRUE a info request is sent, othwerwise a status request is sent
      Returns:
      A JobAgentInfo object containing the information
      Throws:
      IOException - If there is an error
    • close

      public void close()
      Close the connection to the job agent. In the current implementation all connections are stateless and this method does nothing. This might change in the future if a more advanced protocol is implemented. Clients should always call this method.
      Specified by:
      close in interface AutoCloseable
    • send

      public String send​(String cmd) throws IOException
      Send a remote control command to the job agent.
      Parameters:
      cmd - The command to send
      Returns:
      The answer
      Throws:
      IOException - If there is an error
      Since:
      2.16 (was private before that)
    • connect

      private Socket connect() throws IOException
      Create a socket connection to the job agent.
      Returns:
      The socket
      Throws:
      IOException
    • sendCmd

      private String sendCmd​(Socket socket, String cmd) throws IOException
      Send a command to the job agent and return the answer. If the answer is FAILED an exception will be thrown.
      Parameters:
      socket - The socket used for communicating with the job agent
      cmd - The command to send
      Returns:
      The answer from the job server
      Throws:
      IOException - If there is a communications error