Fix: Prevent parameter refresh failure during web container startup due to issues with the execution order of the ApplicationRunner.

pull/1135/head
yanrongzhen 3 years ago
parent 6df52eb796
commit 2d5bb01bc7

@ -25,12 +25,16 @@ import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import java.util.concurrent.Executor;
/**
* Abstract web thread pool service.
*/
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
public abstract class AbstractWebThreadPoolService implements WebThreadPoolService, ApplicationRunner {
private final IWebThreadPoolHandlerSupport support;

@ -18,22 +18,28 @@
package cn.hippo4j.adapter.web;
import cn.hippo4j.common.config.ApplicationContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.concurrent.Executor;
/**
* Default WebThreadPoolService abstract class,
* reuses common capabilities for web container operations.
*/
@Slf4j
public abstract class DefaultAbstractWebThreadPoolService extends AbstractWebThreadPoolService {
public DefaultAbstractWebThreadPoolService(IWebThreadPoolHandlerSupport support) {
super(support);
}
private static final String STARTED_FIELD_NAME = "started";
/**
* Get the internal abstract method of the web container thread pool,
* to be implemented by subclasses.
@ -53,6 +59,19 @@ public abstract class DefaultAbstractWebThreadPoolService extends AbstractWebThr
return getWebServer().getPort();
}
@Override
public boolean isContainerStarted() {
try {
WebServer container = getWebServer();
Field field = ReflectionUtils.findField(WebServer.class, STARTED_FIELD_NAME);
ReflectionUtils.makeAccessible(field);
return (boolean) ReflectionUtils.getField(field, container);
} catch (Throwable th) {
log.error("Failed to get isStarted flag.", th);
return false;
}
}
/**
* Get the thread pool object of the current web container based on the WebServer.
* @param webServer current Web-Server.

@ -30,6 +30,11 @@ import java.util.concurrent.Executor;
*/
public interface WebThreadPoolService {
/**
* Determine if the web container has started.
*/
boolean isContainerStarted();
/**
* Get web thread pool.
*

@ -20,18 +20,25 @@ package cn.hippo4j.config.springboot1x.starter.web;
import cn.hippo4j.adapter.web.AbstractWebThreadPoolService;
import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport;
import cn.hippo4j.common.config.ApplicationContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
/**
* Abstract class for adapting WebThreadPoolService to Spring 1.x version.
*/
@Slf4j
public abstract class AbstractWebThreadPoolService1x extends AbstractWebThreadPoolService {
public AbstractWebThreadPoolService1x(IWebThreadPoolHandlerSupport support) {
super(support);
}
private static final String STARTED_FIELD_NAME = "started";
/**
* Get the embedded Servlet container from the Spring application context.
*/
@ -46,4 +53,17 @@ public abstract class AbstractWebThreadPoolService1x extends AbstractWebThreadPo
public Integer getPort() {
return getContainer().getPort();
}
@Override
public boolean isContainerStarted() {
try {
EmbeddedServletContainer container = getContainer();
Field field = ReflectionUtils.findField(EmbeddedServletContainer.class, STARTED_FIELD_NAME);
ReflectionUtils.makeAccessible(field);
return (boolean) ReflectionUtils.getField(field, container);
} catch (Throwable th) {
log.error("Failed to get isStarted flag.", th);
return false;
}
}
}

Loading…
Cancel
Save