~elambert/gearmanij/gearman_java_library

« back to all changes in this revision

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

  • Committer: Eric Lambert
  • Date: 2009-07-10 04:07:39 UTC
  • Revision ID: eric.d.lambert@gmail.com-20090710040739-ru4tgzfcnuo9b9uc
removed unused worker and job

Show diffs side-by-side

added added

removed removed

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