com.google.common.eventbus.SubscriberExceptionHandler Java Examples

The following examples show how to use com.google.common.eventbus.SubscriberExceptionHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultAsyncEventBus.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
public DefaultAsyncEventBus() {
    this.blockingQueue = new LinkedBlockingQueue<>(1000);
    this.executor = new ThreadPoolExecutor(30, 100, 3, TimeUnit.SECONDS, blockingQueue, ThreadPoolUtil.buildThreadFactory("DefaultAsyncEventBus", true), new ThreadPoolExecutor.AbortPolicy());

    this.eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() {
        @Override
        public void handleException(Throwable exception, SubscriberExceptionContext context) {
            log.error("异步消息队列异常: [subscribeMethod={}, event={} ]",context.getSubscriberMethod(), context.getEvent().toString(),exception);
        }
    });

}
 
Example #2
Source File: EventBusFactory.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private static EventBus newEventBus(final String name, final Executor executor) {
  try {
    Class<?> dispatcherClass = EventBus.class.getClassLoader().loadClass("com.google.common.eventbus.Dispatcher");

    // immediate dispatcher means events are always processed in a reentrant fashion
    Method immediateDispatcherMethod = dispatcherClass.getDeclaredMethod("immediate");
    immediateDispatcherMethod.setAccessible(true);

    // EventBus constructor that accepts custom executor is not yet part of the public API
    Constructor<EventBus> eventBusConstructor = EventBus.class.getDeclaredConstructor(
        String.class, Executor.class, dispatcherClass, SubscriberExceptionHandler.class);
    eventBusConstructor.setAccessible(true);

    Object immediateDispatcher = immediateDispatcherMethod.invoke(null);
    SubscriberExceptionHandler exceptionHandler = new Slf4jSubscriberExceptionHandler(name);

    return eventBusConstructor.newInstance(name, executor, immediateDispatcher, exceptionHandler);
  }
  catch (Exception e) {
    Throwables.throwIfUnchecked(e);
    throw new LinkageError("Unable to create EventBus with custom executor", e);
  }
}
 
Example #3
Source File: BlazeWorkspace.java    From bazel with Apache License 2.0 6 votes vote down vote up
public BlazeWorkspace(
    BlazeRuntime runtime,
    BlazeDirectories directories,
    SkyframeExecutor skyframeExecutor,
    SubscriberExceptionHandler eventBusExceptionHandler,
    WorkspaceStatusAction.Factory workspaceStatusActionFactory,
    BinTools binTools,
    @Nullable AllocationTracker allocationTracker) {
  this.runtime = runtime;
  this.eventBusExceptionHandler = Preconditions.checkNotNull(eventBusExceptionHandler);
  this.workspaceStatusActionFactory = workspaceStatusActionFactory;
  this.binTools = binTools;
  this.allocationTracker = allocationTracker;

  this.directories = directories;
  this.skyframeExecutor = skyframeExecutor;

  if (directories.inWorkspace()) {
    writeOutputBaseReadmeFile();
    writeDoNotBuildHereFile();
  }

  // Here we use outputBase instead of outputPath because we need a file system to create the
  // latter.
  this.outputBaseFilesystemTypeName = FileSystemUtils.getFileSystem(getOutputBase());
}
 
Example #4
Source File: PubsubEventModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
@Provides
@RegisteredEvents
@Singleton
EventBus provideRegisteredEventBus(SubscriberExceptionHandler subscriberExceptionHandler,
                                   @DeadEventHandler Object deadEventHandler) {

  EventBus eventBus = new AsyncEventBus(registeredExecutor, subscriberExceptionHandler);
  eventBus.register(deadEventHandler);
  return eventBus;
}
 
Example #5
Source File: PubsubEventModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
EventBus provideEventBus(@AsyncExecutor Executor executor,
                         SubscriberExceptionHandler subscriberExceptionHandler,
                         @DeadEventHandler Object deadEventHandler) {

  EventBus eventBus = new AsyncEventBus(executor, subscriberExceptionHandler);
  eventBus.register(deadEventHandler);
  return eventBus;
}
 
Example #6
Source File: PubsubEventModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
SubscriberExceptionHandler provideSubscriberExceptionHandler(StatsProvider statsProvider) {
  final AtomicLong subscriberExceptions = statsProvider.makeCounter(EXCEPTIONS_STAT);
  return (exception, context) -> {
    subscriberExceptions.incrementAndGet();
    log.error(
        "Failed to dispatch event to " + context.getSubscriberMethod() + ": " + exception,
        exception);
  };
}
 
