5959
6060/**
6161 * 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()}.
6265 *
6366 * @author Miroslav Fuksa
6467 */
6568final class MonitoringStatisticsImpl implements MonitoringStatistics {
6669
6770 /**
6871 * 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>
6989 */
7090 static class Builder {
7191
@@ -90,7 +110,7 @@ public int compare(final Class<?> o1, final Class<?> o2) {
90110 }
91111 });
92112
93- private ExecutionStatisticsImpl .Builder requestStatisticsBuilder ;
113+ private ExecutionStatisticsImpl .Builder executionStatisticsBuilder ;
94114
95115 /**
96116 * Create a new builder.
@@ -102,6 +122,7 @@ public int compare(final Class<?> o1, final Class<?> o2) {
102122
103123 /**
104124 * Create a new builder and initialize it from resource model.
125+ *
105126 * @param resourceModel resource model.
106127 */
107128 Builder (final ResourceModel resourceModel ) {
@@ -143,6 +164,7 @@ private ResourceStatisticsImpl.Builder getOrCreateResourceBuilder(final Resource
143164
144165 /**
145166 * Get the exception mapper statistics builder.
167+ *
146168 * @return Builder of internal exception mapper statistics.
147169 */
148170 ExceptionMapperStatisticsImpl .Builder getExceptionMapperStatisticsBuilder () {
@@ -153,26 +175,25 @@ ExceptionMapperStatisticsImpl.Builder getExceptionMapperStatisticsBuilder() {
153175 * Add global request execution.
154176 *
155177 * @param startTime time of the execution.
156- * @param duration duration of the execution.
178+ * @param duration duration of the execution.
157179 */
158180 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 ();
161183 }
162- requestStatisticsBuilder .addExecution (startTime , duration );
184+ executionStatisticsBuilder .addExecution (startTime , duration );
163185 }
164186
165187 /**
166188 * Add execution of a resource method.
167189 *
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.
176197 */
177198 void addExecution (final String uri , final ResourceMethod resourceMethod ,
178199 final long methodTime , final long methodDuration ,
@@ -194,18 +215,18 @@ void addExecution(final String uri, final ResourceMethod resourceMethod,
194215 .addResourceMethodExecution (methodTime , methodDuration , requestTime , requestDuration );
195216 }
196217
197-
198218 /**
199219 * Add a response status code produces by Jersey.
220+ *
200221 * @param responseCode Response status code.
201222 */
202223 void addResponseCode (final int responseCode ) {
203224 responseStatisticsBuilder .addResponseCode (responseCode );
204225 }
205226
206-
207227 /**
208228 * Build a new instance of monitoring statistics.
229+ *
209230 * @return New instance of {@code MonitoringStatisticsImpl}.
210231 */
211232 MonitoringStatisticsImpl build () {
@@ -214,8 +235,8 @@ MonitoringStatisticsImpl build() {
214235 final Map <Class <?>, ResourceStatistics > classStats = Collections .unmodifiableMap (
215236 Maps .transformValues (resourceClassStatistics , BUILDING_FUNCTION ));
216237
217- final ExecutionStatistics requestStats = requestStatisticsBuilder == null
218- ? ExecutionStatisticsImpl .EMPTY : requestStatisticsBuilder .build ();
238+ final ExecutionStatistics requestStats = executionStatisticsBuilder == null
239+ ? ExecutionStatisticsImpl .EMPTY : executionStatisticsBuilder .build ();
219240
220241 return new MonitoringStatisticsImpl (
221242 uriStats , classStats , requestStats ,
@@ -230,7 +251,6 @@ MonitoringStatisticsImpl build() {
230251 private final Map <String , ResourceStatistics > uriStatistics ;
231252 private final Map <Class <?>, ResourceStatistics > resourceClassStatistics ;
232253
233-
234254 private MonitoringStatisticsImpl (final Map <String , ResourceStatistics > uriStatistics ,
235255 final Map <Class <?>, ResourceStatistics > resourceClassStatistics ,
236256 final ExecutionStatistics requestStatistics ,
@@ -243,24 +263,31 @@ private MonitoringStatisticsImpl(final Map<String, ResourceStatistics> uriStatis
243263 this .exceptionMapperStatistics = exceptionMapperStatistics ;
244264 }
245265
246-
247266 @ Override
248267 public ExecutionStatistics getRequestStatistics () {
249268 return requestStatistics ;
250269 }
251270
252-
253271 @ Override
254272 public ResponseStatistics getResponseStatistics () {
255273 return responseStatistics ;
256274 }
257275
258-
276+ /**
277+ * Refreshed (re-built) on every access. (uses {@link Maps#transformValues(Map, Function)})
278+ *
279+ * @return resource statistics
280+ */
259281 @ Override
260282 public Map <String , ResourceStatistics > getUriStatistics () {
261283 return uriStatistics ;
262284 }
263285
286+ /**
287+ * Refreshed (re-built) on every access. (uses {@link Maps#transformValues(Map, Function)})
288+ *
289+ * @return resource statistics
290+ */
264291 @ Override
265292 public Map <Class <?>, ResourceStatistics > getResourceClassStatistics () {
266293 return resourceClassStatistics ;
@@ -273,7 +300,8 @@ public ExceptionMapperStatistics getExceptionMapperStatistics() {
273300
274301 @ Override
275302 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
277305 return this ;
278306 }
279307}
0 commit comments