@@ -91,8 +91,10 @@ public WorkRequestHandler(
91
91
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
92
92
* writing {@code WorkResponses} to the server.
93
93
* @param cpuUsageBeforeGc The minimum amount of CPU time between explicit garbage collection
94
- * calls.
94
+ * calls. Pass Duration.ZERO to not do explicit garbage collection.
95
+ * @deprecated Use WorkRequestHandlerBuilder instead.
95
96
*/
97
+ @ Deprecated ()
96
98
public WorkRequestHandler (
97
99
BiFunction <List <String >, PrintWriter , Integer > callback ,
98
100
PrintStream stderr ,
@@ -104,6 +106,48 @@ public WorkRequestHandler(
104
106
this .gcScheduler = new CpuTimeBasedGcScheduler (cpuUsageBeforeGc );
105
107
}
106
108
109
+ /** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
110
+ public static class WorkRequestHandlerBuilder {
111
+ private final BiFunction <List <String >, PrintWriter , Integer > callback ;
112
+ private final PrintStream stderr ;
113
+ private final WorkerMessageProcessor messageProcessor ;
114
+ private Duration cpuUsageBeforeGc = Duration .ZERO ;
115
+
116
+ /**
117
+ * Creates a {@code WorkRequestHandlerBuilder}.
118
+ *
119
+ * @param callback Callback method for executing a single WorkRequest in a thread. The first
120
+ * argument to {@code callback} is the set of command-line arguments, the second is where
121
+ * all error messages and other user-oriented messages should be written to. The callback
122
+ * must return an exit code indicating success (zero) or failure (nonzero).
123
+ * @param stderr Stream that log messages should be written to, typically the process' stderr.
124
+ * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
125
+ * and writing {@code WorkResponses} to the server.
126
+ */
127
+ public WorkRequestHandlerBuilder (
128
+ BiFunction <List <String >, PrintWriter , Integer > callback ,
129
+ PrintStream stderr ,
130
+ WorkerMessageProcessor messageProcessor ) {
131
+ this .callback = callback ;
132
+ this .stderr = stderr ;
133
+ this .messageProcessor = messageProcessor ;
134
+ }
135
+
136
+ /**
137
+ * Sets the minimum amount of CPU time between explicit garbage collection calls. Pass
138
+ * Duration.ZERO to not do explicit garbage collection (the default).
139
+ */
140
+ public WorkRequestHandlerBuilder setCpuUsageBeforeGc (Duration cpuUsageBeforeGc ) {
141
+ this .cpuUsageBeforeGc = cpuUsageBeforeGc ;
142
+ return this ;
143
+ }
144
+
145
+ /** Returns a WorkRequestHandler instance with the values in this Builder. */
146
+ public WorkRequestHandler build () {
147
+ return new WorkRequestHandler (callback , stderr , messageProcessor , cpuUsageBeforeGc );
148
+ }
149
+ }
150
+
107
151
/**
108
152
* Runs an infinite loop of reading {@link WorkRequest} from {@code in}, running the callback,
109
153
* then writing the corresponding {@link WorkResponse} to {@code out}. If there is an error
0 commit comments