spring boot ApplicationRunner使用

它的使用比较简单,实现ApplicationRunner的run方法

package com.hikvision.pbg.jc.confuciussecurity.modules.task;

import com.hikvision.pbg.jc.confuciussecurity.modules.constant.Constant;
import com.hikvision.pbg.jc.confuciussecurity.modules.util.kafka.KafkaProducerPool;
import com.hikvision.pbg.jc.confuciussecurity.modules.util.kafka.StringUtil;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
@Order(1)
public class KafkaFaceProducerTest implements ApplicationRunner {
private static Logger logger = LoggerFactory.getLogger(KafkaFaceProducerTest.class);
Producer<String, String> producer = KafkaProducerPool.getInstance(Constant.KAFKA_ADDR);
public KafkaFaceProducerTest(){ }
@Override
public void run(ApplicationArguments applicationArguments) throws Exception { while(true){
Thread.sleep(1000);
logger.info("kafka produce message");
try {
String str =String.valueOf(System.currentTimeMillis());
producer.send(new KeyedMessage<String, String>("JSON_TOPIC", StringUtil.getUUID(), str));
} catch (Exception e) {
e.printStackTrace();
}
}
//Thread.sleep(5000);
//Producer<String, String> producer = KafkaProducerPool.getInstance(Constant.KAFKA_ADDR); }
}

它什么时候使用

一般我们spring boot 启动类这么写

    public static void main(String[] args) {
System.out.println("begin1");
SpringApplication.run(XX.class, args);
System.out.println("begin2");
}

ApplicationRunner的使用就在SpringApplication.run,里面有一段这样的代码实现就是:先把ApplicationRunner list执行完,再接着执行SpringApplication.run方法。

如果想通过先把ApplicationRunner起一个task,如果死循环,就很危险!因为main方法也进入到了死循环的阶段

    private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
Iterator var4 = (new LinkedHashSet(runners)).iterator(); while(var4.hasNext()) {
Object runner = var4.next();
if (runner instanceof ApplicationRunner) {
this.callRunner((ApplicationRunner)runner, args);
} if (runner instanceof CommandLineRunner) {
this.callRunner((CommandLineRunner)runner, args);
}
} }

spring boot ApplicationRunner使用的更多相关文章

  1. Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner

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

  2. Spring Boot文档阅读

    原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...

  3. Spring boot 内存优化

    转自:https://dzone.com/articles/spring-boot-memory-performance It has sometimes been suggested that Sp ...

  4. Spring Boot 启动加载数据 CommandLineRunner

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

  5. Spring Boot Memory Performance

    The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docke ...

  6. Spring Boot特性(转)

    摘要: 1. SpringApplication SpringApplication 类是启动 Spring Boot 应用的入口类,你可以创建一个包含 main() 方法的类,来运行 SpringA ...

  7. Spring Boot 学习笔记

    参考资料 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ Spring Boot简介 Spring Boot使 ...

  8. spring boot容器启动详解

    目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...

  9. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

随机推荐

  1. 【Python collections】

    目录 namedtuple deque Counter OrderedDict defaultdict "在内置数据类型(dict.list.set.tuple)的基础上,collectio ...

  2. mvc 上传文件 HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求。 maxRequestLength与 maxReceivedMessageSize 和 maxAllowedContentL区别

    具体的错误信息如下: 在线上遇到了文件上传问题,在测试环境试了好久都没有发现问题到底出在哪里,以为是服务器做了各种限制,然后一点思绪都没有.最后,尝试将线上的代码包拷贝一份,在测试环境运行,刚开始的时 ...

  3. Java开发中模拟接口工具moco的使用

    场景 在开发中需要依赖一些接口,比如需要请求一个返回Json数据的接口,但是返回Json数据的接口要么是没搭建,要么是交互比较复杂. 此时,就可以使用moco来模拟接口返回接口数据,以便开发和测试工作 ...

  4. IdentityHashCodeTest

    Java学习:identityHashCode和hashCode方法, System类提供了一个identityHashCode(Object x)方法,这个方法返回的是指定对象的精确hashCode ...

  5. as中怎么下载更新platforms和build-tools

    链接:https://blog.csdn.net/sunbinkang/article/details/78632652

  6. 在ubuntu永久添加alias

    1. cd 进入家目录 操作如下: cd ~ 2.显示隐藏文件 操作如下:ls -al 3.vim打开.bashrc 操作如下: vim .bashrc 4.按a编辑添加alias example=' ...

  7. 【Struts 分派Action】DispatchAction

    LoginAction package k.action; import k.form.UserForm; import org.apache.struts.action.ActionForm; im ...

  8. idea 快捷使用(二)回退断点的使用

    在调试的时候,想要重新走一下流程而不用再次发起一个请求? 1.所谓的断点回退,其实就是回退到上一个方法调用的开始处,在IDEA里测试无法一行一行地回退或回到到上一个断点处,而是回到上一个方法.回退的方 ...

  9. windows ,linux永久和临时修改pip源

    临时修改(建议)pypi镜像源方法:如果有untrust 报错,可使用https开头的网站,或加上--trusted 例如: pip install pywin32 -i http://mirrors ...

  10. springmvc基于java配置的实现

    该案例的github地址:https://github.com/zhouyanger/demo/tree/master/springmvc-noxml-demo 1.介绍 之前搭建SpringMvc项 ...