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 Details

    • getId

      int getId()
    • register

      void register(Object eventBusSubscriber)
      Register a subscriber
      Parameters:
      eventBusSubscriber - The subscriber
    • listSubscriber

      Collection<Subscriber> listSubscriber(EventBusMessage eventBusMessage)
      Subscribers corresponding to the event message
      Parameters:
      eventBusMessage - The event message
      Returns:
      The corresponding subscribers
    • listTopic

      Set<String> listTopic()
      All event source topics subscribed by the current eventBus
      Returns:
      All event source topics subscribed by the current eventBus
    • setStatus

      void setStatus(EventBusStatus status)
    • fire

      void fire(EventBusMessage eventBusMessage)
      [Asynchronous] Send an event to all subscribers
      1. 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

      void fireSync(EventBusMessage eventBusMessage)
      [Synchronous] Send an event to all subscribers
      1. [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

      default void fire(Object eventSource)
      [Asynchronous] Send an event to all subscribers
      1. 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

      default void fireSync(Object eventSource)
      [Synchronous] Send an event to all subscribers
      1. [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

      default void fireLocal(Object eventSource)
      [Asynchronous] Send event messages to subscribers of all logic servers in the current process
      Parameters:
      eventSource - The event source
    • fireLocal

      void fireLocal(EventBusMessage eventBusMessage)
      [Asynchronous] Send event messages to subscribers of all logic servers in the current process
      Parameters:
      eventBusMessage - The event message
    • fireLocalSync

      default void fireLocalSync(Object eventSource)
      [Synchronous] Send event messages to subscribers of all logic servers in the current process
      Parameters:
      eventSource - The event source
    • fireLocalSync

      void fireLocalSync(EventBusMessage eventBusMessage)
      [Synchronous] Send event messages to subscribers of all logic servers in the current process
      Parameters:
      eventBusMessage - The event message
    • fireMe

      default void fireMe(Object eventSource)
      [Asynchronous] Send event messages only to the subscribers of the current EventBus
      Subscribers that have been registered with register(Object)
      
      Parameters:
      eventSource - The event source
    • fireMe

      void fireMe(EventBusMessage eventBusMessage)
      [Asynchronous] Send event messages only to the subscribers of the current EventBus
      Subscribers that have been registered with register(Object)
      
      Parameters:
      eventBusMessage - The event message
    • fireMeSync

      default void fireMeSync(Object eventSource)
      [Synchronous] Send event messages only to the subscribers of the current EventBus
      Subscribers that have been registered with register(Object)
      
      Parameters:
      eventSource - The event source
    • fireMeSync

      void fireMeSync(EventBusMessage eventBusMessage)
      [Synchronous] Send event messages only to the subscribers of the current EventBus
      Subscribers that have been registered with register(Object)
      
      Parameters:
      eventBusMessage - The event message
    • fireAny

      default void fireAny(Object eventSource)
      [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

      void fireAny(EventBusMessage eventBusMessage)
      [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

      default void fireAnySync(Object eventSource)
      [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

      void fireAnySync(EventBusMessage eventBusMessage)
      [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

      void setSubscribeExecutorStrategy(SubscribeExecutorStrategy subscribeExecutorStrategy)
      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

      void setSubscriberInvokeCreator(SubscriberInvokeCreator subscriberInvokeCreator)
      Set SubscriberInvokeCreator
      Parameters:
      subscriberInvokeCreator - SubscriberInvokeCreator
    • setEventBusMessageCreator

      void setEventBusMessageCreator(EventBusMessageCreator eventBusMessageCreator)
      Set the event message creator, EventBusMessage creator
      Parameters:
      eventBusMessageCreator - EventBusMessageCreator
    • setEventBusListener

      void setEventBusListener(EventBusListener eventBusListener)
      Set the event listener
      Parameters:
      eventBusListener - The event listener
    • getEventBusListener

      EventBusListener getEventBusListener()
      Get the event listener
      Returns:
      The event listener
    • setEventServerMessage

      void setEventServerMessage(EventServerMessage eventServerMessage)
      Set the event bus logic server related information
      Parameters:
      eventServerMessage - The event bus logic server related information
    • setCommunication

      void setCommunication(EventBusMessageCommunication communication)
    • setExecutorRegion

      void setExecutorRegion(ExecutorRegion executorRegion)
      Set the executor region
      Parameters:
      executorRegion - The executor region
    • getExecutorRegion

      ExecutorRegion getExecutorRegion()
      Get the executor region
      Returns:
      The executor region
    • 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

      Class<?> getTopicClass(String topic)
    • createEventBusMessage

      default EventBusMessage createEventBusMessage(Object eventSource)
      Create an event message
      Parameters:
      eventSource - The event source
      Returns:
      The event message