Spring Boot Application后台守护Daemon应用
本地代码启动不报错,部署到服务器之后出现如下一个错误。
系统的日志如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. [o.s.b.a.l.ConditionEvaluationReportLoggingListener]
2019-08-15 15:57:51.789 [restartedMain] ERROR Application run failed [org.springframework.boot.SpringApplication]
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.hvisen.springboot.ApplicationLauncher.main(ApplicationLauncher.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@65f0c43a has been closed already
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1067)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
at com.hvisen.springboot.utils.SpringUtils.getBean(SpringUtils.java:26)
at com.hvisen.springboot.ApplicationLauncher.run(ApplicationLauncher.java:28)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
... 10 common frames omitted
程序在启动之后,自动退出了。
问题分析:
对于应用来说,无需监听,在执行完任务之后,就自动退出,这个是非常正常的情况。那该如何做才能在后台让其一直运行呢?
办法也很简单,就是利用Thread.join()功能来实现,让主线程等待当前线程的退出,当前线程一直执行,不自动退出,则可以形成一个监听状态的应用。
解决代码如下:
import com.hvisen.springboot.base.TaskStarter;
import com.hvisen.springboot.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application implements CommandLineRunner { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} @Override
public void run(String[] args) throws Exception {
Thread.currentThread().join();
//需要启动的代码逻辑
}
}
Spring Boot Application后台守护Daemon应用的更多相关文章
- Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in
Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...
- SpringBoot零XML配置的Spring Boot Application
Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性properties文件.YAML 文件.环境变量和命令行参数来定义优先级不同的配置值.零XML配置的Spring B ...
- 【转】spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type
问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...
- Spring boot application.properties 配置
原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...
- spring boot项目后台运行
spring boot项目后台运行 Spring Boot应用的几种运行方式: (1)运行Spring Boot的应用主类 (2)使用Maven的Spring Boot插件mvn spring-boo ...
- spring boot application.properties 属性详解
2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...
- spring boot application properties配置详解
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- Spring Boot Application
spring boot默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,spring boot允许你自定义一个application.pro ...
随机推荐
- python常用模块:pickle、shelve、json、xml、configparser
今日内容主要有: 一.pickle模块二.shelve模块三.json模块四.json练习五.xml模块 六.xml练习七.configparser模块 一.pickle模块 #pickle是一个用来 ...
- zabbix 3.2.2 agent端(源码包)安装部署 (二)
一.zabbix agent 端安装部署 1.创建zabbix用户和组 # groupadd zabbix # useradd -g zabbix zabbix -s /sbin/nologin 2. ...
- python 3.4.3 安装pygame
之前一直都是用的python3.5,后来接触了pygame,又被python3.5的打包折磨的死去活来,后来干脆用python 3.4.3. 我之前安装轮子都是直接打开cmd,然后 pip3 inst ...
- 第二章 Vue快速入门--12 事件修饰符的介绍
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Python把多行文本合并
在引用论文时,往往格式出错,出现非常多行,这样操作非常不方便.这种方法讲多行合并之后,再处理: # 文件空格和回车键处理工具infile = r'C:\Users\SAM\Desktop\新建文本文档 ...
- python3安装模块,摘自网上
配置好Python3.6和pip3安装EPEL和IUS软件源 yum install epel-release -y yum install https://centos7.iuscommunity. ...
- MySQL 关于自定义函数的操作
-- 函数 --> 模块化,封装,代码复用 create function 函数名([参数列表]) returns 数据类型 begin SQL语句: return 值: end; 示例: -- ...
- Python之import方法引入模块详解
在python用import或者from-import或者from-import-as-来导入相应的模块,作用和使用方法与C语言的include头文件类似.其实就是引入某些成熟的函数库和成熟的方法,避 ...
- Practical, Dynamic Visibility for Games(可实现动态显示技术)
Practical, Dynamic Visibility for Games(可实现动态显示技术) 原文地址 1引言 游戏场景越来越复杂,包含的内容越来越多,动态显示技术很需要. 本文介绍2种互补的 ...
- HDU 6033 - Add More Zero | 2017 Multi-University Training Contest 1
/* HDU 6033 - Add More Zero [ 简单公式 ] | 2017 Multi-University Training Contest 1 题意: 问 2^n-1 有几位 分析: ...