Skip to content

Commit 8c9b6e2

Browse files
committed
Use default phase 0 for WebSocket messaging
Closes gh-27519
1 parent d5c7a5e commit 8c9b6e2

File tree

8 files changed

+23
-13
lines changed

8 files changed

+23
-13
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ public MessageHeaderInitializer getHeaderInitializer() {
276276

277277
/**
278278
* Set the phase that this handler should run in.
279-
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}.
279+
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}, but with
280+
* {@code @EnableWebSocketMessageBroker} configuration it is set to 0.
280281
* @since 6.1.4
281282
*/
282283
public void setPhase(int phase) {

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ public boolean isAutoStartup() {
202202

203203
/**
204204
* Set the phase that this handler should run in.
205-
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}.
205+
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}, but with
206+
* {@code @EnableWebSocketMessageBroker} configuration it is set to 0.
206207
* @since 6.1.4
207208
*/
208209
public void setPhase(int phase) {

spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.beans.factory.annotation.Qualifier;
2929
import org.springframework.context.ApplicationContext;
3030
import org.springframework.context.ApplicationContextAware;
31-
import org.springframework.context.SmartLifecycle;
3231
import org.springframework.context.annotation.Bean;
3332
import org.springframework.context.event.SmartApplicationListener;
3433
import org.springframework.lang.Nullable;
@@ -197,7 +196,7 @@ protected final int getPhase() {
197196
}
198197

199198
protected int initPhase() {
200-
return SmartLifecycle.DEFAULT_PHASE;
199+
return 0;
201200
}
202201

203202
/**

spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public MessageHeaderInitializer getHeaderInitializer() {
159159

160160
/**
161161
* Set the phase that this handler should run in.
162-
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}.
162+
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}, but with
163+
* {@code @EnableWebSocketMessageBroker} configuration it is set to 0.
163164
* @since 6.1.4
164165
*/
165166
public void setPhase(int phase) {

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ default void configureMessageBroker(MessageBrokerRegistry registry) {
117117
* handling beans of type {@link SmartLifecycle} should run in.
118118
* <p>The default implementation returns {@code null} which allows other
119119
* configurers to decide. As soon as any configurer returns a value, that
120-
* value is used. If no configurer returns a value, then by default
121-
* {@link SmartLifecycle#DEFAULT_PHASE} is used.
120+
* value is used. If no configurer returns a value, then 0 is used.
122121
* <p>It is recommended to use a phase value such as 0 in order to ensure that
123-
* components start before the web server in Spring Boot application. In 6.2.0,
124-
* the default used will change to 0.
122+
* components start before the web server in Spring Boot application.
125123
* @since 6.1.4
126124
* @see SmartLifecycle
127125
*/

spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ public int getTimeToFirstMessage() {
254254

255255
/**
256256
* Set the phase that this handler should run in.
257-
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}.
257+
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}, but with
258+
* {@code @EnableWebSocketMessageBroker} configuration it is set to 0.
258259
* @since 6.1.4
259260
*/
260261
public void setPhase(int phase) {

spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public void setWebSocketUpgradeMatch(boolean match) {
6262

6363
/**
6464
* Set the phase that this handler should run in.
65-
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}.
65+
* <p>By default, this is {@link SmartLifecycle#DEFAULT_PHASE}, but with
66+
* {@code @EnableWebSocketMessageBroker} configuration it is set to 0.
6667
* @since 6.1.4
6768
*/
6869
public void setPhase(int phase) {

spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,18 @@ void webSocketMessageBrokerStats() {
212212
}
213213

214214
@Test
215-
void lifecyclePhase() {
215+
void lifecyclePhaseDefault() {
216+
ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
217+
assertPhase(context, 0);
218+
}
219+
220+
@Test
221+
void lifecyclePhaseExplicitlySet() {
216222
ApplicationContext context = createContext(LifecyclePhaseConfig.class);
223+
assertPhase(context, 99);
224+
}
217225

218-
int phase = 99;
226+
private static void assertPhase(ApplicationContext context, int phase) {
219227
Consumer<String> executorTester = beanName ->
220228
assertThat(context.getBean(beanName, ThreadPoolTaskExecutor.class).getPhase()).isEqualTo(phase);
221229

0 commit comments

Comments
 (0)