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的使用的更多相关文章

  1. Spring boot CommandLineRunner接口使用例子

    前言 Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次. 如何使用CommandLineR ...

  2. [Spring boot] CommandLineRunner and Autowired

    Previously we use Application Context to get Bean and Dependenies injection. It is actually easier t ...

  3. Spring Boot 启动加载数据 CommandLineRunner

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...

  4. 十三、 Spring Boot 启动加载数据 CommandLineRunner

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...

  5. Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner

    本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...

  6. Spring Boot 启动载入数据 CommandLineRunner

    实际应用中,我们会有在项目服务启动的时候就去载入一些数据或做一些事情这种需求. 为了解决这种问题.Spring Boot 为我们提供了一个方法.通过实现接口 CommandLineRunner 来实现 ...

  7. spring boot 在jdk 1.7下使用 commandLineRunner

    在spring boot 中有一段代码,使用的是java 1.8的语法: @Bean public CommandLineRunner commandLineRunner(ApplicationCon ...

  8. 23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069503 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求 ...

  9. (23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】

    [Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...

随机推荐

  1. Dynamic Data linq to SQL Web Application

    微软提供了一个数据驱动网站模板,可以自动生成CRUD页面,使用过程中碰到些问题 1.首先是如何应用,只需要创建个context并且在Global.asax里面加入下面这一句就可以了 DefaultMo ...

  2. 转载:appium踩过的坑

    原文地址:http://blog.csdn.net/wirelessqa/article/details/29188665 自己的操作:由于在window上安装appium时,报各种错误:所以选择在u ...

  3. 牛客小白月赛11 Rinne Loves Xor

    题目链接:https://ac.nowcoder.com/acm/contest/370/I code: #include<bits/stdc++.h> using namespace s ...

  4. D. Restore Permutation(权值线段树)

    D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. AOP 底层实现原理

    1.核心业务接口与实现 public interface IManager { void add(String item); } public class IManagerImpl implement ...

  6. Linux 系统配置永久性时间同步

    临时修改系统时间(reboot后系统时间恢复): date 查看系统时间 date -s  "设置的系统时间" 永久性修改系统时间: date 查看系统时间 hwclock --s ...

  7. 怎样在VMware虚拟机中使用安装并设置Ubuntu系统

    1 2 3 4 5 6 7 分步阅读 Ubuntu 系统是一款优秀的.基于GNU/Linux 的平台的桌面系统. 当然,目前为止很多应用程序还完全不能允许运行在 Ubuntu 系统上,而且 Ubunt ...

  8. LC 986. Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

  9. RotateDrawable

    用来对Drawable进行旋转,也是通过setLevel来控制旋转的,最大值也是:10000 相关属性如下: fromDegrees:起始的角度,,对应最低的level值,默认为0 toDegrees ...

  10. 利用socket实现聊天-Android端核心代码

    实体类与服务端报错一致,包括包名 package loaderman.im.bean; /** * 一个聊天消息的JavaBean * * */ public class ChatMsgEntity ...