@@ -106,7 +106,7 @@ synchronized void addOutput(String s) {
106
106
final ConcurrentMap <Integer , RequestInfo > activeRequests = new ConcurrentHashMap <>();
107
107
108
108
/** The function to be called after each {@link WorkRequest} is read. */
109
- private final BiFunction < List < String >, PrintWriter , Integer > callback ;
109
+ private final WorkRequestCallback callback ;
110
110
111
111
/** This worker's stderr. */
112
112
private final PrintStream stderr ;
@@ -129,6 +129,7 @@ synchronized void addOutput(String s) {
129
129
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
130
130
* writing {@code WorkResponses} to the server.
131
131
*/
132
+ @ Deprecated
132
133
public WorkRequestHandler (
133
134
BiFunction <List <String >, PrintWriter , Integer > callback ,
134
135
PrintStream stderr ,
@@ -163,23 +164,69 @@ public WorkRequestHandler(
163
164
/**
164
165
* Creates a {@code WorkRequestHandler} that will call {@code callback} for each WorkRequest
165
166
* received. Only used for the Builder.
167
+ *
168
+ * @deprecated Use WorkRequestHandlerBuilder instead.
166
169
*/
170
+ @ Deprecated
167
171
private WorkRequestHandler (
168
172
BiFunction <List <String >, PrintWriter , Integer > callback ,
169
173
PrintStream stderr ,
170
174
WorkerMessageProcessor messageProcessor ,
171
175
Duration cpuUsageBeforeGc ,
172
176
BiConsumer <Integer , Thread > cancelCallback ) {
177
+ this (
178
+ new WorkRequestCallback ((request , pw ) -> callback .apply (request .getArgumentsList (), pw )),
179
+ stderr ,
180
+ messageProcessor ,
181
+ cpuUsageBeforeGc ,
182
+ cancelCallback );
183
+ }
184
+
185
+ /**
186
+ * Creates a {@code WorkRequestHandler} that will call {@code callback} for each WorkRequest
187
+ * received. Only used for the Builder.
188
+ *
189
+ * @param callback WorkRequestCallback object with Callback method for executing a single
190
+ * WorkRequest in a thread. The first argument to {@code callback} is the WorkRequest, the
191
+ * second is where all error messages and other user-oriented messages should be written to.
192
+ * The callback must return an exit code indicating success (zero) or failure (nonzero).
193
+ */
194
+ private WorkRequestHandler (
195
+ WorkRequestCallback callback ,
196
+ PrintStream stderr ,
197
+ WorkerMessageProcessor messageProcessor ,
198
+ Duration cpuUsageBeforeGc ,
199
+ BiConsumer <Integer , Thread > cancelCallback ) {
173
200
this .callback = callback ;
174
201
this .stderr = stderr ;
175
202
this .messageProcessor = messageProcessor ;
176
203
this .gcScheduler = new CpuTimeBasedGcScheduler (cpuUsageBeforeGc );
177
204
this .cancelCallback = cancelCallback ;
178
205
}
179
206
207
+ /** A wrapper class for the callback BiFunction */
208
+ public static class WorkRequestCallback {
209
+
210
+ /**
211
+ * Callback method for executing a single WorkRequest in a thread. The first argument to {@code
212
+ * callback} is the WorkRequest, the second is where all error messages and other user-oriented
213
+ * messages should be written to. The callback must return an exit code indicating success
214
+ * (zero) or failure (nonzero).
215
+ */
216
+ private final BiFunction <WorkRequest , PrintWriter , Integer > callback ;
217
+
218
+ public WorkRequestCallback (BiFunction <WorkRequest , PrintWriter , Integer > callback ) {
219
+ this .callback = callback ;
220
+ }
221
+
222
+ public Integer apply (WorkRequest workRequest , PrintWriter printWriter ) {
223
+ return callback .apply (workRequest , printWriter );
224
+ }
225
+ }
226
+
180
227
/** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
181
228
public static class WorkRequestHandlerBuilder {
182
- private final BiFunction < List < String >, PrintWriter , Integer > callback ;
229
+ private final WorkRequestCallback callback ;
183
230
private final PrintStream stderr ;
184
231
private final WorkerMessageProcessor messageProcessor ;
185
232
private Duration cpuUsageBeforeGc = Duration .ZERO ;
@@ -195,11 +242,32 @@ public static class WorkRequestHandlerBuilder {
195
242
* @param stderr Stream that log messages should be written to, typically the process' stderr.
196
243
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
197
244
* and writing {@code WorkResponses} to the server.
245
+ * @deprecated use WorkRequestHandlerBuilder with WorkRequestCallback instead
198
246
*/
247
+ @ Deprecated
199
248
public WorkRequestHandlerBuilder (
200
249
BiFunction <List <String >, PrintWriter , Integer > callback ,
201
250
PrintStream stderr ,
202
251
WorkerMessageProcessor messageProcessor ) {
252
+ this (
253
+ new WorkRequestCallback ((request , pw ) -> callback .apply (request .getArgumentsList (), pw )),
254
+ stderr ,
255
+ messageProcessor );
256
+ }
257
+
258
+ /**
259
+ * Creates a {@code WorkRequestHandlerBuilder}.
260
+ *
261
+ * @param callback WorkRequestCallback object with Callback method for executing a single
262
+ * WorkRequest in a thread. The first argument to {@code callback} is the WorkRequest, the
263
+ * second is where all error messages and other user-oriented messages should be written to.
264
+ * The callback must return an exit code indicating success (zero) or failure (nonzero).
265
+ * @param stderr Stream that log messages should be written to, typically the process' stderr.
266
+ * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
267
+ * and writing {@code WorkResponses} to the server.
268
+ */
269
+ public WorkRequestHandlerBuilder (
270
+ WorkRequestCallback callback , PrintStream stderr , WorkerMessageProcessor messageProcessor ) {
203
271
this .callback = callback ;
204
272
this .stderr = stderr ;
205
273
this .messageProcessor = messageProcessor ;
@@ -287,7 +355,7 @@ void respondToRequest(WorkRequest request, RequestInfo requestInfo) throws IOExc
287
355
PrintWriter pw = new PrintWriter (sw )) {
288
356
int exitCode ;
289
357
try {
290
- exitCode = callback .apply (request . getArgumentsList () , pw );
358
+ exitCode = callback .apply (request , pw );
291
359
} catch (RuntimeException e ) {
292
360
e .printStackTrace (pw );
293
361
exitCode = 1 ;
0 commit comments