~elambert/gearmanij/gearman_java_library

« back to all changes in this revision

Viewing changes to src/org/gearman/Worker.java

  • Committer: Eric Lambert
  • Date: 2009-07-07 02:18:15 UTC
  • mfrom: (57.1.65 gearmanij-trunk)
  • Revision ID: eric.d.lambert@gmail.com-20090707021815-0xbupi72ubyoa62a
merge from trunk. ReverseWorkerTest has been ignored, it was failing claiming that certain ops were not supported, since there is duplication in the worker code, for the time being will just ignore this issue, will resolve once the worker code has been straightened out

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * GNU Lesser General Public License (LGPL) version 2.1.
6
6
 * See the COPYING file in the parent directory for full text.
7
7
 */
8
 
package gearmanij;
9
 
 
10
 
import org.gearman.PacketType;
11
 
import gearmanij.util.IORuntimeException;
 
8
package org.gearman;
12
9
 
13
10
import java.util.EnumSet;
14
11
import java.util.List;
15
12
import java.util.Map;
16
13
 
 
14
import org.gearman.common.PacketType;
 
15
import org.gearman.util.IORuntimeException;
 
16
 
17
17
/**
18
18
 * A Worker grabs a {@link Job} from a job server, performs the
19
19
 * {@link JobFunction} specified on the data in the Job, and returns the results
22
22
 * results to the job server.
23
23
 */
24
24
public interface Worker {
25
 
  // These enums were copied over from the C library.
26
 
  public enum WorkerOption {
27
 
    NON_BLOCKING, PACKET_INIT, GRAB_JOB_IN_USE, PRE_SLEEP_IN_USE, WORK_JOB_IN_USE, CHANGE, GRAB_UNIQ
28
 
  }
29
 
 
30
 
  enum FunctionOption {
31
 
    PACKET_IN_USE, CHANGE, REMOVE
32
 
  }
33
 
 
34
 
  enum WorkerState {
35
 
    START, STATE_FUNCTION_SEND, STATE_CONNECT, STATE_GRAB_JOB_SEND, STATE_GRAB_JOB_RECV, STATE_PRE_SLEEP
36
 
  }
37
 
 
38
 
  enum WorkState {
39
 
    GRAB_JOB, FUNCTION, COMPLETE, FAIL
40
 
  }
41
 
 
42
 
  /**
43
 
   * Wait for a job and call the appropriate callback function when it gets one.
44
 
   */
45
 
  void work();
46
 
 
47
 
  /**
48
 
   * Clears all {@link WorkerOption}s.
49
 
   */
50
 
  void clearWorkerOptions();
51
 
 
52
 
  /**
53
 
   * Returns {@link java.util.EnumSet} of {@link WorkerOption}s.
54
 
   * 
55
 
   * @return EnumSet of WorkerOptions
56
 
   */
57
 
  EnumSet<WorkerOption> getWorkerOptions();
58
 
 
59
 
  /**
60
 
   * Removes each specified WorkerOption from the current set of Worker options.
61
 
   * 
62
 
   * @param workerOptions
63
 
   *          one or more WorkerOptions
64
 
   */
65
 
  void removeWorkerOptions(WorkerOption... workerOptions);
66
 
 
67
 
  /**
68
 
   * Adds each specified WorkerOption to the current set of Worker options. For
69
 
   * example,
70
 
   * <code>worker.setWorkerOptions(WorkerOption.NON_BLOCKING, WorkerOption.GRAB_JOB_IN_USE))</code>
71
 
   * 
72
 
   * @param workerOptions
73
 
   *          one or more WorkerOptions
74
 
   */
75
 
  void setWorkerOptions(WorkerOption... workerOptions);
76
 
 
77
 
  /**
78
 
   * Adds a {@link Connection} to a job server.
79
 
   * 
80
 
   * @param conn
81
 
   *          connection to a job server
82
 
   */
83
 
  void addServer(Connection conn);
84
 
 
85
 
  /**
86
 
   * Sends <code>text</code> to a job server with expectation of receiving the
87
 
   * same data echoed back.
88
 
   * 
89
 
   * @param text
90
 
   *          String to be echoed
91
 
   * @param conn
92
 
   *          connection to a job server
93
 
   * @throws IORuntimeException
94
 
   */
95
 
  String echo(String text, Connection conn);
96
 
 
97
 
