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基础编程: 函数示例、装饰器、模块、内置函数
目录: 函数示例 装饰器 模块 内置函数 一.函数示例: 1.为什么使用函数之模块化程序设计: 不使用模块程序设计的缺点: 1.体系结构不清晰,可主读性差: 2.可扩展性差: 3.程序冗长: 2.定义 ...
- python读取txt文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte sequence
python读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte ...
- python学习笔记(三)条件判断和循环
1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: 1 2 3 4 5 6 7 8 9 age_of_cc = 27 age = int( ...
- golang gin框架 集成swagger 自动生成文档
goswagger github仓库 https://github.com/swaggo/swag 安装 swag cli 1.因为网络原因,先安装gopm 管理工具 go get -v -u git ...
- 201871010104-陈园园《面向对象程序设计(java)》第十七周学习总结
201871010104-陈园园<面向对象程序设计(java)>第十七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- BZOJ 2836: 魔法树 (树链剖分+线段树)
板题-记得开longlong #include <cstdio> #include <cctype> #include <cstring> #include < ...
- 常用NoSql数据库比较
1. CouchDB 所用语言: Erlang 特点:DB一致性,易于使用 使用许可: Apache 协议: HTTP/REST 双向数据复制, 持续进行或临时处理, 处理时带冲突检查, 因此,采用的 ...
- JS转换/Date(-28800000)/格式
去除/Date() if (value.includes('/Date')) { var re = /-?\d+/; value = re.exec(value); value = new Date( ...
- Confluence 6.15 锚点(Anchor)宏
允许你链接到页面的特定部分.有关如何使用锚点的内容,请参考页面 Anchors 页面中的详细内容. Wiki 标记(markup) 示例 宏名称: anchor 宏内容:None. {anchor:h ...
- 『HGOI 20190917』Cruise 题解 (计算几何+DP)
题目概述 在平面直角坐标系的第$1$象限和第$4$象限有$n$个点,其中第$i$个点的坐标为$(x_i,y_i)$,有一个权值$p_i$ 从原点$O(0,0)$出发,不重复的经过一些点,最终走到原点, ...