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. 被折腾的sql编程

  2. [转载]tornado.database添加PooledDB连接池功能

    转载自 http://chenxiaoyu.org/2009/12/01/python-tornado-dbutil.html tornado.database模块简单包装了下对MySQL的操作,短小 ...

  3. maven快速入门之安装

    maven是基于项目对象模型(pom) 可以通过y一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具. 覆盖编译,测试运行清理构建周期,提供仓库的概念,统一帮助管理项目. 下载:http: ...

  4. 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结

    第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...

  5. zabbix自动发现与监控内存和CPU使用率最高的进程,监测路由器

    https://cloud.tencent.com/info/488cfc410f29d110c03bcf0faaac55b2.html         (未测试) https://www.cnblo ...

  6. windows,linux,esxi系统判断当前主机是物理机还是虚拟机?查询主机序列号命令

    参考网站:https://blog.csdn.net/yangzhenping/article/details/49996765 查序列号: http://www.bubuko.com/infodet ...

  7. leetcode557

    public class Solution { public string ReverseWords(string s) { var list = s.Split(' ').AsEnumerable( ...

  8. leetcode292

    public class Solution { public bool CanWinNim(int n) { //如果要赢,最后3个必须是自己来拿 //也就是最后剩1,2,3是胜利, //如果剩4枚, ...

  9. Kotlin语言学习笔记(7)

    反射 // 反射 val c = MyClass::class val c2 = MyClass::class.java // 获取KClass的引用 val widget: Widget = ... ...

  10. Linux CentOS7中 设置IP地址、网关DNS

    cd  /etc/sysconfig/network-scripts/  #进入网络配置文件目录 vi  ifcfg-eno16777736  #编辑配置文件,此处eno后边的编号因电脑而易 TYPE ...