Enum Class ExecutorSelector

java.lang.Object
java.lang.Enum<ExecutorSelector>
com.iohao.net.framework.communication.eventbus.ExecutorSelector
All Implemented Interfaces:
Serializable, Comparable<ExecutorSelector>, Constable

public enum ExecutorSelector extends Enum<ExecutorSelector>
Subscriber thread executor selection strategy.

for example

public class YourEventBusSubscriber implements EventBusSubscriber {
    // Specify the thread executor to execute the subscriber's logic
    @EventSubscribe(ExecutorSelector.userExecutor)
    public void userLogin(YourEventMessage message) {
        log.info("event - User [{}] logged in", message.getUserId());
    }
}

@Data
public class YourEventMessage {
    final long userId;
    public YourEventMessage(long userId) {
        this.userId = userId;
    }
}

Since:
21
Author:
渔民小镇
See Also:
date:
2024-01-11
  • Enum Constant Details

    • userExecutor

      public static final ExecutorSelector userExecutor
      [Thread Safe] Execute in the user thread executor
      This strategy will use the action's thread executor to ensure that the same user (userId),
      when consuming events and actions, uses the same thread executor to avoid concurrency issues.
      
      Note: Do not perform time-consuming IO-related operations to avoid blocking the consumption of actions.
      
      See Also:
    • userVirtualExecutor

      public static final ExecutorSelector userVirtualExecutor
      Execute in the virtual thread executor
      Time-consuming operations can choose this strategy
      
      See Also:
    • methodExecutor

      public static final ExecutorSelector methodExecutor
      [Thread Safe] Execute in a thread executor
      This strategy will use the Subscriber.id to determine the thread executor, ensuring that the same subscriber method,
      when consuming events, uses the same thread executor to avoid concurrency issues.
      
      Note: Do not perform time-consuming IO-related operations to avoid blocking the consumption of other subscribers.
      
      Other supplementary explanation:
      Subscriber.id is allocated by the framework. This strategy is similar to userExecutor and simpleExecutor.
      userExecutor and simpleExecutor use userId to determine the thread executor,
      while methodExecutor uses the subscriber's own Subscriber.id to determine the thread executor (you can think of it as being partitioned by subscriber method).
      
      See Also:
    • simpleExecutor

      public static final ExecutorSelector simpleExecutor
      [Thread Safe] Execute in a thread executor
      This strategy is similar to userExecutor, but uses an independent thread executor (ExecutorRegion.getSimpleThreadExecutorRegion()).
      When using, the developer needs to set the value of EventBusMessage.threadIndex (this value needs to be > 0).
      
      See Also:
    • customExecutor

      public static final ExecutorSelector customExecutor
      Reserved for developers
      If the above strategies do not meet the business requirements, developers can implement the SubscribeExecutorStrategy interface for custom extension
      
      example
      // The logic server adds EventBusRunner to handle EventBus related business
      builder.addRunner(new AbstractEventBusRunner() {
          @Override
          public void registerEventBus(EventBus eventBus, BarSkeleton skeleton) {
              // Your thread executor selection strategy
              eventBus.setSubscribeExecutorStrategy(new YourSubscribeExecutorStrategy());
              }
          });
      
      
      See Also:
  • Method Details

    • values

      public static ExecutorSelector[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static ExecutorSelector valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null