Skip to content

Commit 7f9baa3

Browse files
committed
Polishing
1 parent a6e1c53 commit 7f9baa3

File tree

4 files changed

+30
-39
lines changed

4 files changed

+30
-39
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/ScheduledExecutorFactoryBean.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* @author Juergen Hoeller
5959
* @since 2.0
6060
* @see #setPoolSize
61-
* @see #setRemoveOnCancelPolicy(boolean)
61+
* @see #setRemoveOnCancelPolicy
6262
* @see #setThreadFactory
6363
* @see ScheduledExecutorTask
6464
* @see java.util.concurrent.ScheduledExecutorService
@@ -68,17 +68,17 @@
6868
public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport
6969
implements FactoryBean<ScheduledExecutorService> {
7070

71-
// ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(boolean) only available on JDK 1.7+
71+
// ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(boolean) only available on JDK 7+
7272
private static final boolean setRemoveOnCancelPolicyAvailable =
7373
ClassUtils.hasMethod(ScheduledThreadPoolExecutor.class, "setRemoveOnCancelPolicy", boolean.class);
7474

7575

7676
private int poolSize = 1;
7777

78-
private boolean removeOnCancelPolicy;
79-
8078
private ScheduledExecutorTask[] scheduledExecutorTasks;
8179

80+
private boolean removeOnCancelPolicy = false;
81+
8282
private boolean continueScheduledExecutionAfterException = false;
8383

8484
private boolean exposeUnconfigurableExecutor = false;
@@ -95,14 +95,6 @@ public void setPoolSize(int poolSize) {
9595
this.poolSize = poolSize;
9696
}
9797

98-
/**
99-
* Set the same property on ScheduledExecutorService (JDK 1.7+).
100-
* There is no default. If not set, the executor property is not set.
101-
*/
102-
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
103-
this.removeOnCancelPolicy = removeOnCancelPolicy;
104-
}
105-
10698
/**
10799
* Register a list of ScheduledExecutorTask objects with the ScheduledExecutorService
108100
* that this FactoryBean creates. Depending on each ScheduledExecutorTask's settings,
@@ -115,6 +107,15 @@ public void setScheduledExecutorTasks(ScheduledExecutorTask... scheduledExecutor
115107
this.scheduledExecutorTasks = scheduledExecutorTasks;
116108
}
117109

110+
/**
111+
* Set the remove-on-cancel mode on {@link ScheduledThreadPoolExecutor} (JDK 7+).
112+
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
113+
* switched into remove-on-cancel mode (if possible, with a soft fallback otherwise).
114+
*/
115+
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
116+
this.removeOnCancelPolicy = removeOnCancelPolicy;
117+
}
118+
118119
/**
119120
* Specify whether to continue the execution of a scheduled task
120121
* after it threw an exception.

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@
5858
public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
5959
implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler {
6060

61-
// ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(boolean) only available on JDK 1.7+
61+
// ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(boolean) only available on JDK 7+
6262
private static final boolean setRemoveOnCancelPolicyAvailable =
6363
ClassUtils.hasMethod(ScheduledThreadPoolExecutor.class, "setRemoveOnCancelPolicy", boolean.class);
6464

6565

6666
private volatile int poolSize = 1;
6767

68-
private volatile boolean removeOnCancelPolicy;
68+
private volatile boolean removeOnCancelPolicy = false;
6969

7070
private volatile ScheduledExecutorService scheduledExecutor;
7171

@@ -86,8 +86,9 @@ public void setPoolSize(int poolSize) {
8686
}
8787

8888
/**
89-
* Set the same property on ScheduledExecutorService (JDK 1.7+).
90-
* There is no default. If not set, the executor property is not set.
89+
* Set the remove-on-cancel mode on {@link ScheduledThreadPoolExecutor} (JDK 7+).
90+
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
91+
* switched into remove-on-cancel mode (if possible, with a soft fallback otherwise).
9192
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
9293
*/
9394
@UsesJava7
@@ -183,14 +184,17 @@ public int getPoolSize() {
183184
}
184185

