Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit b0f03e2

Browse files
Marek PotočiarGerrit Code Review
authored andcommitted
Merge "J-146: Lower the footprint of Jersey Monitoring for apps that are not used J-138: ExtendedMonitoringStatisticsListener is never called J-154: NPE in TimeWindowStatisticsImpl during WLS shutdown"
2 parents dd10bdd + 723f379 commit b0f03e2

File tree

25 files changed

+1020
-243
lines changed

25 files changed

+1020
-243
lines changed

core-server/src/main/java/org/glassfish/jersey/server/ServerProperties.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,21 @@ public final class ServerProperties {
488488
*/
489489
public static final String MONITORING_STATISTICS_MBEANS_ENABLED = "jersey.config.server.monitoring.statistics.mbeans.enabled";
490490

491+
/**
492+
* Interval (in {@code ms}) indicating how often will be monitoring statistics refreshed and
493+
* {@link org.glassfish.jersey.server.monitoring.MonitoringStatisticsListener#onStatistics(org.glassfish.jersey.server.monitoring.MonitoringStatistics) onStatistics}
494+
* method called.
495+
* <p/>
496+
* The default value is {@code 500}.
497+
* <p/>
498+
* The name of the configuration property is <tt>{@value}</tt>.
499+
* <p/>
500+
*
501+
* @since 2.10
502+
*/
503+
public static final String MONITORING_STATISTICS_REFRESH_INTERVAL =
504+
"jersey.config.server.monitoring.statistics.refresh.interval";
505+
491506
/**
492507
* {@link String} property that defines the application name.
493508
*

core-server/src/main/java/org/glassfish/jersey/server/internal/monitoring/ApplicationStatisticsImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
*
5151
* @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
5252
*/
53-
class ApplicationStatisticsImpl implements ApplicationStatistics {
53+
final class ApplicationStatisticsImpl implements ApplicationStatistics {
54+
5455
private final ResourceConfig resourceConfig;
5556
private final Date startTime;
5657
private final Set<Class<?>> registeredClasses;
@@ -59,15 +60,15 @@ class ApplicationStatisticsImpl implements ApplicationStatistics {
5960

6061
/**
6162
* Create a new application statistics instance.
63+
*
6264
* @param resourceConfig Resource config of the application being monitored.
6365
* @param startTime Start time of the application (when initialization was finished).
6466
* @param registeredClasses Registered resource classes.
6567
* @param registeredInstances Registered resource instances.
6668
* @param providers Registered providers.
6769
*/
68-
ApplicationStatisticsImpl(ResourceConfig resourceConfig, Date startTime,
69-
Set<Class<?>> registeredClasses,
70-
Set<Object> registeredInstances, Set<Class<?>> providers) {
70+
ApplicationStatisticsImpl(final ResourceConfig resourceConfig, final Date startTime, final Set<Class<?>> registeredClasses,
71+
final Set<Object> registeredInstances, final Set<Class<?>> providers) {
7172
this.resourceConfig = resourceConfig;
7273
this.startTime = startTime;
7374

core-server/src/main/java/org/glassfish/jersey/server/internal/monitoring/ExceptionMapperStatisticsImpl.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,29 @@
5151
*
5252
* @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
5353
*/
54-
class ExceptionMapperStatisticsImpl implements ExceptionMapperStatistics {
54+
final class ExceptionMapperStatisticsImpl implements ExceptionMapperStatistics {
5555

5656
/**
5757
* Builder of exception mapper statistics.
5858
*/
5959
static class Builder {
60+
6061
private Map<Class<?>, Long> exceptionMapperExecutionCount = Maps.newHashMap();
6162
private long successfulMappings;
6263
private long unsuccessfulMappings;
6364
private long totalMappings;
6465

66+
private ExceptionMapperStatisticsImpl cached;
67+
6568
/**
6669
* Add mappings.
70+
*
6771
* @param success True if mappings were successful.
6872
* @param count Number of mappings.
6973
*/
70-
void addMapping(boolean success, int count) {
74+
void addMapping(final boolean success, final int count) {
75+
cached = null;
76+
7177
totalMappings++;
7278
if (success) {
7379
successfulMappings += count;
@@ -78,43 +84,46 @@ void addMapping(boolean success, int count) {
7884

7985
/**
8086
* Add an execution of exception mapper.
87+
*
8188
* @param mapper Exception mapper.
8289
* @param count Number of executions of the {@code mapper}.
8390
*/
84-
void addExceptionMapperExecution(Class<?> mapper, int count) {
91+
void addExceptionMapperExecution(final Class<?> mapper, final int count) {
92+
cached = null;
93+
8594
Long cnt = exceptionMapperExecutionCount.get(mapper);
8695
cnt = cnt == null ? count : cnt + count;
8796
exceptionMapperExecutionCount.put(mapper, cnt);
8897
}
8998

90-
9199
/**
92100
* Build an instance of exception mapper statistics.
93101
*
94102
* @return New instance of exception mapper statistics.
95103
*/
96104
public ExceptionMapperStatisticsImpl build() {
97-
return new ExceptionMapperStatisticsImpl(Collections.unmodifiableMap(exceptionMapperExecutionCount),
98-
successfulMappings, unsuccessfulMappings, totalMappings);
105+
if (cached == null) {
106+
cached = new ExceptionMapperStatisticsImpl(Collections.unmodifiableMap(exceptionMapperExecutionCount),
107+
successfulMappings, unsuccessfulMappings, totalMappings);
108+
}
109+
110+
return cached;
99111
}
100112
}
101113

102-
103114
private final Map<Class<?>, Long> exceptionMapperExecutionCount;
104115
private final long successfulMappings;
105116
private final long unsuccessfulMappings;
106117
private final long totalMappings;
107118

108-
109-
private ExceptionMapperStatisticsImpl(Map<Class<?>, Long> exceptionMapperExecutionCount,
110-
long successfulMappings, long unsuccessfulMappings, long totalMappings) {
119+
private ExceptionMapperStatisticsImpl(final Map<Class<?>, Long> exceptionMapperExecutionCount, final long successfulMappings,
120+
final long unsuccessfulMappings, final long totalMappings) {
111121
this.exceptionMapperExecutionCount = exceptionMapperExecutionCount;
112122
this.successfulMappings = successfulMappings;
113123
this.unsuccessfulMappings = unsuccessfulMappings;
114124
this.totalMappings = totalMappings;
115125
}
116126

117-
118127
@Override
119128
public Map<Class<?>, Long> getExceptionMapperExecutions() {
120129
return exceptionMapperExecutionCount;
@@ -141,5 +150,4 @@ public ExceptionMapperStatistics snapshot() {
141150
return this;
142151
}
143152

144-
145153
}

core-server/src/main/java/org/glassfish/jersey/server/internal/monitoring/ExecutionStatisticsImpl.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,21 @@
5656
*
5757
* @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
5858
*/
59-
class ExecutionStatisticsImpl implements ExecutionStatistics {
59+
final class ExecutionStatisticsImpl implements ExecutionStatistics {
60+
6061
/**
6162
* Builder of execution statistics.
6263
*/
6364
static class Builder {
65+
6466
private long lastStartTime;
6567
private final Map<Long, TimeWindowStatisticsImpl.Builder> intervalStatistics;
6668

6769
/**
6870
* Create a new builder.
6971
*/
7072
public Builder() {
71-
this.intervalStatistics = new HashMap<Long, TimeWindowStatisticsImpl.Builder>(4);
73+
this.intervalStatistics = new HashMap<>(6);
7274
addInterval(0, TimeUnit.MILLISECONDS);
7375
addInterval(1, TimeUnit.SECONDS);
7476
addInterval(15, TimeUnit.SECONDS);
@@ -77,20 +79,20 @@ public Builder() {
7779
addInterval(1, TimeUnit.HOURS);
7880
}
7981

80-
private void addInterval(long interval, TimeUnit timeUnit) {
82+
private void addInterval(final long interval, final TimeUnit timeUnit) {
8183
final long intervalInMillis = timeUnit.toMillis(interval);
82-
intervalStatistics.put(intervalInMillis,
83-
new TimeWindowStatisticsImpl.Builder(intervalInMillis, TimeUnit.MILLISECONDS));
84+
intervalStatistics.put(intervalInMillis, new TimeWindowStatisticsImpl.Builder(intervalInMillis,
85+
TimeUnit.MILLISECONDS));
8486
}
8587

86-
8788
/**
8889
* Add execution of a target.
90+
*
8991
* @param startTime (Unix timestamp format)
9092
* @param duration Duration of target execution in milliseconds.
9193
*/
92-
void addExecution(long startTime, long duration) {
93-
for (TimeWindowStatisticsImpl.Builder statBuilder : intervalStatistics.values()) {
94+
void addExecution(final long startTime, final long duration) {
95+
for (final TimeWindowStatisticsImpl.Builder statBuilder : intervalStatistics.values()) {
9496
statBuilder.addRequest(startTime, duration);
9597
}
9698

@@ -99,23 +101,26 @@ void addExecution(long startTime, long duration) {
99101

100102
/**
101103
* Build a new instance of execution statistics.
104+
*
102105
* @return new instance of execution statistics.
103106
*/
104107
public ExecutionStatisticsImpl build() {
105-
Map<Long, TimeWindowStatistics> newIntervalStatistics = Maps.newHashMap();
106-
for (Map.Entry<Long, TimeWindowStatisticsImpl.Builder> builderEntry : intervalStatistics.entrySet()) {
108+
final Map<Long, TimeWindowStatistics> newIntervalStatistics = Maps.newHashMap();
109+
for (final Map.Entry<Long, TimeWindowStatisticsImpl.Builder> builderEntry : intervalStatistics.entrySet()) {
107110
newIntervalStatistics.put(builderEntry.getKey(), builderEntry.getValue().build());
108111
}
109112

113+
// cache when request rate is 0
114+
110115
return new ExecutionStatisticsImpl(lastStartTime, Collections.unmodifiableMap(newIntervalStatistics));
111116
}
112117
}
113118

119+
static final ExecutionStatistics EMPTY = new Builder().build();
114120

115121
private final Date lastStartTime;
116122
private final Map<Long, TimeWindowStatistics> timeWindowStatistics;
117123

118-
119124
@Override
120125
public Date getLastStartTime() {
121126
return lastStartTime;
@@ -132,7 +137,7 @@ public ExecutionStatistics snapshot() {
132137
return this;
133138
}
134139

135-
private ExecutionStatisticsImpl(long lastStartTime, Map<Long, TimeWindowStatistics> timeWindowStatistics) {
140+
private ExecutionStatisticsImpl(final long lastStartTime, final Map<Long, TimeWindowStatistics> timeWindowStatistics) {
136141
this.lastStartTime = new Date(lastStartTime);
137142
this.timeWindowStatistics = timeWindowStatistics;
138143
}

core-server/src/main/java/org/glassfish/jersey/server/internal/monitoring/MonitoringAutodiscoverable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
5555
*/
5656
@ConstrainedTo(RuntimeType.SERVER)
57-
public class MonitoringAutodiscoverable implements ForcedAutoDiscoverable {
57+
public final class MonitoringAutodiscoverable implements ForcedAutoDiscoverable {
5858

5959
@Override
6060
public void configure(final FeatureContext context) {

core-server/src/main/java/org/glassfish/jersey/server/internal/monitoring/MonitoringContainerListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -57,7 +57,7 @@
5757
*
5858
* @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
5959
*/
60-
public class MonitoringContainerListener implements ContainerLifecycleListener {
60+
public final class MonitoringContainerListener implements ContainerLifecycleListener {
6161

6262
private volatile ApplicationEvent initFinishedEvent;
6363
private volatile ApplicationEventListener listener;

0 commit comments

Comments
 (0)