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

  1. Google的Guava类库简介(转)

    说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...

  2. Google的Guava之IO升华

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luo201227/article/details/36413279 程序员在开发过程中,使用文件的几 ...

  3. ArcGIS Engine开发之旅02--ArcGIS Engine中的类库

    原文:ArcGIS Engine开发之旅02--ArcGIS Engine中的类库 System类库 System类库是ArcGIS体系结构中最底层的类库.System类库包含给构成ArcGIS的其他 ...

  4. 关于iOS6应用中第三方类库不支持armv7s的问题解决

    今天编译ios6+cocos2d v2 .1 beta2制作的游戏,出现下面的错误: ld: file is universal (3 slices) but does not contain a(n ...

  5. Java中基础类库使用

    Java中基础类库: 在这里我仅仅介绍几种我个人觉得会常常使用的 1:Object类中的Clone机制仅仅是对对象进行浅层次的克隆,假设须要进行深层次的克隆的话那么就要自己写(详细Clone方法请參考 ...

  6. HTML5中Modernizr类库的作用和使用

    Modernizr 是一个用来检测浏览器功能支持情况的JavaScript 库.通过这个库我们可以检测不同的浏览器对于HTML5特性的支持情况. 使用Modernizr类库和使用其他第三方类库的方法是 ...

  7. ArcGIS engine中Display类库——Display

    转自原文  ArcGIS engine中Display类库——Display Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这 ...

  8. ArcGIS engine中Display类库 (局部刷新)

    转自原文 ArcGIS engine中Display类库 (局部刷新) Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这个类库 ...

  9. Guava学习笔记:Google Guava 类库简介

    http://www.cnblogs.com/peida/tag/Guava/ Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, cachin ...

随机推荐

  1. storm的可靠性

    消息确认机制: 在数据发送的过程中可能会数据丢失导致没能接收到,spout有个超时时间(默认是30S),如果30S过去了还是没有接收到数据,也认为是处理失败. 运行结果都是处理成功 参考代码Storm ...

  2. 小朋友学Java(2):Win 7安装JDK

    1 打开命令行窗口,输入java -version. 若提示不认识java命令,说明没有java环境.   1.png 2 从甲骨文网站(http://www.oracle.com/technetwo ...

  3. snmp简单识记

    免费snmp探测 http://webluker.com/webtools/snmp snmp简单网络管理协议(simple network management protocol)前身时sgmp简单 ...

  4. CentOS7.3下yum练手安装Nginx

    安装Nginx # 查看相关信息 yum info nginx yum info httpd # 移除 httpd,也就是 Apache yum remove httpd -y # 安装 nginx ...

  5. 17.scrapy框架简例使用

    目标:创建scrapy项目 创建一个spider来抓取站点和处理数据 通过命令行将抓取内容导出 1.创建项目 scrapy startproject tutorial 2.创建spider cd tu ...

  6. MVC 4 Razor Design Sample Demo Project

    This is a demo project in MCV 4 razor design which encompases the general design of MVC pattern. The ...

  7. leetcode127

    class Solution { public int ladderLength(String beginWord, String endWord, List<String> wordLi ...

  8. jquery 的 $.extend 和 $.fn.extend

    $.extend({ add:function(a,b){return a+b;}, bad:function(a,b){return a-b;} }); $.fn.extend({ loading: ...

  9. C#--Winform项目核心模块--考勤模块

    C#--Winform项目核心模块--考勤模块(一) C#--Winform项目核心--考勤模块(二) C#--Winform项目核心模块--考勤模块(三)

  10. tomcat的catalina.out日志文件过大

    今天发现一个服务器的/opt目录数据过大,最后发现是tomcat中的catalina.out日志过大引起的 用du命令查看opt下一层的数据文件大小 [root@ccssapportalp opt]# ...