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 ...
随机推荐
- 从n个数里面找最大的两个数理论最少需要比较
答案是:n+logn-2 过程是这样的: 甲乙比甲胜出,丙丁比丙胜出,最后甲丙比较,甲胜出...容易得出找出最大数为n-1次. 现在开始找出第二大的数字:明显,第二大的数字,一定和甲进行过比较.... ...
- K-MEANS算法及sklearn实现
K-MEANS算法 聚类概念: 1.无监督问题:我们手里没有标签 2.聚类:相似的东西分到一组 3.难点:如何评估,如何调参 4.要得到簇的个数,需要指定K值 5.质心:均值,即向量各维取平均即可 6 ...
- 解决mysql提示服务无法启动问题
1.管理员权限打开命令,进入mysql下bin文件夹 删除根目录下data文件夹没有不用管,重新安装 2. ---------------------------------------------- ...
- Mybatis问题-Type interface com.zzu.ssm.dao.UserMapper is not known to the MapperRegistry
1. mapper.xml中namespace名称是否与dao接口包名一致 2. 在mybatis配置文件中注册mapper
- 【leetcode】1287. Element Appearing More Than 25% In Sorted Array
题目如下: Given an integer array sorted in non-decreasing order, there is exactly one integer in the arr ...
- 面向对象(oop)特征
上课时老师不止一次和我们说过,面向对象是Java基础的重中之重!!所以一定要扎实基本功,代码都是呆子活,重要的是思想! 一般来说oop的三大特性是:封装.继承和多态,上次笔者看到有把抽象也归类其中,不 ...
- nodejs(上)(获取请求参数)
Node.js是一个让JavaScript运行在服务器端的开发平台 参考文章 nodejs特点: 单线程 异步非阻塞i/o(异步相对节省资源,把那个等待的时间利用上了) 事件驱动 稳定性差(因为 ...
- CDOJ 1255 斓少摘苹果 图论 2016_5_14
斓少摘苹果 Time Limit: 3000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- 『HGOI 20190917』Lefkaritika 题解 (DP)
题目概述 一个$n \times m$的整点集.其中$q$个点被m被设置为不能访问. 问这个点集中含有多少个不同的正方形,满足不包含任何一个不能访问的点. 对于$50\%$的数据满足$1 \leq n ...
- python数据分析-数据导入
1.导入CSV格式数据 import pandas data = pandas.read_csv("C:\\Users\\zhaosai\\Desktop\\进击的DBA\\谁说菜鸟不会数据 ...