google中guava类库:AsyncEventBus
1.guava事件总线(AsyncEventBus)使用
1.1引入依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
1.2在spring中通过配置类(支持spring4.x以上及springboot)使AsyncEventBus交给spring容器管理,并设置为单例模式
package com.zy.eventbus; import com.google.common.eventbus.AsyncEventBus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; /**
* 事件注入中心
*/
@Configuration
public class AsynEventBusConfig { @Bean
@Scope("singleton")
public AsyncEventBus asyncEventBus() {
final ThreadPoolTaskExecutor executor = executor();
return new AsyncEventBus(executor);
} @Bean
public ThreadPoolTaskExecutor executor(){
/*
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
private int corePoolSize = 1;
private int maxPoolSize = 2147483647;
private int queueCapacity = 2147483647;
private int keepAliveSeconds = 60;
private boolean allowCoreThreadTimeOut = false;
* */
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(1000);
// executor.setKeepAliveSeconds(600);
// executor.setAllowCoreThreadTimeOut(true);
return executor;
}
}
1.3在需要异步执行的类中注册该类,并给异步执行的方法上加@Subscribe注解
package com.zy.service.impl; import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.Subscribe;import com.zy.service.StuServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @Service("stuService")
public class StuServiceImpl implements StuServiceI { @Autowired
private AsyncEventBus asyncEventBus; @PostConstruct // 注册该类
public void register(){
asyncEventBus.register(this);
} @AllowConcurrentEvents//线程安全
@Subscribe // 异步执行的方法标识:需要传入String类型参数
public void async01(String str){
System.out.println(str);
}
}
1.4调用方法的类
package com.zy.controller; import com.google.common.eventbus.AsyncEventBus;
import com.zy.service.StuServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping
public class GuavaEventBusController { @Autowired
private AsyncEventBus eventBus; @GetMapping("/eventbus")
public String eventbus(){
System.out.println("hello");
eventBus.post("bbb"); // 调用执行方法的参数
System.out.println("world");
return "hello eventbus";
}
}
google中guava类库:AsyncEventBus的更多相关文章
- Google的Guava类库简介(转)
说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...
- Google的Guava之IO升华
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luo201227/article/details/36413279 程序员在开发过程中,使用文件的几 ...
- ArcGIS Engine开发之旅02--ArcGIS Engine中的类库
原文:ArcGIS Engine开发之旅02--ArcGIS Engine中的类库 System类库 System类库是ArcGIS体系结构中最底层的类库.System类库包含给构成ArcGIS的其他 ...
- 关于iOS6应用中第三方类库不支持armv7s的问题解决
今天编译ios6+cocos2d v2 .1 beta2制作的游戏,出现下面的错误: ld: file is universal (3 slices) but does not contain a(n ...
- Java中基础类库使用
Java中基础类库: 在这里我仅仅介绍几种我个人觉得会常常使用的 1:Object类中的Clone机制仅仅是对对象进行浅层次的克隆,假设须要进行深层次的克隆的话那么就要自己写(详细Clone方法请參考 ...
- HTML5中Modernizr类库的作用和使用
Modernizr 是一个用来检测浏览器功能支持情况的JavaScript 库.通过这个库我们可以检测不同的浏览器对于HTML5特性的支持情况. 使用Modernizr类库和使用其他第三方类库的方法是 ...
- ArcGIS engine中Display类库——Display
转自原文 ArcGIS engine中Display类库——Display Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这 ...
- ArcGIS engine中Display类库 (局部刷新)
转自原文 ArcGIS engine中Display类库 (局部刷新) Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这个类库 ...
- Guava学习笔记:Google Guava 类库简介
http://www.cnblogs.com/peida/tag/Guava/ Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, cachin ...
随机推荐
- 6.27-JSTL、标签、分页
一.JSTL 条件标签: <c:if> if if(){ }else if(){ }else{ } <c:choose> <c:when></c:when&g ...
- gz文件最后四位检测
[root@node-0 ~]# ll -rw-r--r-- 1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...
- json化的必要性
参考文章:http://www.cnblogs.com/SanMaoSpace/p/3139186.html http://www.oschina.net/question/100267_61459
- 属性,类方法@classmethod
# 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...
- C# List<string>和ArrayList用指定的分隔符分隔成字符串
原文地址:https://www.cnblogs.com/ahwwmb/p/4166707.html 串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符 List<string> ...
- selenium自动化测试遇到的问题积累--持续积累中
引言: 在做UI自动化测试过程中,总是会遇到各种问题,而解决问题总是会花费一些时间和心思,但是解决后对于自己就是一种成长,持续积累,当可能遇到的问题都被你遇到过,并且都知道解决办法,这就是一种经验的价 ...
- saltstack安装配置及常用命令
1.salt安装及配置详解 https://www.cnblogs.com/lgeng/p/6567424.html centos7配置: https://www.jianshu.com/p/4c91 ...
- 2. java获取下周日-下周六的时间
String[] arrDate = new String[7]; String[] arrWeek = new String[7]; int mondayPlus = 0; Calendar cd ...
- git提交到远程仓库
Git概述 什么是Git? 刚开始对这个东西也感到挺迷茫,并且问了好多已经学习android一段时间的同学也是一头雾水,直到了解并使用之后,才体会到Git的好处以及重要意义. Git:是目前世界上最先 ...
- cb6xe7代码提示风格变化