-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77eb7e0
commit d32e1f4
Showing
12 changed files
with
282 additions
and
11 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/EnvUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...o4j-server/src/main/java/cn/hippo4j/server/listener/BaseSpringApplicationRunListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
hippo4j-server/src/main/java/cn/hippo4j/server/listener/Hippo4JApplicationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
hippo4j-server/src/main/java/cn/hippo4j/server/listener/StartingApplicationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.springframework.boot.SpringApplicationRunListener=cn.hippo4j.server.listener.BaseSpringApplicationRunListener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 : | ||
| | ' ,/ | | '.'|: : : : : : \ \ / ' ; |/ ../ `..- , | ||
; : ;--' ; : ;| | : | | : `----' | : ;\ \ ; | ||
| ,/ | , / `---'.| `---'.| ' ,/ \ \ ,' | ||
'---' ---`-' `---` `---` '--' "---....--' |