  /**
98
 
   * Registers a JobFunction that a Worker can perform on a Job. If the worker
99
 
   * does not respond with a result within the given timeout period in seconds,
100
 
   * the job server will assume the work will not be performed by that worker
101
 
   * and will again make the work available to be performed by any worker
102
 
   * capable of performing this function.
103
 
   * 
104
 
   * @param function
105
 
   *          JobFunction for a function a Worker can perform
106
 
   * @param timeout
107
 
   *          time in seconds after job server will consider job to be abandoned
108
 
   */
109
 
  void registerFunction(JobFunction function, int timeout);
110
 
 
111
 
  /**
112
 
   * Registers a JobFunction that a Worker can perform on a Job.
113
 
   * 
114
 
   * @param function
115
 
   *          JobFunction for a function a Worker can perform
116
 
   */
117
 
  void registerFunction(JobFunction function);
118
 
 
119
 
  /**
120
 
   * Registers a JobFunction that a Worker can perform on a Job. If the worker
121
 
   * does not respond with a result within the given timeout period in seconds,
122
 
   * the job server will assume the work will not be performed by that worker
123
 
   * and will again make the work available to be performed by any worker
124
 
   * capable of performing this function.
125
 
   * 
126
 
   * @param function
127
 
   *          JobFunction Class for a function a Worker can perform
128
 
   * @param timeout
129
 
   *          time in seconds after job server will consider job to be abandoned
130
 
   */
131
 
  void registerFunction(Class<? extends JobFunction> function, int timeout);
132
 
 
133
 
  /**
134
 
   * Registers a JobFunction that a Worker can perform on a Job.
135
 
   * 
136
 
   * @param function
137
 
   *          JobFunction Class for a function a Worker can perform
138
 
   */
139
 
  void registerFunction(Class<? extends JobFunction> function);
140
 
 
141
 
  /**
142
 
   * Registers a JobFunctionFactory that a Worker will use to create a
143
 
   * JobFunction object to execute a Job.If the worker does not respond with a
144
 
   * result within the given timeout period in seconds, the job server will
145
 
   * assume the work will not be performed by that worker and will again make
146
 
   * the work available to be performed by any worker capable of performing this
147
 
   * function.
148
 
   * 
149
 
   * @param factory
150
 
   *          Factory that will be called to create a JobFunction instance for a
151
 
   *          function a Worker can perform
152
 
   * @param timeout
153
 
   *          time in seconds after job server will consider job to be abandoned
154
 
   */
155
 
  void registerFunctionFactory(JobFunctionFactory factory, int timeout);
156
 
 
157
 
  /**
158
 
   * Registers a JobFunctionFactory that a Worker will use to create a
159
 
   * JobFunction object to execute a Job.
160
 
   * 
161
 
   * @param factory
162
 
   *          Factory that will be called to create a JobFunction instance for a
163
 
   *          function a Worker can perform
164
 
   */
165
 
  void registerFunctionFactory(JobFunctionFactory factory);
166
 
 
167
 
  /**
168
 
   * Sets the worker ID in a job server so monitoring and reporting commands can
169
 
   * uniquely identify the connected workers.
170
 
   * 
171
 
   * @param id
172
 
   *          ID that job server should use for an instance of a worker
173
 
   */
174
 
  void setWorkerID(String id);
175
 
 
176
 
  /**
177
 
   * Sets the worker ID in a job server so monitoring and reporting commands can
178
 
   * uniquely identify the connected workers. If a different ID is set with each
179
 
   * job server, and connections can more easily be monitored and reported on
180
 
   * independently.
181
 
   * 
182
 
   * @param id
183
 
   *          ID that job server should use for an instance of a worker
184
 
   * @param conn
185
 
   *          connection to the job server
186
 
   */
187
 
  void setWorkerID(String id, Connection conn);
188
 
 
189
 
  /**
190
 
   * Unregisters with the Connection a function that a worker can perform on a
191
 
   * Job.
192
 
   * 
193
 
   * @param functionName
194
 
   *          Name for a function a Worker can no longer perform
195
 
   */
196
 
  void unregisterFunction(String functionName);
197
 
 
198
 
  /**
199
 
   * Unregisters all functions on all Connections.
200
 
   */
201
 
  void unregisterAll();
202
 
 
203
 
  /**
204
 
   * Attempts to grab and then execute a Job on each connection.
205
 
   * 
206
 
   * @return a Map indicating for each connection whether a Job was grabbed
207
 
   */
208
 
  Map<Connection, PacketType> grabJob();
209
 
 
210
 
