linux下其他jar包
# java -jar program.jar &
当要停止程序时很多人先会考虑使用 kill -9 $pid ,强制程序退出,这有可能造成程序处理进程被半路中断,造成写入数据不完整。为了能优雅的退出,考虑通过捕捉USR2信号安全退出,以HttpServer为例。
package com.uar.daemon; import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import sun.misc.Signal; import sun.misc.SignalHandler; import com.sun.net.httpserver.HttpServer; import com.uar.bean.ConfigSetting; public class HttpServerTest implements SignalHandler { private static Logger logger = LogManager.getLogger(HttpServerTest.class); private HttpServer server; private ExecutorService httpThreadPool; @Override public void handle(Signal sn) { logger.info("Signal [" + sn.getName() + "] is received, stopServer soon..."); stopServer(); logger.info("Stop successfully."); } public static void main(String[] args){ HttpServerTest main = new HttpServerTest(); // 捕捉USR2信号 Signal.handle(new Signal("USR2"), main); main.startServer(); } public void startServer() { int port = 5555; String context = "/KillTest"; int maxConnections = 50; try { InetSocketAddress addr = new InetSocketAddress(port); server = HttpServer.create(addr, maxConnections); server.createContext(context, new ServerHandler()); httpThreadPool = Executors.newCachedThreadPool(); server.setExecutor(httpThreadPool); server.start(); } catch (IOException e) { logger.error(e); } } /** * 安全的关闭HttpServer监听服务 */ private void stopServer() { server.stop(1); httpThreadPool.shutdown(); } }
server.stop()
stop public abstract void stop(int delay) stops this server by closing the listening socket and disallowing any new exchanges from being processed. The method will then block until all current exchange handlers have completed or else when approximately delay seconds have elapsed (whichever happens sooner). Then, all open TCP connections are closed, the background thread created by start() exits, and the method returns. Once stopped, a HttpServer cannot be re-used. Parameters: delay - the maximum time in seconds to wait until exchanges have finished. Throws: IllegalArgumentException - if delay is less than zero.
相关推荐
SpringBoot是基于Spring框架的微服务开发工具,它简化了创建独立、生产级别的Java应用程序的过程。SpringBoot应用通常被打包为可执行的jar文件,可以直接运行,无需额外的服务器环境。 接下来,让我们关注Linux ...
Spring-Boot是Spring框架的一个子项目,旨在简化创建独立的、生产级别的基于Spring的应用程序。它通过提供默认配置、自动配置以及“起步依赖”(Starter POMs)使得开发过程更加简单快速。Spring-Boot应用通常包含一...
可以优雅的关闭系统,也是判断程序员是能对系统掌控能力的核心指标之一。 这里我们来讨论一下基于Spring Boot优雅关机的常见场景。 2. 原理知识 当程序运行时,操作系统调度器加载到内存,分配进程ID,进入待执行...
SpringBoot是Java领域一个流行的微服务框架,它简化了创建独立、生产级别的基于Spring的应用程序流程。 描述虽然为空,但根据标题,我们可以推测文章可能包含了创建和使用shell脚本来管理SpringBoot应用的方法。这...
- **kill[信号代码]<进程PID>**:发送信号给进程。 - **renice<优先级表达式><进程表达式>**:更改进程的优先级。 - **top**:实时监控系统资源使用情况。 - **nohup<命令>**:在后台运行命令,即使用户退出也不受...