Interface EventBus
public interface EventBus
EventBus, the relationship between EventBus, the business framework, and the logic server is 1:1:1.
When publishing events:
1. If the relevant subscribers are within the same process, synchronous and asynchronous sending can be controlled.
2. If the relevant subscribers are not in the same process but are distributed across different processes, only asynchronous sending is possible (even if a synchronous method is used to publish the event).
【Synchronous】 here means: when publishing an event, the main logic will only continue to proceed after the relevant subscribers have completed their execution.
【Asynchronous】 here means: when publishing an event, the main logic will not be blocked, and the relevant subscribers will execute in other threads.
Whether synchronous or asynchronous, when the relevant subscribers execute the logic server, it is thread-safe by default; this is because the subscriber EventSubscribe uses the user thread executor by default.
Related examples for obtaining EventBus
for example 1 - Get the corresponding eventBus through FlowContext
EventBus eventBus = flowContext.getEventBus();
eventBus.fire(userLoginEventMessage);
The fire series provides multiple types of event publishing mechanisms:
1. fire sends events to subscribers, which include:
a. Sending event messages to subscribers of all logic servers in the current process.
b. Sending event messages to subscribers in other processes.
2. fireLocal sends event messages to subscribers of all logic servers in the current process.
3. fireMe sends event messages only to the subscribers of the current EventBus.
4. fireAny sends events to subscribers, which include:
a. Sending event messages to subscribers of all logic servers in the current process.
b. Sending event messages to subscribers in other processes.
c. When there are multiple logic server instances of the same type, the event will only be sent to one of the logic servers of that type.
The fire series provides multiple types of event publishing mechanisms. The above methods are asynchronous by default, and the corresponding synchronous methods are named as fireXXXSync.
Convenient usage - FlowContext
In addition to publishing events via EventBus, the framework also provides EventBus-related methods in FlowContext.
FlowContext internally uses EventBus to publish events.
- Since:
- 21
- Author:
- 渔民小镇
- See Also:
- date:
- 2023-12-24
-
Method Summary
Modifier and TypeMethodDescriptiondefault EventBusMessagecreateEventBusMessage(Object eventSource) Create an event messagevoidfire(EventBusMessage eventBusMessage) [Asynchronous] Send an event to all subscribersdefault void[Asynchronous] Send an event to all subscribersvoidfireAny(EventBusMessage eventBusMessage) [Asynchronous] Send event messages to subscribers in the current process and subscribers in remote processes.default void[Asynchronous] Send event messages to subscribers in the current process and subscribers in remote processes.voidfireAnySync(EventBusMessage eventBusMessage) [Synchronous] Send event messages to subscribers in the current process and subscribers in remote processes.default voidfireAnySync(Object eventSource) [Synchronous] Send event messages to subscribers in the current process and subscribers in remote processes.voidfireLocal(EventBusMessage eventBusMessage) [Asynchronous] Send event messages to subscribers of all logic servers in the current processdefault void[Asynchronous] Send event messages to subscribers of all logic servers in the current processvoidfireLocalSync(EventBusMessage eventBusMessage) [Synchronous] Send event messages to subscribers of all logic servers in the current processdefault voidfireLocalSync(Object eventSource) [Synchronous] Send event messages to subscribers of all logic servers in the current processvoidfireMe(EventBusMessage eventBusMessage) [Asynchronous] Send event messages only to the subscribers of the current EventBusdefault void[Asynchronous] Send event messages only to the subscribers of the current EventBusvoidfireMeSync(EventBusMessage eventBusMessage) [Synchronous] Send event messages only to the subscribers of the current EventBusdefault voidfireMeSync(Object eventSource) [Synchronous] Send event messages only to the subscribers of the current EventBusvoidfireSync(EventBusMessage eventBusMessage) [Synchronous] Send an event to all subscribersdefault void[Synchronous] Send an event to all subscribersGet the event listenerGet the event message creatorGet the event bus logic server related informationGet the executor regionintgetId()Get the subscriber thread executor selection strategyClass<?> getTopicClass(String topic) listSubscriber(EventBusMessage eventBusMessage) Subscribers corresponding to the event messageAll event source topics subscribed by the current eventBusvoidRegister a subscribervoidsetCommunication(EventBusMessageCommunication communication) voidsetEventBusListener(EventBusListener eventBusListener) Set the event listenervoidsetEventBusMessageCreator(EventBusMessageCreator eventBusMessageCreator) Set the event message creator, EventBusMessage creatorvoidsetEventServerMessage(EventServerMessage eventServerMessage) Set the event bus logic server related informationvoidsetExecutorRegion(ExecutorRegion executorRegion) Set the executor regionvoidsetStatus(EventBusStatus status) voidsetSubscribeExecutorStrategy(SubscribeExecutorStrategy subscribeExecutorStrategy) Set the subscriber thread executor selection strategyvoidsetSubscriberInvokeCreator(SubscriberInvokeCreator subscriberInvokeCreator) Set SubscriberInvokeCreator
-
Method Details
-
getId
int getId() -
register
Register a subscriber- Parameters:
eventBusSubscriber- The subscriber
-
listSubscriber
Subscribers corresponding to the event message- Parameters:
eventBusMessage- The event message- Returns:
- The corresponding subscribers
-
listTopic
-
setStatus
-
fire
[Asynchronous] Send an event to all subscribers1. Send event messages to subscribers of all logic servers in the current process. 2. Send event messages to subscribers in other processes.
- Parameters:
eventBusMessage- The event message
-
fireSync
[Synchronous] Send an event to all subscribers1. [Synchronous] Send event messages to subscribers of all logic servers in the current process. 2. [Asynchronous] Send event messages to subscribers in other processes. Note that the synchronization here only refers to the synchronization of subscribers within the current process, and is invalid for subscribers in other processes (asynchronous is used for handling remote subscribers).
- Parameters:
eventBusMessage- The event message
-
fire
[Asynchronous] Send an event to all subscribers1. Send event messages to subscribers of all logic servers in the current process. 2. Send event messages to subscribers in other processes.
- Parameters:
eventSource- The event source
-
fireSync
[Synchronous] Send an event to all subscribers1. [Synchronous] Send event messages to subscribers of all logic servers in the current process. 2. [Asynchronous] Send event messages to subscribers in other processes. Note that the synchronization here only refers to the synchronization of subscribers within the current process, and is invalid for subscribers in other processes (asynchronous is used for handling remote subscribers).
- Parameters:
eventSource- The event source
-
fireLocal
[Asynchronous] Send event messages to subscribers of all logic servers in the current process- Parameters:
eventSource- The event source
-
fireLocal
[Asynchronous] Send event messages to subscribers of all logic servers in the current process- Parameters:
eventBusMessage- The event message
-
fireLocalSync
[Synchronous] Send event messages to subscribers of all logic servers in the current process- Parameters:
eventSource- The event source
-
fireLocalSync
[Synchronous] Send event messages to subscribers of all logic servers in the current process- Parameters:
eventBusMessage- The event message
-
fireMe
[Asynchronous] Send event messages only to the subscribers of the current EventBusSubscribers that have been registered with
register(Object)- Parameters:
eventSource- The event source
-
fireMe
[Asynchronous] Send event messages only to the subscribers of the current EventBusSubscribers that have been registered with
register(Object)- Parameters:
eventBusMessage- The event message
-
fireMeSync
[Synchronous] Send event messages only to the subscribers of the current EventBusSubscribers that have been registered with
register(Object)- Parameters:
eventSource- The event source
-
fireMeSync
[Synchronous] Send event messages only to the subscribers of the current EventBusSubscribers that have been registered with
register(Object)- Parameters:
eventBusMessage- The event message
-
fireAny
[Asynchronous] Send event messages to subscribers in the current process and subscribers in remote processes. If multiple logic server instances of the same type exist, the event will only be sent to one of the instances.Suppose there is a mail logic server for distributing rewards, and we have started two (or more) mail logic server instances to handle the business. When we use the fireAny method to send an event, the event will only be sent to one of the instances.
- Parameters:
eventSource- The event source
-
fireAny
[Asynchronous] Send event messages to subscribers in the current process and subscribers in remote processes. If multiple logic server instances of the same type exist, the event will only be sent to one of the instances.Suppose there is a mail logic server for distributing rewards, and we have started two (or more) mail logic server instances to handle the business. When we use the fireAny method to send an event, the event will only be sent to one of the instances.
- Parameters:
eventBusMessage- The event message
-
fireAnySync
[Synchronous] Send event messages to subscribers in the current process and subscribers in remote processes. If multiple logic server instances of the same type exist, the event will only be sent to one of the instances.Suppose there is a mail logic server for distributing rewards, and we have started two (or more) mail logic server instances to handle the business. When we use the fireAny method to send an event, the event will only be sent to one of the instances.
- Parameters:
eventSource- The event source
-
fireAnySync
[Synchronous] Send event messages to subscribers in the current process and subscribers in remote processes. If multiple logic server instances of the same type exist, the event will only be sent to one of the instances.Suppose there is a mail logic server for distributing rewards, and we have started two (or more) mail logic server instances to handle the business. When we use the fireAny method to send an event, the event will only be sent to one of the instances.
- Parameters:
eventBusMessage- The event message
-
setSubscribeExecutorStrategy
Set the subscriber thread executor selection strategy- Parameters:
subscribeExecutorStrategy- The subscriber thread executor selection strategy
-
getSubscribeExecutorStrategy
SubscribeExecutorStrategy getSubscribeExecutorStrategy()Get the subscriber thread executor selection strategy- Returns:
- The subscriber thread executor selection strategy
-
setSubscriberInvokeCreator
Set SubscriberInvokeCreator- Parameters:
subscriberInvokeCreator- SubscriberInvokeCreator
-
setEventBusMessageCreator
Set the event message creator, EventBusMessage creator- Parameters:
eventBusMessageCreator- EventBusMessageCreator
-
setEventBusListener
Set the event listener- Parameters:
eventBusListener- The event listener
-
getEventBusListener
-
setEventServerMessage
Set the event bus logic server related information- Parameters:
eventServerMessage- The event bus logic server related information
-
setCommunication
-
setExecutorRegion
Set the executor region- Parameters:
executorRegion- The executor region
-
getExecutorRegion
-
getEventBusMessageCreator
EventBusMessageCreator getEventBusMessageCreator()Get the event message creator- Returns:
- The event message creator
-
getEventServerMessage
EventServerMessage getEventServerMessage()Get the event bus logic server related information- Returns:
- The event bus logic server related information
-
getTopicClass
-
createEventBusMessage
Create an event message- Parameters:
eventSource- The event source- Returns:
- The event message
-