- 浏览: 63911 次
文章分类
最新评论
-
小灯笼:
ZooKeeper分布式专题与Dubbo微服务入门网盘地址:h ...
dubbo+zookeeper构建高可用分布式集群 -
qingfengxiu1985:
有没有全部工程代码?发一个呗,邮箱:qingfengxiu19 ...
mongodb+spring +morphia完整版框架搭建
Guava是Google开源的一个Java基础类库,它在Google内部被广泛使用。Guava提供了很多功能模块比如:集合、并发库、缓存等,EventBus是Guava框架对观察者模式的一种实现,使用EventBus可以很简洁的实现事件注册监听和消费。Guava框架里面提供了两种相关的实现,一种是单线程同步事件消费,另外一直是多线程异步事件消费。后者在对象设计上是前者的子类,EventBus 异步事件订阅处理通过post发送消息对需要的事件进行注册,在通过事件处理,根据类名,参数去处理注册的事件。
导入Guava 包:
spring 整合Guagva EventBus事件配置文件:
event 事件:
事件注入中心:
异步处理事件,通过 @Subscribe 注解,
异步使用订阅事件:
导入Guava 包:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
spring 整合Guagva EventBus事件配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="demo.dcn"><!-- 扫描一下的包,完成注册bean 并过滤掉控制器扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 线程池 --> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="20" /> <property name="maxPoolSize" value="200" /> <property name="queueCapacity" value="1000000" /> <property name="keepAliveSeconds" value="600" /> <property name="rejectedExecutionHandler"> <!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 --> <!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 --> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> <bean id="userFeedRedisSubscriber" class="demo.dcn.guava.eventBus.subscriber.UserFeedRedisSubscriber" /> <!-- 事件总线 --> <bean id="eventBus" class="com.google.common.eventbus.AsyncEventBus" > <constructor-arg ref="taskExecutor" /> </bean> <!-- 主册事件 --> <bean id="eventBuilder" class="demo.dcn.guava.eventBus.AsynEventBusBuilder"> <property name="eventBus" ref="eventBus"/> <property name="handlers"> <set> <!-- <ref bean="msgNoticeSubscriber"/> <ref bean="userActivitySubcriber"/> --> <ref bean="userFeedRedisSubscriber"/> </set> </property> </bean> </beans>
event 事件:
package demo.dcn.guava.eventBus.events; import java.io.Serializable; /** * 用户喜欢关注,移除关注,触发的交互事件 * @author kun.zhang@downjoy.com * * */ public class FollowEvent implements Serializable{ private static final long serialVersionUID = 1L; // 用户ID private Long lookerId; // 类型 private FriendEventType type; private Long friendId; public FollowEvent(Long lookerId, FriendEventType type, Long friendId) { super(); this.lookerId = lookerId; this.type = type; this.friendId = friendId; } public FollowEvent() { super(); } public enum FriendEventType{ ADD_FOLLOW_FRIEND(1L, "增加关注好友"),ADD_FANS_FRIEND(2L, "增加粉丝好友"),DEL_FOLLOW_FRIEND(3L, "删除关注好友"),DEL_FANS_FRIEND(4L, "删除粉丝好友"); private Long id; private String desc; private FriendEventType(Long id, String desc) { this.id = id; this.desc = desc; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } } }
事件注入中心:
package demo.dcn.guava.eventBus; import java.util.Set; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import com.google.common.eventbus.AsyncEventBus; /** * 事件注入中心 * @author kun.zhang@downjoy.com * * */ public class AsynEventBusBuilder implements InitializingBean,DisposableBean { private AsyncEventBus eventBus; private Set<Object> handlers; @Override public void destroy() throws Exception { } @Override public void afterPropertiesSet() throws Exception { for (Object handler : handlers) { eventBus.register(handler); } } /** * @return the eventBus */ public AsyncEventBus getEventBus() { return eventBus; } /** * @param eventBus the eventBus to set */ public void setEventBus(AsyncEventBus eventBus) { this.eventBus = eventBus; } /** * @return the handlers */ public Set<Object> getHandlers() { return handlers; } /** * @param handlers the handlers to set */ public void setHandlers(Set<Object> handlers) { this.handlers = handlers; } }
异步处理事件,通过 @Subscribe 注解,
package demo.dcn.guava.eventBus.subscriber; import com.google.common.eventbus.AllowConcurrentEvents; import com.google.common.eventbus.Subscribe; import demo.dcn.guava.eventBus.events.FollowEvent; /** * 用户活动订阅事件处理 * @author kun.zhang@downjoy.com * * */ public class UserFeedRedisSubscriber { /** * 处理喜欢关注,移除关注 * @param event */ @Subscribe @AllowConcurrentEvents//线程安全 public void followInteractionHandel(FollowEvent event){ if(event!=null){ System.out.println("用户关注了你");//接送消息系统 } } }
异步使用订阅事件:
@Override public ResultMapper addUserFollow(Long lookerId, Long hosterId) { ResultMapper result = new ResultMapper(); if(lookerId==null||hosterId==null||hosterId==0l||lookerId==0l){ result.setCode(ResultMap.FAILURE.getCode()); result.setMsg(ResultMap.FAILURE.getDesc()); }if(lookerId != null && hosterId != null&&lookerId.equals(hosterId)){ result.setCode(ResultMap.REPETOPER.getCode()); result.setMsg(ResultMap.REPETOPER.getDesc()); return result; } try{ boolean flag = userFollowDAO.addFollow(lookerId, hosterId); if(!flag){ result.setCode(ResultMap.SUCCESS.getCode()); result.setMsg(ResultMap.SUCCESS.getDesc()); } FollowEvent event = new FollowEvent(lookerId,FriendEventType.ADD_FOLLOW_FRIEND,hosterId); eventBus.post(event);//异步处理关联订阅事件 }catch(Exception e){ logger.error("userfeed addFollower error,Long userId={}, Long follerId={}", lookerId, hosterId, e); result.setCode(ResultMap.FAILURE.getCode()); result.setMsg(ResultMap.FAILURE.getDesc()); } return result; }
发表评论
-
Java 设计模式源码
2020-08-17 20:17 141经过一段时间学习和实践,整理了绝大多数设计模式源码demo 。 ... -
Springboot+mybatis 高可用实现数据源配置
2019-01-14 15:37 1621在开发过程中,项目架构上面,由于公司的业务关系 ... -
Centos7 搭建nexus
2019-01-09 19:57 515centos7 搭建 nexus服务器 前面参考文章 htt ... -
Redis 服务器搭建
2019-01-09 15:39 0redis 服务器搭建 参考: https://blog.cs ... -
Centos7 安装mysql
2019-01-09 14:33 365在CentOS中默认安装有Mari ... -
mac ngrok 使用
2018-12-20 18:22 817ngrok 是一个反向代理,通过在公共端点和本 ... -
Java元组学习
2018-12-19 15:38 558在Java 中我们平时用的接口和方法 只是单一 ... -
密码学2 密码安全注意
2018-11-30 15:11 4921.Java API支持 位于java.security包及子 ... -
base64 和 base32 源码解析
2018-11-30 14:19 1411package com.zd.demo; import ... -
maven 常用命令
2018-01-22 14:43 330mvn compile 编译源代码 mvn test-comp ... -
上传excel 通过url下载文件
2017-07-06 16:22 1007/** * 下载图片 */ ... -
多线程实现原理并发机制
2017-03-07 20:29 754进程: 查询百度大致可以理解为一段具有独 ... -
网络编程TCP/IP协议组
2017-03-04 13:42 466TCP/IP是个协议组: 主要可以分为4层,分别是应 ... -
Guava包的ListenableFuture解析
2016-10-09 13:40 984package com.downjoy.test.guava. ... -
httpUtil工具和apche httpclient 工具类使用
2016-09-26 15:38 2331httpUtil请求网络请求工具: package demo ... -
mongodb+spring +morphia完整版框架搭建
2016-09-09 10:22 5659Morphia是一个开放源代 ... -
mongodb注解详解
2016-09-06 09:26 39881、@Entity 如果你想通过Morphia把你的对 ... -
自定义MD5加盐加密方式代码实现
2016-09-02 16:45 5014按照自己的理解对密码加盐加密。当用户注册时候会先生成盐值 ... -
密码学
2016-08-25 11:14 5241 密码学简介 2.1 ... -
kafka
2016-08-11 14:08 656Kafka is a distributed,partiti ...
相关推荐
基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒杀系统。基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒杀系统。基于SpringBoot + MySQL + Redis + RabbitMQ ...
基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒杀系统基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒杀系统基于SpringBoot + MySQL + Redis + RabbitMQ + ...
Google+Guava+官方教程+-+v1.1.rar
基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒
基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒杀系统
本系统是使用SpringBoot开发的高并发限时抢购秒杀系统,除了实现基本的登录、查看商品列表、秒杀、下单等功能,项目中还针对高并发情况实现了系统缓存、降级和限流。
系统介绍 本系统是使用SpringBoot开发的高并发限时抢购秒杀系统,除了实现基本的登录,查看商品列表,秒杀,下单等功能,项目中还针对高并发情况实现了系统缓存,降级和限流。 开发工具 IntelliJ IDEA + Navicat + ...
线上Demo展示项目开发环境IDEA + Maven + Tomcat8 + Git + Mysql + Redis涉及到的相关技术Spring Framework + SpringMVC + MyBatis + Mybatis PageHelper + Redis + Logback +Guava项目目录main ├─java │ └─...
苗沙 基于SpringBoot + MySQL + Redis + RabbitMQ + Guava开发的高并发商品限时秒杀系统
springmvc+国际化i18N+springmvc验证+jetbrick-template使用+@responsebody+谷歌guava: 1)围绕springmvc做的国际化 2)围绕springmvc做的验证 3)使用的jetbrick-template模板引擎 ……
Otto 是基于 Guava 的,增强的事件总线,强调 Android 支持。 Otto 是把应用分离成不同部分,但是仍然可以很好的进行通讯。Otto 是从 Guava forked 而来,但是添加了许多独特的功能,而且精炼了事件总线。下载: ...
spring 3.2.0+hibernate 4.1.6+struts2整合需要的jar包 的目录 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar asm-commons-3.3.jar aspectjweaver-1.5.3.jar bonecp-0.7.1.RELEASE.jar cglib-2.1.3.jar ...
压缩包包含着springboot项目retry,项目中演示了使用spring-retry传统的方式以及注解的方式使用重试的案例,也有guava-retry的使用案例,以及两者的区别
Spring Retry 和 Guava Retrying重试机制的使用详解 内容简介: 1、Spring-Retry优雅地实现重试机制 2、Guava-Retry灵活且强大的重试机制库 3、总结
Spring缓存+番石榴+ Redis