Example #7
Source File: DefaultEventBus.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
public DefaultEventBus() {
    this.eventBus = new EventBus(new SubscriberExceptionHandler() {
        @Override
        public void handleException(Throwable exception, SubscriberExceptionContext context) {
            log.error("同步消息总线异常: [subscribeMethod={}, event={} ]",context.getSubscriberMethod(),context.getEvent().toString(),exception);
            throw new ServiceException(ErrorCode.BUS_ERROR.getErrorCode(), ErrorCode.BUS_ERROR.getErrorMsg(), exception);
        }
    });
}
 
Example #8
Source File: DefaultAsyncEventBus.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
public DefaultAsyncEventBus() {
    this.blockingQueue = new LinkedBlockingQueue<>(1000);
    this.executor = new ThreadPoolExecutor(30, 100, 3, TimeUnit.SECONDS, blockingQueue, ThreadPoolUtil.buildThreadFactory("DefaultAsyncEventBus", true), new ThreadPoolExecutor.AbortPolicy());

    this.eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() {
        @Override
        public void handleException(Throwable exception, SubscriberExceptionContext context) {
            log.error("异步消息队列异常: [subscribeMethod={}, event={} ]",context.getSubscriberMethod(), context.getEvent().toString(),exception);
        }
    });

}
 
Example #9
Source File: WorkspaceBuilder.java    From bazel with Apache License 2.0 5 votes vote down vote up
BlazeWorkspace build(
    BlazeRuntime runtime,
    PackageFactory packageFactory,
    ConfiguredRuleClassProvider ruleClassProvider,
    SubscriberExceptionHandler eventBusExceptionHandler) throws AbruptExitException {
  // Set default values if none are set.
  if (skyframeExecutorFactory == null) {
    skyframeExecutorFactory =
        new SequencedSkyframeExecutorFactory(runtime.getDefaultBuildOptions());
  }

  SkyframeExecutor skyframeExecutor =
      skyframeExecutorFactory.create(
          packageFactory,
          runtime.getFileSystem(),
          directories,
          runtime.getActionKeyContext(),
          workspaceStatusActionFactory,
          diffAwarenessFactories.build(),
          skyFunctions.build(),
          customDirtinessCheckers.build(),
          managedDirectoriesKnowledge);
  return new BlazeWorkspace(
      runtime,
      directories,
      skyframeExecutor,
      eventBusExceptionHandler,
      workspaceStatusActionFactory,
      binTools,
      allocationTracker);
}
 
Example #10
Source File: TekuDefaultExceptionHandlerTest.java    From teku with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setupBus() {
  lenient()
      .doAnswer(
          invocation -> {
            handledException.complete(invocation.getArgument(1));
            logLevelFuture.complete(Level.WARN);
            return null;
          })
      .when(log)
      .specificationFailure(anyString(), any(Exception.class));
  lenient()
      .doAnswer(
          invocation -> {
            handledException.complete(invocation.getArgument(1));
            logLevelFuture.complete(Level.FATAL);
            return null;
          })
      .when(log)
      .unexpectedFailure(anyString(), any(Exception.class));

  final var exceptionHandlerRecordingWrapper =
      new SubscriberExceptionHandler() {
        private final SubscriberExceptionHandler delegate = new TekuDefaultExceptionHandler(log);

        @Override
        public void handleException(
            final Throwable exception, final SubscriberExceptionContext context) {
          try {
            delegate.handleException(exception, context);
          } catch (final RuntimeException thrown) {
            unhandledExceptionFuture.complete(thrown);
            throw thrown;
          }
        }
      };

  bus = new AsyncEventBus(executor, exceptionHandlerRecordingWrapper);
}
 
Example #11
Source File: EventControllerFactory.java    From Thunder with Apache License 2.0 4 votes vote down vote up
public static EventController createAsyncController(Executor executor, SubscriberExceptionHandler subscriberExceptionHandler) {
    return new EventControllerImpl(new AsyncEventBus(executor, subscriberExceptionHandler));
}
 
Example #12
Source File: BlazeRuntime.java    From bazel with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public Builder setEventBusExceptionHandler(
    SubscriberExceptionHandler eventBusExceptionHandler) {
  this.eventBusExceptionHandler = eventBusExceptionHandler;
  return this;
}
 
