Package net.sf.basedb.util.jobagent
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 sendstart
,stop
andinfo
commands.- Version:
- 2.0
- Author:
- nicklas
- See Also:
JobAgentServerConnection
- Last modified
- $Date: 2018-12-12 15:41:10 +0100 (on, 12 dec 2018) $
-
-
Constructor Summary
Constructors Constructor Description JobAgentConnection(int port, int timeout)
Create a connection to the local job agent running on the specified port.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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the connection to the job agent.private Socket
connect()
Create a socket connection to the job agent.JobAgentInfo
getInfo(boolean full)
Send aninfo
orstatus
request to the job agent.static Map<String,List<String>>
parseAnswer(String answer)
Utility method for parsing a 'typical' answer from a job agent.static Map<String,String>
parseSimpleAnswer(String answer)
Utility method for parsing a 'typical' answer from a job agent.String
send(String cmd)
Send a remote control command to the job agent.private String
sendCmd(Socket socket, String cmd)
Send a command to the job agent and return the answer.String
sendPause()
String
sendPing()
Send theping
command to the job agent.String
sendStart()
Send astart
request to the job agent.String
sendStop()
-
-
-
Constructor Detail
-
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 ontimeout
- 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 hostport
- The port the job agent is listening ontimeout
- The timeout in milliseconds
-
-
Method Detail
-
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 theping
command to the job agent. The only answer to this command isOK
.- Returns:
- OK if successful
- Throws:
IOException
- If there is an error
-
sendStart
public String sendStart() throws IOException
Send astart
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 aninfo
orstatus
request to the job agent. The difference is that astatus
request doesn't return information about cpu, memory or executing jobs.- Parameters:
full
- If TRUE ainfo
request is sent, othwerwise astatus
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 interfaceAutoCloseable
-
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 isFAILED
an exception will be thrown.- Parameters:
socket
- The socket used for communicating with the job agentcmd
- The command to send- Returns:
- The answer from the job server
- Throws:
IOException
- If there is a communications error
-
-