SlideShare une entreprise Scribd logo
1  sur  22
Guava’s Event Bus
1
Traditional Events
Listener Listener
Activity
Service / Helper
Thread
Activity
Communication Issues?!
• Tight coupling of components
 Inflexible, changes are expensive
• Boiler plate code
– Define interfaces
– Callbacks for asynch. communication
– Listener management
– Propagation through all layers
EventBus Communication
Fragment Fragment
Activity
Service / Helper
Thread
Activity
Event
Bus
Guava’s EventBus
• A message dispatching system
– to allow publish-subscribe style of
communication between components
– No-nonsense
– Lightweight
– and very practical
• Everything happens within the run-time
boundaries of the same Java application.
https://code.google.com/p/guava-
libraries/wiki/EventBusExplained
Guava’s EventBus
• The Event Bus comes in two flavours:
– Synchronous (backed-up by the EventBus class), and
– Asynchronous (backed-up by the AsyncEventBus class which
extends EventBus)
• com.google.common.eventbus package
• Exposed API
– void register(Object) - registers and caches
subscribers for subsequent event handling
– void unregister(Object) - undoes the register action
– void post(Object) - posts an event (event) to all
registered subscribers
Building an Event Bus
• The Guava’s Event (Message) Bus itself
• The event (message), which can be any Java
object: a Date, a String, your POJO etc…
• The event subscriber (listener) – any
complexity Java class that must have a specially
annotated method for handling events (messages);
EventBus Configuration (Spring way)
• EventBus instances will create when
application is deploying
<bean id="eventBus" class="com.google.common.even
tbus.EventBus" />
<bean id="asyncEventBus"
class="com.google.common.eventbus.AsyncEventBus">
<constructor-arg name="executor" ref="executorService" />
</bean>
EventBus : Listener
• Define an event handler
@Component
Public class EventHandler
{
}
Event
Handler
E
v
e
n
t
B
u
s
EventBus: Listener Registration
• Register handler
@PostConstruct
public void registerHandler()
{
asyncEventBus.register(this);
}
Event
Handler
E
v
e
n
t
B
u
s
register
EventBus: Listener Subscription
• Subscribe for events a.k.a objects
@Subscribe
@AllowConcurrentEvents
public void handleEventMethod(ObjectA objA)
{
}
Event
Handler
E
v
e
n
t
B
u
s
subscribe
register
EventBus: Event Post
• Post an event
// Post information back to event bus
asyncEventBus.post(objA);
Event
Handler
E
v
e
n
t
B
u
s
subscribe
registerEvent
post
Guava Event Bus
E
v
e
n
t
B
u
s
Event
Event
Event
Event
Handler
Event
Handler
Event
Handler
post
post
post
subscribe
subscribe
subscribe
register
register
register
Event Handler Method
• EventBus scans subscribers
– During (first) registration of subscriber and
registers the event listener methods based on
the method’s parameter type
• Event handler methods
– public visibility
– No return value (void)
– Single parameter for the event to receive
14
Type-based Event Routing
• Event type: Java class of the event
• To receive an event, its type must match
• E.g.
post(new User());
handleUserXXX(User user) {…}
Post(new Address());
handleAddressXXX(Address address) {…}
Publish / Subscribe
Publisher
Event
Bus
Publish / Subscribe
Publisher
Event
Bus
Event
post(Object1)
Publish / Subscribe
Publisher
Event
Bus
Subscriber
Event
post(Object1)
Event
handleMethod1(Object1)
Subscriber
Event
handleMethod2(Object1)
Event Type is a Filter
Publisher
Event
Bus
Subscriber
Event
post
(user)
Event
handleUserXXX(User)
Subscriber
handleAddressXXX(Address)
It‘s time to see some
• CODE
EventBus Code: Sender (Spring)
@Autowired
private EventBus eventBus;
…
eventBus.post(user);
…
OR
@Autowired
private AsyncEventBus asyncEventBus;
…
asyncEventBus.post(user);
…
EventBus Code: Receiver (Spring)
@Component
public class UserListener
{
@Autowired
private AsyncEventBus asyncEventBus;
@PostConstruct
public void registerHandler()
{
asyncEventBus.register(this);
}
@PreDestroy
public void unRegisterHandler()
{
asyncEventBus.unregister(this);
}
@Subscribe
@AllowConcurrentEvents
public void handleUserXXX(User user)
{
// your logic
}
}

Contenu connexe

Tendances

Tendances (20)

ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
Infrastructure as Code with Ansible
Infrastructure as Code with AnsibleInfrastructure as Code with Ansible
Infrastructure as Code with Ansible
 
Rest API
Rest APIRest API
Rest API
 
Microservice Architecture
Microservice ArchitectureMicroservice Architecture
Microservice Architecture
 
Finally, easy integration testing with Testcontainers
Finally, easy integration testing with TestcontainersFinally, easy integration testing with Testcontainers
Finally, easy integration testing with Testcontainers
 
Microservices Security
Microservices SecurityMicroservices Security
Microservices Security
 
Why Microservice
Why Microservice Why Microservice
Why Microservice
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Microservice Architecture
Microservice ArchitectureMicroservice Architecture
Microservice Architecture
 