Example #13
Source File: BlazeRuntime.java    From bazel with Apache License 2.0 4 votes vote down vote up
private BlazeRuntime(
    FileSystem fileSystem,
    QueryEnvironmentFactory queryEnvironmentFactory,
    ImmutableList<QueryFunction> queryFunctions,
    ImmutableList<OutputFormatter> queryOutputFormatters,
    PackageFactory pkgFactory,
    ConfiguredRuleClassProvider ruleClassProvider,
    ImmutableMap<String, InfoItem> infoItems,
    ActionKeyContext actionKeyContext,
    Clock clock,
    Runnable abruptShutdownHandler,
    OptionsParsingResult startupOptionsProvider,
    Iterable<BlazeModule> blazeModules,
    SubscriberExceptionHandler eventBusExceptionHandler,
    BugReporter bugReporter,
    ProjectFile.Provider projectFileProvider,
    QueryRuntimeHelper.Factory queryRuntimeHelperFactory,
    InvocationPolicy moduleInvocationPolicy,
    Iterable<BlazeCommand> commands,
    String productName,
    BuildEventArtifactUploaderFactoryMap buildEventArtifactUploaderFactoryMap,
    ImmutableMap<String, AuthHeadersProvider> authHeadersProviderMap,
    RepositoryRemoteExecutorFactory repositoryRemoteExecutorFactory,
    Supplier<Downloader> downloaderSupplier) {
  // Server state
  this.fileSystem = fileSystem;
  this.blazeModules = blazeModules;
  overrideCommands(commands);

  this.packageFactory = pkgFactory;
  this.projectFileProvider = projectFileProvider;
  this.queryRuntimeHelperFactory = queryRuntimeHelperFactory;
  this.moduleInvocationPolicy = moduleInvocationPolicy;

  this.ruleClassProvider = ruleClassProvider;
  this.infoItems = infoItems;
  this.actionKeyContext = actionKeyContext;
  this.clock = clock;
  this.abruptShutdownHandler = abruptShutdownHandler;
  this.startupOptionsProvider = startupOptionsProvider;
  this.queryEnvironmentFactory = queryEnvironmentFactory;
  this.queryFunctions = queryFunctions;
  this.queryOutputFormatters = queryOutputFormatters;
  this.eventBusExceptionHandler = eventBusExceptionHandler;
  this.bugReporter = bugReporter;

  CommandNameCache.CommandNameCacheInstance.INSTANCE.setCommandNameCache(
      new CommandNameCacheImpl(getCommandMap()));
  this.productName = productName;
  this.buildEventArtifactUploaderFactoryMap = buildEventArtifactUploaderFactoryMap;
  this.authHeadersProviderMap =
      Preconditions.checkNotNull(authHeadersProviderMap, "authHeadersProviderMap");
  this.repositoryRemoteExecutorFactory = repositoryRemoteExecutorFactory;
  this.downloaderSupplier = downloaderSupplier;
}
 
Example #14
Source File: EventControllerFactory.java    From Thunder with Apache License 2.0 4 votes vote down vote up
public static EventController createSyncController(SubscriberExceptionHandler subscriberExceptionHandler) {
    return new EventControllerImpl(new EventBus(subscriberExceptionHandler));
}
 
Example #15
Source File: EventBusModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Provides @Singleton
EventBus eventBus(SubscriberExceptionHandler exceptionHandler) {
    return new ReentrantEventBus(exceptionHandler);
}
 
Example #16
Source File: EventBusModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void configure() {
    bind(SubscriberExceptionHandler.class).to(EventExceptionHandler.class);
}
 
Example #17
Source File: ReentrantEventBus.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public ReentrantEventBus(SubscriberExceptionHandler subscriberExceptionHandler) {
    super(subscriberExceptionHandler);
    this.isDispatching = getIsDispatching();
}
 
Example #18
Source File: BlazeModule.java    From bazel with Apache License 2.0 2 votes vote down vote up
/**
 * Returns handler for {@link com.google.common.eventbus.EventBus} subscriber and async thread
 * exceptions. For async thread exceptions, {@link
 * SubscriberExceptionHandler#handleException(Throwable,
 * com.google.common.eventbus.SubscriberExceptionContext)} will be called with null {@link
 * com.google.common.eventbus.SubscriberExceptionContext}. If all modules return null, a handler
 * that crashes on all async exceptions and files bug reports for all EventBus subscriber
 * exceptions will be used.
 */
public SubscriberExceptionHandler getEventBusAndAsyncExceptionHandler() {
  return null;
}