Spring Boot CommandLineRunner的使用
1. 说明
程序在启动完成的时候需要去处理某些业务,因此Spring Boot程序中需要去实现CommandLineRunner接口。
2. CommandLineRunner方法执行顺序
程序启动后,会执行接口重写的run方法,如果有多个Service的话,执行是有顺序的,可以在类上添加Order注解,来制定该run方法的执行顺序,Order中value的值越小,执行的顺序越靠前。
@Order(value = 200)
@Service
public class CatService implements CommandLineRunner
3. 重写run方法中使用了阻塞程序
如果程序启动后,我们要执行一个阻塞程序,例如程序启动后我要从阻塞队列取数据,取到数据后完成我的业务逻辑,一般这样的逻辑会写成while(true),一直去取数据处理数据,这样就会导致程序会阻塞。当然run方法不会结束。这样会带来一个问题,如果我有多个这样的业务逻辑操作,只会执行Order中value最小的那一个,其他的不会执行,因为这个执行是顺序执行,前面的阻塞了,后面的就不会被执行到。
4. 线程池使用
为解决多个Service启动后执行,并且需要阻塞执行的问题,需要在run方法中使用线程池解决。
@Order(value = 200)
@Service
public class CatService implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CatService.class); private static ExecutorService singlePool = Executors.newSingleThreadExecutor(); @Override
public void run(String... args) throws Exception {
singlePool.execute(new Runnable() {
@Override
public void run() {
while (true) {
logger.info("cat:" + Stream.of(args).collect(Collectors.joining("-"))); try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
5. 测试代码
https://github.com/liuzwei/commandline.git
Spring Boot CommandLineRunner的使用的更多相关文章
- Spring boot CommandLineRunner接口使用例子
前言 Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次. 如何使用CommandLineR ...
- [Spring boot] CommandLineRunner and Autowired
Previously we use Application Context to get Bean and Dependenies injection. It is actually easier t ...
- Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- 十三、 Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...
- Spring Boot 启动载入数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去载入一些数据或做一些事情这种需求. 为了解决这种问题.Spring Boot 为我们提供了一个方法.通过实现接口 CommandLineRunner 来实现 ...
- spring boot 在jdk 1.7下使用 commandLineRunner
在spring boot 中有一段代码,使用的是java 1.8的语法: @Bean public CommandLineRunner commandLineRunner(ApplicationCon ...
- 23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069503 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求 ...
- (23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
[Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...
随机推荐
- ODBC连接到400
1.首先iSeries Client安装的时候要勾选ODBC , 这样才能找到Driver 2.某个Application是32位上,要用32位路径下的ODBC Administration打开,添加 ...
- iptables防火墙--------基本概念
iptables按照规则进行处理,而iptables的规则存储在内核空间的信息包过滤表中,这些规则分别指定了源地址.目的地址.传输协议(TCP.UDP.ICMP)和服务类型(如HTTP.FTP和SMT ...
- centos 利用iptables来配置linux禁止所有端口登陆和开放指定端口的方法
1.关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放. 下面是命令实现: iptables -P INPUT DROPiptables -P FORWARD DROPiptabl ...
- Django基础之form表单的补充进阶
1. 应用Bootstrap样式 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- Egyptian Collegiate Programming Contest (ECPC 2015) C题 Connecting Graph
这题上次用的是线性求LCA过的,数据比较水,当时没有被T掉(不过线性的做法是在线的).现在重新的分析一下这个问题.在所有的操作都进行完毕以后,这个图形肯定会变成一棵树,而我们的要求是在这棵树上的一条链 ...
- 【原创】CancellableWait
应用程序不能正常退出,导致无法关机,这种情况通常是应用程序在等待一些I/O request to finish. 应用程序访问远程文件时,这种情况的发生更加频繁. If an application ...
- js生成带log的二维码(qrcodejs)
github: qrcodejs cdn: http://static.runoob.com/assets/qrcode/qrcode.min.js #qrcode #qrcode margin: 2 ...
- MySQL 临时表和复制表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如果你的MySQL版本 ...
- VBA 格式化excel数据表 (数据分列)
Sub ImportData() ' ' Copy Data from one workbook to the Current Workbook ' Place the macro file in t ...
- 微信自定义分享 IOS端分享失败
1.在IOS微信浏览器中自定义分享link 链接中的中文需要encodeURIComponent() 编码(安卓会自动编码) 2.另外在IOS微信浏览器中自定义分享 imgUrl 不能大于34KB ...