Cross-Browser-Testing with Protractor & Browserstack
Cross-Browser-Testing with Protractor & BrowserstackCross-Browser-Testing with Protractor & Browserstack
Cross-Browser-Testing with Protractor & Browserstack
 
Azure Container Services
Azure Container Services Azure Container Services
Azure Container Services
 
Flutter
FlutterFlutter
Flutter
 
Micro services Architecture
Micro services ArchitectureMicro services Architecture
Micro services Architecture
 
Microservices Design Patterns | Edureka
Microservices Design Patterns | EdurekaMicroservices Design Patterns | Edureka
Microservices Design Patterns | Edureka
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022
 
RichFaces 4: Rich Ajax Components For Your JSF Applications
RichFaces 4: Rich Ajax Components For Your JSF ApplicationsRichFaces 4: Rich Ajax Components For Your JSF Applications
RichFaces 4: Rich Ajax Components For Your JSF Applications
 
From Monolithic to Microservices
From Monolithic to Microservices From Monolithic to Microservices
From Monolithic to Microservices
 

Similaire à Guava’s Event Bus

Similaire à Guava’s Event Bus (20)

Infinum Android Talks #02 - EventBus
Infinum Android Talks #02 - EventBusInfinum Android Talks #02 - EventBus
Infinum Android Talks #02 - EventBus
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
 
Guaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, Nutanix
Guaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, NutanixGuaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, Nutanix
Guaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, Nutanix
 
Komunikacja oparta o zdarzenia z wykorzystaniem AWS Event Bridge
Komunikacja oparta o zdarzenia z wykorzystaniem AWS Event BridgeKomunikacja oparta o zdarzenia z wykorzystaniem AWS Event Bridge
Komunikacja oparta o zdarzenia z wykorzystaniem AWS Event Bridge
 
GreenRobot-Eventbus
GreenRobot-EventbusGreenRobot-Eventbus
GreenRobot-Eventbus
 
Eventbus Library and How Does it Work?
Eventbus Library and How Does it Work?Eventbus Library and How Does it Work?
Eventbus Library and How Does it Work?
 
Multi-Process JavaScript Architectures
Multi-Process JavaScript ArchitecturesMulti-Process JavaScript Architectures
Multi-Process JavaScript Architectures
 
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
Winning the Lottery with Spring: A Microservices Case Study for the Dutch Lot...
 
Explained: Domain events
Explained: Domain eventsExplained: Domain events
Explained: Domain events
 
Scala API - Azure Event Hub Integration
Scala API - Azure Event Hub IntegrationScala API - Azure Event Hub Integration
Scala API - Azure Event Hub Integration
 
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
 
Automating Research Data Flows and an Introduction to the Globus Platform
Automating Research Data Flows and an Introduction to the Globus PlatformAutomating Research Data Flows and an Introduction to the Globus Platform
Automating Research Data Flows and an Introduction to the Globus Platform
 
AxonHub beta release 11 april 2018
AxonHub beta release 11 april 2018AxonHub beta release 11 april 2018
AxonHub beta release 11 april 2018
 
Istio Playground
Istio PlaygroundIstio Playground
Istio Playground
 
Actors or Not: Async Event Architectures
Actors or Not: Async Event ArchitecturesActors or Not: Async Event Architectures
Actors or Not: Async Event Architectures
 
Band of brothers, building scalable social web apps on windows azure with asp...
Band of brothers, building scalable social web apps on windows azure with asp...Band of brothers, building scalable social web apps on windows azure with asp...
Band of brothers, building scalable social web apps on windows azure with asp...
 
Service workers
Service workersService workers
Service workers
 
Automating Research Data Flows and Introduction to the Globus Platform
Automating Research Data Flows and Introduction to the Globus PlatformAutomating Research Data Flows and Introduction to the Globus Platform
Automating Research Data Flows and Introduction to the Globus Platform
 
Timeline Service v.2 (Hadoop Summit 2016)
Timeline Service v.2 (Hadoop Summit 2016)Timeline Service v.2 (Hadoop Summit 2016)
Timeline Service v.2 (Hadoop Summit 2016)
 
Timeline service V2 at the Hadoop Summit SJ 2016
Timeline service V2 at the Hadoop Summit SJ 2016Timeline service V2 at the Hadoop Summit SJ 2016
Timeline service V2 at the Hadoop Summit SJ 2016
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Guava’s Event Bus

Notes de l'éditeur

  1. TraditionalAs stated above — the traditional method requires an interface declaration, an explicit subscription, and knowledge of the object that is posting the particular event. Additionally, it forces the object that is posting the event to invent its own method of publishing the event.
  2. The Guava’s Event (Message) Bus itself – represented by the EventBus class that provides methods for subscribers (event listeners) to register and unregister themselves with the Bus as well as a method for dispatching events (messages) to the target subscribers The event (message), which can be any Java object: a Date, a String, your POJO, anything that can be routed by the Bus to your subscriber The event subscriber (listener) – an arbitrary complexity Java class that must have a specially annotated method for handling events (messages); this method is a call-back function that must return void and take one parameter of the same type as the type of the corresponding event (a Date, a String, your POJO, etc.)