spring boot ApplicationRunner使用
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使用的更多相关文章
- Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...
- Spring Boot文档阅读
原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...
- Spring boot 内存优化
转自:https://dzone.com/articles/spring-boot-memory-performance It has sometimes been suggested that Sp ...
- Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- Spring Boot Memory Performance
The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docke ...
- Spring Boot特性(转)
摘要: 1. SpringApplication SpringApplication 类是启动 Spring Boot 应用的入口类,你可以创建一个包含 main() 方法的类,来运行 SpringA ...
- Spring Boot 学习笔记
参考资料 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ Spring Boot简介 Spring Boot使 ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
随机推荐
- UIButton按钮的高亮状态颜色
首先是adjustsImageWhenHighlighted属性的正确使用: UIButton的adjustsImageWhenHighlighted属性是当UIButton设置了背景图片时,并且没有 ...
- opencv:图像模糊
高斯模糊 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespa ...
- PAT 1014 Waiting in Line (模拟)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...
- jq 获取input多选框
1.获取checkbox选中个数 $("input[name='ckb-jobid']:checked").length $("input[type='checkbox' ...
- python匿名函数与三元运算
匿名函数 匿名函数就是不需要显示式的指定函数名 首先看一行代码: def calc(x,y): return x*y print(calc(2,3)) # 换成匿名函数 calc = lambda ...
- pycharm连接数据库报错Access denied for user 'root'@'localhost' (using password:YES),以及wampserver 2/3个服务器正在运行 问题
使用mysql版本为mysql5.7,参考下列 https://blog.csdn.net/qq_32969455/article/details/79051932 https://blog.csdn ...
- ASP.NET Core搭建多层网站架构【11-WebApi统一处理返回值、异常】
2020/02/01, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[11-WebApi统一处理返回值.异常] 使用I ...
- mysql5.7修改root密码
use mysql; update mysql.user set authentication_string=password('123456') where user='root'; flush p ...
- mac下Red Hat 7.4服务器初始化
物料:VMware Fusion for Mac版 rhel-server-7.4-x86_64-dvd.iso 通过VMware安装好虚拟机,打开终端: 1.通过ifconfig查看ip和网 ...
- PyQt5打印机
1.打印机操作(打印默认文本里面的内容)from PyQt5 import QtGui,QtWidgets,QtPrintSupportfrom PyQt5.QtWidgets import *imp ...