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. GO111MODULE的设置(及GOPROXY)

    环境:win7  go1.13.1 早听说GO111MODULE大名,今天才测试成功,步骤如下: 因为我的Go version >= 1.13,直接用go env -w 设置(注意大小写) go ...

  2. [Luogu] 矩形覆盖

    https://www.luogu.org/problemnew/show/P1034 数据太水 爆搜过掉 #include <iostream> #include <cstdio& ...

  3. python实现随机生成头像

    今天遇到如何给用户分配随机头像的问题,想着要在本地放很多图片,有点无聊,就找了一些生成头像的工具.发现gravatar生成图像还不错,挺好玩的. 1.下面上代码 # -*- coding: utf-8 ...

  4. codeforces1213G Path Queries 并查集

    题意 给定n个结点的树,每条边有边权,有m个询问,每个询问给一个\(q_i\)输出树上有多少点对的简单路径上最大的边权不超过\(q_i\). 分析 用并查集维护点集,同时维护大小. 将所有边按边权排序 ...

  5. pandas入门之DataFrame

    创建DataFrame - DataFrame是一个[表格型]的数据结构.DataFrame由按一定顺序排列的多列数据组成.设计初衷是将Series的使用场景从一维拓展到多维.DataFrame既有行 ...

  6. jquer属性 offset、position、scrollTop

    尺寸操作 1.获取宽高      a) jq对象.height/width () :只有获取高度/宽度         尺寸,不包括padding和margin 和 border 2.设置宽度     ...

  7. ROS Topic 常用指令

    rostopic list rosnode list一樣,就是列出目前運行中的topic有哪些. rostopic echo <topic_name> 接下來這個指令比較重要啦,就是去監聽 ...

  8. 令人窒息的操作,nodejs面向对象。

    // async函数返回一个 Promise 对象,可以使用then方法添加回调函数. // 当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句. clas ...

  9. 基本PSO算法实现(Java)

    一.算法流程 Step1:初始化一群粒子(粒子个数为50个),包括随即位置和速度: Step2:计算每个粒子的适应度fitness: Step3:对每个粒子,将其适应度与其进过的最好位置(局部)pbe ...

  10. OpenResty之 lua_shared_dict 指令

    1. lua_shared_dict 指令介绍 原文: lua_shared_dict syntax:lua_shared_dict <name> <size> default ...