59
59
60
60
/**
61
61
* Monitoring statistics implementation.
62
+ * <p/>
63
+ * This object is loosely immutable (i.e., {@link #getResourceClassStatistics()} and {@link #getUriStatistics()} gets updated on
64
+ * access). As a result, it is unnecessary to call {@link #snapshot()}.
62
65
*
63
66
* @author Miroslav Fuksa
64
67
*/
65
68
final class MonitoringStatisticsImpl implements MonitoringStatistics {
66
69
67
70
/**
68
71
* Builder of monitoring statistics.
72
+ * <p/>
73
+ * This builder does not need to be threadsafe as it's only accessed by jersey-background-task-scheduler. However, {@link
74
+ * #BUILDING_FUNCTION} is triggered when it is accessed (e.g., by servlet-container thread-pool threads) which adds threadsafe
75
+ * constraint on some of the sub-builders.
76
+ * <p/>
77
+ * Sub-Builders that require thread-safety
78
+ * <pre><ul>
79
+ * <li>{@link org.glassfish.jersey.server.internal.monitoring.ExecutionStatisticsImpl.Builder}</li>
80
+ * <li>{@link org.glassfish.jersey.server.internal.monitoring.ResourceStatisticsImpl.Builder}</li>
81
+ * <li>{@link org.glassfish.jersey.server.internal.monitoring.ResourceMethodStatisticsImpl.Builder}</li>
82
+ * <li>{@link org.glassfish.jersey.server.internal.monitoring.TimeWindowStatisticsImpl.Builder}</li>
83
+ * </ul>
84
+ * The rest does not need to be thread-safe
85
+ * <ul>
86
+ * <li>{@link org.glassfish.jersey.server.internal.monitoring.ExceptionMapperStatisticsImpl.Builder}</li>
87
+ * <li>{@link org.glassfish.jersey.server.internal.monitoring.ResponseStatisticsImpl.Builder}</li>
88
+ * </ul></pre>
69
89
*/
70
90
static class Builder {
71
91
@@ -90,7 +110,7 @@ public int compare(final Class<?> o1, final Class<?> o2) {
90
110
}
91
111
});
92
112
93
- private ExecutionStatisticsImpl .Builder requestStatisticsBuilder ;
113
+ private ExecutionStatisticsImpl .Builder executionStatisticsBuilder ;
94
114
95
115
/**
96
116
* Create a new builder.
@@ -102,6 +122,7 @@ public int compare(final Class<?> o1, final Class<?> o2) {
102
122
103
123
/**
104
124
* Create a new builder and initialize it from resource model.
125
+ *
105
126
* @param resourceModel resource model.
106
127
*/
107
128
Builder (final ResourceModel resourceModel ) {
@@ -143,6 +164,7 @@ private ResourceStatisticsImpl.Builder getOrCreateResourceBuilder(final Resource
143
164
144
165
/**
145
166
* Get the exception mapper statistics builder.
167
+ *
146
168
* @return Builder of internal exception mapper statistics.
147
169
*/
148
170
ExceptionMapperStatisticsImpl .Builder getExceptionMapperStatisticsBuilder () {
@@ -153,26 +175,25 @@ ExceptionMapperStatisticsImpl.Builder getExceptionMapperStatisticsBuilder() {
153
175
* Add global request execution.
154
176
*
155
177
* @param startTime time of the execution.
156
- * @param duration duration of the execution.
178
+ * @param duration duration of the execution.
157
179
*/
158
180
void addRequestExecution (final long startTime , final long duration ) {
159
- if (requestStatisticsBuilder == null ) {
160
- requestStatisticsBuilder = new ExecutionStatisticsImpl .Builder ();
181
+ if (executionStatisticsBuilder == null ) {
182
+ executionStatisticsBuilder = new ExecutionStatisticsImpl .Builder ();
161
183
}
162
- requestStatisticsBuilder .addExecution (startTime , duration );
184
+ executionStatisticsBuilder .addExecution (startTime , duration );
163
185
}
164
186
165
187
/**
166
188
* Add execution of a resource method.
167
189
*
168
- * @param uri String uri which was executed.
169
- * @param resourceMethod Resource method.
170
- * @param methodTime Time spent on execution of resource method itself (Unix timestamp format).
171
- * @param methodDuration Time of execution of the resource method.
172
- * @param requestTime Time of whole request processing (from receiving
173
- * the request until writing the response). (Unix timestamp format)
174
- * @param requestDuration Time when the request matching to the executed resource method has been received
175
- * by Jersey.
190
+ * @param uri String uri which was executed.
191
+ * @param resourceMethod Resource method.
192
+ * @param methodTime Time spent on execution of resource method itself (Unix timestamp format).
193
+ * @param methodDuration Time of execution of the resource method.
194
+ * @param requestTime Time of whole request processing (from receiving the request until writing the response). (Unix
195
+ * timestamp format)
196
+ * @param requestDuration Time when the request matching to the executed resource method has been received by Jersey.
176
197
*/
177
198
void addExecution (final String uri , final ResourceMethod resourceMethod ,
178
199
final long methodTime , final long methodDuration ,
@@ -194,18 +215,18 @@ void addExecution(final String uri, final ResourceMethod resourceMethod,
194
215
.addResourceMethodExecution (methodTime , methodDuration , requestTime , requestDuration );
195
216
}
196
217
197
-
198
218
/**
199
219
* Add a response status code produces by Jersey.
220
+ *
200
221
* @param responseCode Response status code.
201
222
*/
202
223
void addResponseCode (final int responseCode ) {
203
224
responseStatisticsBuilder .addResponseCode (responseCode );
204
225
}
205
226
206
-
207
227
/**
208
228
* Build a new instance of monitoring statistics.
229
+ *
209
230
* @return New instance of {@code MonitoringStatisticsImpl}.
210
231
*/
211
232
MonitoringStatisticsImpl build () {
@@ -214,8 +235,8 @@ MonitoringStatisticsImpl build() {
214
235
final Map <Class <?>, ResourceStatistics > classStats = Collections .unmodifiableMap (
215
236
Maps .transformValues (resourceClassStatistics , BUILDING_FUNCTION ));
216
237
217
- final ExecutionStatistics requestStats = requestStatisticsBuilder == null
218
- ? ExecutionStatisticsImpl .EMPTY : requestStatisticsBuilder .build ();
238
+ final ExecutionStatistics requestStats = executionStatisticsBuilder == null
239
+ ? ExecutionStatisticsImpl .EMPTY : executionStatisticsBuilder .build ();
219
240
220
241
return new MonitoringStatisticsImpl (
221
242
uriStats , classStats , requestStats ,
@@ -230,7 +251,6 @@ MonitoringStatisticsImpl build() {
230
251
private final Map <String , ResourceStatistics > uriStatistics ;
231
252
private final Map <Class <?>, ResourceStatistics > resourceClassStatistics ;
232
253
233
-
234
254
private MonitoringStatisticsImpl (final Map <String , ResourceStatistics > uriStatistics ,
235
255
final Map <Class <?>, ResourceStatistics > resourceClassStatistics ,
236
256
final ExecutionStatistics requestStatistics ,
@@ -243,24 +263,31 @@ private MonitoringStatisticsImpl(final Map<String, ResourceStatistics> uriStatis
243
263
this .exceptionMapperStatistics = exceptionMapperStatistics ;
244
264
}
245
265
246
-
247
266
@ Override
248
267
public ExecutionStatistics getRequestStatistics () {
249
268
return requestStatistics ;
250
269
}
251
270
252
-
253
271
@ Override
254
272
public ResponseStatistics getResponseStatistics () {
255
273
return responseStatistics ;
256
274
}
257
275
258
-
276
+ /**
277
+ * Refreshed (re-built) on every access. (uses {@link Maps#transformValues(Map, Function)})
278
+ *
279
+ * @return resource statistics
280
+ */
259
281
@ Override
260
282
public Map <String , ResourceStatistics > getUriStatistics () {
261
283
return uriStatistics ;
262
284
}
263
285
286
+ /**
287
+ * Refreshed (re-built) on every access. (uses {@link Maps#transformValues(Map, Function)})
288
+ *
289
+ * @return resource statistics
290
+ */
264
291
@ Override
265
292
public Map <Class <?>, ResourceStatistics > getResourceClassStatistics () {
266
293
return resourceClassStatistics ;
@@ -273,7 +300,8 @@ public ExceptionMapperStatistics getExceptionMapperStatistics() {
273
300
274
301
@ Override
275
302
public MonitoringStatistics snapshot () {
276
- // snapshot functionality not yet implemented
303
+ // snapshot is not needed, this object is loosely immutable (see javadoc of Maps getters)
304
+ // all the other Statistics objects are immutable
277
305
return this ;
278
306
}
279
307
}
0 commit comments