mirror of https://github.com/longtai-cn/hippo4j
优化 Server 启动脚本日志输出. (#92)
parent
77eb7e08fa
commit
d32e1f4450
@ -0,0 +1,55 @@
|
||||
package cn.hippo4j.config.toolkit;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Env Util.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/2/9 07:46
|
||||
*/
|
||||
public class EnvUtil {
|
||||
|
||||
public static final String HIPPO4J_HOME_KEY = "hippo4j.home";
|
||||
|
||||
public static final String STANDALONE_MODE_PROPERTY_NAME = "hippo4j.standalone";
|
||||
|
||||
private static String HIPPO4J_HOME_PATH = null;
|
||||
|
||||
private static Boolean IS_STANDALONE = null;
|
||||
|
||||
/**
|
||||
* Get hippo4j home.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getHippo4JHome() {
|
||||
if (StringUtils.isBlank(HIPPO4J_HOME_PATH)) {
|
||||
String hippo4jHome = System.getProperty(HIPPO4J_HOME_KEY);
|
||||
if (StringUtils.isBlank(hippo4jHome)) {
|
||||
hippo4jHome = Paths.get(System.getProperty("user.home"), "hippo4j").toString();
|
||||
}
|
||||
|
||||
return hippo4jHome;
|
||||
}
|
||||
|
||||
return HIPPO4J_HOME_PATH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Standalone mode or not.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean getStandaloneMode() {
|
||||
if (Objects.isNull(IS_STANDALONE)) {
|
||||
IS_STANDALONE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);
|
||||
}
|
||||
|
||||
return IS_STANDALONE;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package cn.hippo4j.server.listener;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringApplicationRunListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Base spring application run listener.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/2/9 04:29
|
||||
*/
|
||||
public class BaseSpringApplicationRunListener implements SpringApplicationRunListener, Ordered {
|
||||
|
||||
private final SpringApplication application;
|
||||
|
||||
private final String[] args;
|
||||
|
||||
private List<Hippo4JApplicationListener> hippo4JApplicationListeners = new ArrayList();
|
||||
|
||||
{
|
||||
hippo4JApplicationListeners.add(new StartingApplicationListener());
|
||||
}
|
||||
|
||||
public BaseSpringApplicationRunListener(SpringApplication application, String[] args) {
|
||||
this.application = application;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void starting() {
|
||||
hippo4JApplicationListeners.forEach(each -> each.starting());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextPrepared(ConfigurableApplicationContext context) {
|
||||
hippo4JApplicationListeners.forEach(each -> each.contextPrepared(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void started(ConfigurableApplicationContext context) {
|
||||
hippo4JApplicationListeners.forEach(each -> each.started(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(ConfigurableApplicationContext context, Throwable exception) {
|
||||
hippo4JApplicationListeners.forEach(each -> each.failed(context, exception));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return HIGHEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package cn.hippo4j.server.listener;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* Hippo4J application listener.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/2/9 04:35
|
||||
*/
|
||||
public interface Hippo4JApplicationListener {
|
||||
|
||||
/**
|
||||
* {@link BaseSpringApplicationRunListener#starting}
|
||||
*/
|
||||
void starting();
|
||||
|
||||
/**
|
||||
* {@link BaseSpringApplicationRunListener#contextPrepared}
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
void contextPrepared(ConfigurableApplicationContext context);
|
||||
|
||||
/**
|
||||
* {@link BaseSpringApplicationRunListener#started}
|
||||
*
|
||||
* @param context context
|
||||
*/
|
||||
void started(ConfigurableApplicationContext context);
|
||||
|
||||
/**
|
||||
* {@link BaseSpringApplicationRunListener#failed}
|
||||
*
|
||||
* @param context
|
||||
* @param exception
|
||||
*/
|
||||
void failed(ConfigurableApplicationContext context, Throwable exception);
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package cn.hippo4j.server.listener;
|
||||
|
||||
import cn.hippo4j.config.toolkit.EnvUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Starting application listener.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/2/9 04:39
|
||||
*/
|
||||
@Slf4j
|
||||
public class StartingApplicationListener implements Hippo4JApplicationListener {
|
||||
|
||||
private volatile boolean starting;
|
||||
|
||||
private ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
@Override
|
||||
public void starting() {
|
||||
starting = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextPrepared(ConfigurableApplicationContext context) {
|
||||
if (EnvUtil.getStandaloneMode()) {
|
||||
scheduledExecutorService = new ScheduledThreadPoolExecutor(
|
||||
1,
|
||||
r -> {
|
||||
Thread thread = new Thread(r);
|
||||
thread.setName("server.hippo4j-starting");
|
||||
return thread;
|
||||
}
|
||||
);
|
||||
|
||||
scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
||||
if (starting) {
|
||||
log.info("Hippo4J is starting...");
|
||||
}
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void started(ConfigurableApplicationContext context) {
|
||||
starting = false;
|
||||
|
||||
closeExecutor();
|
||||
|
||||
if (EnvUtil.getStandaloneMode()) {
|
||||
log.info("Hippo4J started successfully...");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(ConfigurableApplicationContext context, Throwable exception) {
|
||||
log.error("Startup errors : {}", exception);
|
||||
|
||||
closeExecutor();
|
||||
context.close();
|
||||
|
||||
log.error("Hippo4J failed to start, please see {} for more details.",
|
||||
Paths.get(EnvUtil.getHippo4JHome(), "logs/hippo4j.log"));
|
||||
}
|
||||
|
||||
private void closeExecutor() {
|
||||
if (scheduledExecutorService != null) {
|
||||
scheduledExecutorService.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.springframework.boot.SpringApplicationRunListener=cn.hippo4j.server.listener.BaseSpringApplicationRunListener
|
@ -0,0 +1,15 @@
|
||||
|
||||
,--, ,--, ,---._
|
||||
,--.'| ,--.'| .-- -.' \ Hippo4J ${application.version}
|
||||
,--, | : ,--, ,-.----. ,-.----. ,--, | : | | : Port: ${server.port}
|
||||
,---.'| : ',--.'| \ / \ \ / \ ,---. ,---.'| : ' : ; | PID: ${pid}
|
||||
| | : _' || |, | : || : | ' ,'\ ; : | | ; : | Console: http://127.0.0.1:${server.port}/index.html
|
||||
: : |.' |`--'_ | | .\ :| | .\ : / / || | : _' | | : :
|
||||
| ' ' ; :,' ,'| . : |: |. : |: |. ; ,. :: : |.' | : https://hippox.cn
|
||||
' | .'. |' | | | | \ :| | \ :' | |: :| ' ' ; : | ; |
|
||||
| | : | '| | : | : . || : . |' | .; :\ \ .'. | ___ l
|
||||
' : | : ;' : |__ : |`-': |`-'| : | `---`: | ' / /\ J :
|
||||
| | ' ,/ | | '.'|: : : : : : \ \ / ' ; |/ ../ `..- ,
|
||||
; : ;--' ; : ;| | : | | : `----' | : ;\ \ ;
|
||||
| ,/ | , / `---'.| `---'.| ' ,/ \ \ ,'
|
||||
'---' ---`-' `---` `---` '--' "---....--'
|
Loading…
Reference in new issue