Skip to content

Commit 9018406

Browse files
Ali Ihsan TASDELENalihsan-tsdln
authored andcommitted
Refactor EurekaServerInitializerConfiguration
Replace the raw 'new Thread' creation with CompletableFuture.runAsync() to align with modern Java concurrency practices. Consolidate field injection and ServletContextAware into a single constructor. Add the volatile keyword to the 'running' flag to ensure thread visibility across the container. Inject ApplicationEventPublisher instead of the full ApplicationContext to improve interface segregation. Fixes gh-4570 Signed-off-by: Ali Ihsan TASDELEN <atasdelen@infodif.com> Signed-off-by: Ali Ihsan TASDELEN <alihsan.tsdln@gmail.com>
1 parent 3f36565 commit 9018406

1 file changed

Lines changed: 21 additions & 25 deletions

File tree

spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerInitializerConfiguration.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,53 @@
1616

1717
package org.springframework.cloud.netflix.eureka.server;
1818

19-
import com.netflix.eureka.EurekaServerConfig;
20-
import jakarta.servlet.ServletContext;
19+
import java.util.concurrent.CompletableFuture;
20+
2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23-
24-
import org.springframework.beans.factory.annotation.Autowired;
2523
import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent;
2624
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
2725
import org.springframework.context.ApplicationContext;
2826
import org.springframework.context.ApplicationEvent;
2927
import org.springframework.context.SmartLifecycle;
3028
import org.springframework.context.annotation.Configuration;
3129
import org.springframework.core.Ordered;
32-
import org.springframework.web.context.ServletContextAware;
30+
31+
import com.netflix.eureka.EurekaServerConfig;
32+
33+
import jakarta.servlet.ServletContext;
3334

3435
/**
3536
* @author Dave Syer
3637
*/
3738
@Configuration(proxyBeanMethods = false)
38-
public class EurekaServerInitializerConfiguration implements ServletContextAware, SmartLifecycle, Ordered {
39+
public class EurekaServerInitializerConfiguration implements SmartLifecycle, Ordered {
3940

4041
private static final Log log = LogFactory.getLog(EurekaServerInitializerConfiguration.class);
4142

42-
@Autowired
43-
private EurekaServerConfig eurekaServerConfig;
43+
private static final int ORDER = 1;
4444

45-
private ServletContext servletContext;
45+
private final EurekaServerConfig eurekaServerConfig;
4646

47-
@Autowired
48-
private ApplicationContext applicationContext;
47+
private final ServletContext servletContext;
4948

50-
@Autowired
51-
private EurekaServerBootstrap eurekaServerBootstrap;
49+
private final ApplicationContext applicationContext;
5250

53-
private boolean running;
51+
private final EurekaServerBootstrap eurekaServerBootstrap;
5452

55-
private final int order = 1;
53+
private volatile boolean running;
5654

57-
@Override
58-
public void setServletContext(ServletContext servletContext) {
55+
public EurekaServerInitializerConfiguration(EurekaServerConfig eurekaServerConfig, ServletContext servletContext,
56+
ApplicationContext applicationContext, EurekaServerBootstrap eurekaServerBootstrap) {
57+
this.eurekaServerConfig = eurekaServerConfig;
5958
this.servletContext = servletContext;
59+
this.applicationContext = applicationContext;
60+
this.eurekaServerBootstrap = eurekaServerBootstrap;
6061
}
6162

6263
@Override
6364
public void start() {
64-
new Thread(() -> {
65+
CompletableFuture.runAsync(() -> {
6566
try {
6667
// TODO: is this class even needed now?
6768
eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
@@ -75,7 +76,7 @@ public void start() {
7576
// Help!
7677
log.error("Could not initialize Eureka servlet context", ex);
7778
}
78-
}).start();
79+
});
7980
}
8081

8182
private EurekaServerConfig getEurekaServerConfig() {
@@ -102,14 +103,9 @@ public int getPhase() {
102103
return 0;
103104
}
104105

105-
@Override
106-
public boolean isAutoStartup() {
107-
return true;
108-
}
109-
110106
@Override
111107
public int getOrder() {
112-
return this.order;
108+
return ORDER;
113109
}
114110

115111
}

0 commit comments

Comments
 (0)