com.github.shepherdviolet.glacimon.spring.misc.SpringStartupUtils Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2022-2022 S.Violet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project GitHub: https://github.com/shepherdviolet/glacimon
* Email: [email protected]
*/
package com.github.shepherdviolet.glacimon.spring.misc;
import org.slf4j.LoggerFactory;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
/**
* Spring启动工具
*
* @author shepherdviolet
*/
public class SpringStartupUtils {
/**
* 启动非WEB容器的SpringBoot应用
*
*
* SpringStartupUtils.startNonWebSpringBootApp(Main.class, args);
*
*/
public static void startNonWebSpringBootApp(Class> source, String[] args){
if (source == null) {
throw new RuntimeException("source class is null");
}
final ShutdownState shutdownState = new ShutdownState();
final Thread thread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
shutdownState.shutdown = true;
thread.interrupt();
}
}));
try {
new SpringApplicationBuilder(source)
.web(WebApplicationType.NONE)
.run(args);
} catch (Exception e) {
LoggerFactory.getLogger(source).error("Error while application starting", e);
return;
}
while (!shutdownState.shutdown) {
try {
Thread.sleep(60000L);
} catch (InterruptedException ignored) {
}
}
}
private static class ShutdownState{
private volatile boolean shutdown = false;
}
}