  /**
211
 
   * Attempts to grab and then execute a Job on the specified connection.
212
 
   * 
213
 
   * @param conn
214
 
   *          connection to a job server
215
 
   * @return a PacketType indicating with a job was grabbed
216
 
   */
217
 
  PacketType grabJob(Connection conn);
218
 
 
219
 
  /**
220
 
   * Stops the work loop; requests to shutdown
221
 
   */
222
 
  void stop();
223
 
 
224
 
  /**
225
 
   * Stops the work loop and closes all open connections.
226
 
   * 
227
 
   * @return a List of any Exceptions thrown when closing connections
228
 
   */
229
 
  List<Exception> shutdown();
 
25
    // These enums were copied over from the C library.
 
26
    public enum WorkerOption {
 
27
        NON_BLOCKING, PACKET_INIT, GRAB_JOB_IN_USE, PRE_SLEEP_IN_USE, WORK_JOB_IN_USE, CHANGE, GRAB_UNIQ
 
28
    }
 
29
 
 
30
    enum FunctionOption {
 
31
        PACKET_IN_USE, CHANGE, REMOVE
 
32
    }
 
33
 
 
34
    enum WorkerState {
 
35
        START, STATE_FUNCTION_SEND, STATE_CONNECT, STATE_GRAB_JOB_SEND, STATE_GRAB_JOB_RECV, STATE_PRE_SLEEP
 
36
    }
 
37
 
 
38
    enum WorkState {
 
39
        GRAB_JOB, FUNCTION, COMPLETE, FAIL
 
40
    }
 
41
 
 
42
    /**
 
43
     * Wait for a job and call the appropriate callback function when it gets
 
44
     * one.
 
45
     */
 
46
    void work();
 
47
 
 
48
    /**
 
49
     * Clears all {@link WorkerOption}s.
 
50
     */
 
51
    void clearWorkerOptions();
 
52
 
 
53
    /**
 
54
     * Returns {@link java.util.EnumSet} of {@link WorkerOption}s.
 
55
     * 
 
56
     * @return EnumSet of WorkerOptions
 
57
     */
 
58
    EnumSet<WorkerOption> getWorkerOptions();
 
59
 
 
60
    /**
 
61
     * Removes each specified WorkerOption from the current set of Worker
 
62
     * options.
 
63
     * 
 
64
     * @param workerOptions
 
65
     *            one or more WorkerOptions
 
66
     */
 
67
    void removeWorkerOptions(WorkerOption... workerOptions);
 
68
 
 
69
    /**
 
70
     * Adds each specified WorkerOption to the current set of Worker options.
 
71
     * For example,
 
72
     * <code>worker.setWorkerOptions(WorkerOption.NON_BLOCKING, WorkerOption.GRAB_JOB_IN_USE))</code>
 
73
     * 
 
74
     * @param workerOptions
 
75
     *            one or more WorkerOptions
 
76
     */
 
77
    void setWorkerOptions(WorkerOption... workerOptions);
 
78
 
 
79
    /**
 
80
     * Adds a {@link PacketConnection} to a job server.
 
81
     * 
 
82
     * @param conn
 
83
     *            connection to a job server
 
84
     */
 
85
    void addServer(PacketConnection conn);
 
86
 
 
87
    /**
 
88
     * Sends <code>text</code> to a job server with expectation of receiving the
 
89
     * same data echoed back.
 
90
     * 
 
91
     * @param text
 
92
     *            String to be echoed
 
93
     * @param conn
 
94
     *            connection to a job server
 
95
     * @throws IORuntimeException
 
96
     */
 
97
    String echo(String text, PacketConnection conn);
 
98
 
 
99
    /**
 
100
     * Registers a JobFunction that a Worker can perform on a Job. If the worker
 
101
     * does not respond with a result within the given timeout period in
 
102
     * seconds, the job server will assume the work will not be performed by
 
103
     * that worker and will again make the work available to be performed by any
 
104
     * worker capable of performing this function.
 
105
     * 
 
106
     * @param function
 
107
     *            JobFunction for a function a Worker can perform
 
108
     * @param timeout
 
109
     *            time in seconds after job server will consider job to be
 
110
     *            abandoned
 
111
     */
 
112
    void registerFunction(JobFunction function, int timeout);
 
113
 
 
114
    /**
 
115
     * Registers a JobFunction that a Worker can perform on a Job.
 
116
     * 
 
117
     * @param function
 
118
     *            JobFunction for a function a Worker can perform
 
119
     */
 
120
    void registerFunction(JobFunction function);
 
121
 
 
122
    /**
 
123
     * Registers a JobFunction that a Worker can perform on a Job. If the worker
 
124
     * does not respond with a result within the given timeout period in
 
125
     * seconds, the job server will assume the work will not be performed by
 
126
     * that worker and will again make the work available to be performed by any
 
127
     * worker capable of performing this function.
 
128
     * 
 
129
     * @param function
 
130
     *            JobFunction Class for a function a Worker can perform
 
131
     * @param timeout
 
132
     *            time in seconds after job server will consider job to be
 
133
     *            abandoned
 
134
     */
 
135
    void registerFunction(Class<? extends JobFunction> function, int timeout);
 
136
 
 
137
    /**
 
138
     * Registers a JobFunction that a Worker can perform on a Job.
 
139
     * 
 
140
     * @param function
 
141
     *            JobFunction Class for a function a Worker can perform
 
142
     */
 
143
    void registerFunction(Class<? extends JobFunction> function);
 
144
 
 
145
    /**
 
146
     * Registers a JobFunctionFactory that a Worker will use to create a
 
147
     * JobFunction object to execute a Job.If the worker does not respond with a
 
148
     * result within the given timeout period in seconds, the job server will
 
149
     * assume the work will not be performed by that worker and will again make
 
150
     * the work available to be performed by any worker capable of performing
 
151
     * this function.
 
152
     * 
 
153
     * @param factory
 
154
     *            Factory that will be called to create a JobFunction instance
 
155
     *            for a function a Worker can perform
 
156
     * @param timeout
 
157
     *            time in seconds after job server will consider job to be
 
158
     *            abandoned
 
159
     */
 
160
    void registerFunctionFactory(JobFunctionFactory factory, int timeout);
 
161
 
 
162
    /**
 
163
     * Registers a JobFunctionFactory that a Worker will use to create a
 
164
     * JobFunction object to execute a Job.
 
165
     * 
 
166
     * @param factory
 
167
     *            Factory that will be called to create a JobFunction instance
 
168
     *            for a function a Worker can perform
 
169
     */
 
170
    void registerFunctionFactory(JobFunctionFactory factory);
 
171
 
 
172
    /**
 
173
     * Sets the worker ID in a job server so monitoring and reporting commands
 
174
     * can uniquely identify the connected workers.
 
175
     * 
 
176
     * @param id
 
177
     *            ID that job server should use for an instance of a worker
 
178
     */
 
179
    void setWorkerID(String id);
 
180
 
 
181
    /**
 
182
     * Sets the worker ID in a job server so monitoring and reporting commands
 
183
     * can uniquely identify the connected workers. If a different ID is set
 
184
     * with each job server, and connections can more easily be monitored and
 
185
     * reported on independently.
 
186
     * 
 
187
     * @param id
 
188
     *            ID that job server should use for an instance of a worker
 
189
     * @param conn
 
190
     *            connection to the job server
 
191
     */
 
192
    void setWorkerID(String id, PacketConnection conn);
 
193
 
 
194
    /**
 
195
     * Unregisters with the Connection a function that a worker can perform on a
 
196
     * Job.
 
197
     * 
 
198
     * @param functionName
 
199
     *            Name for a function a Worker can no longer perform
 
200
     */
 
201
    void unregisterFunction(String functionName);
 
202
 
 
203
    /**
 
204
     * Unregisters all functions on all Connections.
 
205
     */
 
206
    void unregisterAll();
 
207
 
 
208
    /**
 
209
     * Attempts to grab and then execute a Job on each connection.
 
210
     * 
 
211
     * @return a Map indicating for each connection whether a Job was grabbed
 
212
     */
 
213
    Map<PacketConnection, PacketType> grabJob();
 
214
 
 
215
    /**
 
216
     * Attempts to grab and then execute a Job on the specified connection.
 
217
     * 
 
218
     * @param conn
 
219
     *            connection to a job server
 
220
     * @return a PacketType indicating with a job was grabbed
 
221
     */
 
222
    PacketType grabJob(PacketConnection conn);
 
223
 
 
224
    /**
 
225
     * Stops the work loop; requests to shutdown
 
226
     */
 
227
    void stop();
 
228
 
 
229
    /**
 
230
     * Stops the work loop and closes all open connections.
 
231
     * 
 
232
     * @return a List of any Exceptions thrown when closing connections
 
233
     */
 
234
    List<Exception> shutdown();
230
235
 
231
236
}