185186
/**
186-
* Return the current setting of removeOnCancelPolicy.
187-
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor} and JDK 1.7+.
187+
* Return the current setting for the remove-on-cancel mode.
188+
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
188189
*/
189190
@UsesJava7
190191
public boolean isRemoveOnCancelPolicy() {
192+
if (!setRemoveOnCancelPolicyAvailable) {
193+
return false;
194+
}
191195
if (this.scheduledExecutor == null) {
192196
// Not initialized yet: return our setting for the time being.
193-
return (setRemoveOnCancelPolicyAvailable && this.removeOnCancelPolicy);
197+
return this.removeOnCancelPolicy;
194198
}
195199
return getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy();
196200
}

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ public SockJsServiceRegistration setTaskScheduler(TaskScheduler taskScheduler) {
8181
* iframe so that code in the iframe can run from a domain local to the SockJS
8282
* server. Since the iframe needs to load the SockJS javascript client library,
8383
* this property allows specifying where to load it from.
84-
*
8584
* <p>By default this is set to point to
8685
* "https://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js". However it can
8786
* also be set to point to a URL served by the application.
88-
*
8987
* <p>Note that it's possible to specify a relative URL in which case the URL
9088
* must be relative to the iframe URL. For example assuming a SockJS endpoint
9189
* mapped to "/sockjs", and resulting iframe URL "/sockjs/iframe.html", then the

spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.springframework.web.socket.sockjs.support;
1818

1919
import java.io.IOException;
20-
2120
import javax.servlet.ServletOutputStream;
2221
import javax.servlet.http.HttpServletResponse;
2322

2423
import org.junit.Before;
2524
import org.junit.Test;
25+
2626
import org.springframework.http.HttpStatus;
2727
import org.springframework.http.server.ServerHttpRequest;
2828
import org.springframework.http.server.ServerHttpResponse;
@@ -55,6 +55,7 @@ public void setUp() {
5555
this.service = new TestSockJsService(new ThreadPoolTaskScheduler());
5656
}
5757

58+
5859
@Test
5960
public void validateRequest() throws Exception {
6061

@@ -76,7 +77,6 @@ public void validateRequest() throws Exception {
7677

7778
@Test
7879
public void handleInfoGet() throws Exception {
79-
8080
resetResponseAndHandleRequest("GET", "/echo/info", HttpStatus.OK);
8181

8282
assertEquals("application/json;charset=UTF-8", this.servletResponse.getContentType());
@@ -98,9 +98,7 @@ public void handleInfoGet() throws Exception {
9898
body.substring(body.indexOf(',')));
9999
}
100100

101-
// SPR-11443
102-
103-
@Test
101+
@Test // SPR-11443
104102
public void handleInfoGetCorsFilter() throws Exception {
105103

106104
// Simulate scenario where Filter would have already set CORS headers
@@ -111,9 +109,7 @@ public void handleInfoGetCorsFilter() throws Exception {
111109
assertEquals("foobar:123", this.servletResponse.getHeader("Access-Control-Allow-Origin"));
112110
}
113111

114-
// SPR-11919
115-
116-
@Test
112+
@Test // SPR-11919
117113
public void handleInfoGetWildflyNPE() throws Exception {
118114
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
119115
ServletOutputStream ous = mock(ServletOutputStream.class);
@@ -128,9 +124,7 @@ public void handleInfoGetWildflyNPE() throws Exception {
128124

129125
@Test
130126
public void handleInfoOptions() throws Exception {
131-
132127
this.servletRequest.addHeader("Access-Control-Request-Headers", "Last-Modified");
133-
134128
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.NO_CONTENT);
135129
this.response.flush();
136130

@@ -143,7 +137,6 @@ public void handleInfoOptions() throws Exception {
143137

144138
@Test
145139
public void handleIframeRequest() throws Exception {
146-
147140
resetResponseAndHandleRequest("GET", "/echo/iframe.html", HttpStatus.OK);
148141

149142
assertEquals("text/html;charset=UTF-8", this.servletResponse.getContentType());
@@ -155,15 +148,12 @@ public void handleIframeRequest() throws Exception {
155148

156149
@Test
157150
public void handleIframeRequestNotModified() throws Exception {
158-
159151
this.servletRequest.addHeader("If-None-Match", "\"06b486b3208b085d9e3220f456a6caca4\"");
160-
161152
resetResponseAndHandleRequest("GET", "/echo/iframe.html", HttpStatus.NOT_MODIFIED);
162153
}
163154

164155
@Test
165156
public void handleRawWebSocketRequest() throws Exception {
166-
167157
resetResponseAndHandleRequest("GET", "/echo", HttpStatus.OK);
168158
assertEquals("Welcome to SockJS!\n", this.servletResponse.getContentAsString());
169159

@@ -174,13 +164,13 @@ public void handleRawWebSocketRequest() throws Exception {
174164

175165
@Test
176166
public void handleEmptyContentType() throws Exception {
177-
178-
servletRequest.setContentType("");
167+
this.servletRequest.setContentType("");
179168
resetResponseAndHandleRequest("GET", "/echo/info", HttpStatus.OK);
180169

181170
assertEquals("Invalid/empty content should have been ignored", 200, this.servletResponse.getStatus());
182171
}
183172

173+
184174
private void resetResponseAndHandleRequest(String httpMethod, String uri, HttpStatus httpStatus) throws IOException {
185175
resetResponse();
186176
handleRequest(httpMethod, uri, httpStatus);
@@ -211,14 +201,12 @@ public TestSockJsService(TaskScheduler scheduler) {
211201
@Override
212202
protected void handleRawWebSocketRequest(ServerHttpRequest req, ServerHttpResponse res,
213203
WebSocketHandler handler) throws IOException {
214-
215204
this.handler = handler;
216205
}
217206

218207
@Override
219208
protected void handleTransportRequest(ServerHttpRequest req, ServerHttpResponse res, WebSocketHandler handler,
220209
String sessionId, String transport) throws SockJsException {
221-
222210
this.sessionId = sessionId;
223211
this.transport = transport;
224212
this.handler = handler;

0 commit comments

Comments
 (0)