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. CA证书认证单向和双向的区别

     我觉得最科学的应该是,单向的,每次客户端发两把锁住的东西给服务端,服务端解密两次,服务端用客户端发来的对称密钥加密数据,发送给客户端,客户端只需解密一次,然后客户端每次修改随机密码,传给服务端,服务 ...

  2. configure: error: You need a C++ compiler for C++ support.

    安装pcre包的时候提示缺少c++编译器 报错信息如下: configure: error: You need a C++ compiler for C++ support. 解决办法,使用yum安装 ...

  3. 如何分析 WindowsDump:Dump 起源与初始设置

    https://www.qcloud.com/community/article/511817 转者注:让我感觉以前看蓝屏都白看了~~~原来蓝屏也可以分析具体原因. 适用场景:Windows 系列系统 ...

  4. python之路07文件处理

    一    操作文件的方法: f.read() #读取所有内容,光标移动到文件末尾 f.readline() #读取一行内容,光标移动到第二行首部 f.readlines() #读取每一行内容,存放于列 ...

  5. [Python] Hermite 插值

    # -*- coding: utf-8 -*- #Program 0.5 Hermite Interpolation import matplotlib.pyplot as plt import nu ...

  6. 温故而知新复习下PHP面向对象

    面向对象在PHP中是以类的形势展示的 PHP中的类是单继承的,用关键字extends来实现继承父类, 关键字public protected private 第一个是公开的 谁都可以访问,第二个只能本 ...

  7. ckeditor源码编辑模式,添加style、javascript内容丢失的解决

    我使用ckeditor 我在编辑的使用源码编辑,保存内容包含javascript.style标签的时候,数据库中有javascript.style标签 , 输入到页面也可以执行,但是我再次编辑的时候就 ...

  8. ELK配置过程初次安装使用心得--elasticsearch5.4版--及logstash

    安装所遇到的问题:http://www.bubuko.com/infodetail-1889252.html 一,先创建用户和组groupadd es useradd -g es es passwd  ...

  9. 5. Java中序列化的serialVersionUID作用

    Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象. 序列化是Java中实现持久化存储的一种方法:为数据传输提供了线路级对象表示法. Java的序列化机制是通过在运行时判断 ...

  10. JS计算时间差值

    var d = '2016 04 30 11:28:04'; var currentDate = new Date();//当前时间 var endDate = new Date(d); //结束时间 ...