Example usage for com.google.common.util.concurrent ThreadFactoryBuilder setUncaughtExceptionHandler

List of usage examples for com.google.common.util.concurrent ThreadFactoryBuilder setUncaughtExceptionHandler

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ThreadFactoryBuilder setUncaughtExceptionHandler.

Prototype

public ThreadFactoryBuilder setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) 

Source Link

Document

Sets the UncaughtExceptionHandler for new threads created with this ThreadFactory.

Usage

From source file:org.apache.giraph.utils.ThreadUtils.java

/**
 * Creates new thread factory with specified thread name format.
 *
 * @param nameFormat defines naming format for threads created by
 *                   thread factory/*from   ww w .j a  va  2s  .c  o m*/
 * @param exceptionHandler handles uncaught exceptions in all threads
 *                         produced created thread factory
 * @return new thread factory with specified thread name format and
 * exception handler.
 */
public static ThreadFactory createThreadFactory(String nameFormat,
        Thread.UncaughtExceptionHandler exceptionHandler) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setNameFormat(nameFormat).setDaemon(true);
    if (exceptionHandler != null) {
        builder.setUncaughtExceptionHandler(exceptionHandler);
    }
    return builder.build();
}

From source file:com.google.firebase.internal.FirebaseScheduledExecutor.java

private static ThreadFactory decorateThreadFactory(ThreadFactory threadFactory, String name,
        Thread.UncaughtExceptionHandler handler) {
    checkArgument(!Strings.isNullOrEmpty(name));
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder().setThreadFactory(threadFactory)
            .setNameFormat(name).setDaemon(true);
    if (handler != null) {
        builder.setUncaughtExceptionHandler(handler);
    }//w  ww  .j a va2 s. c  om
    return builder.build();
}

From source file:gobblin.util.ExecutorsUtils.java

private static ThreadFactory newThreadFactory(ThreadFactoryBuilder builder, Optional<Logger> logger,
        Optional<String> nameFormat) {
    if (nameFormat.isPresent()) {
        builder.setNameFormat(nameFormat.get());
    }/* w ww  .j  a  v  a 2 s  .co m*/
    return builder.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler(logger)).build();
}

From source file:com.notifier.desktop.ApplicationModule.java

@Override
protected void configure() {
    bind(Application.class);

    bind(SwtManager.class).to(SwtManagerImpl.class);
    bind(TrayManager.class).to(SwtTrayManager.class);
    bind(PreferencesDialog.class);

    bind(NotificationManager.class).to(NotificationManagerImpl.class);
    bind(new TypeLiteral<NotificationParser<byte[]>>() {
    }).to(MultiNotificationParser.class);
    bind(DeviceManager.class).to(DeviceManagerImpl.class);

    bind(NotificationBroadcaster.class).annotatedWith(Tray.class).to(TrayNotificationBroadcaster.class);
    bind(NotificationBroadcaster.class).annotatedWith(Growl.class).to(GrowlNotificationBroadcaster.class);
    bind(NotificationBroadcaster.class).annotatedWith(Libnotify.class)
            .to(LibnotifyNotificationBroadcaster.class);
    bind(InstantMessagingNotificationBroadcaster.class).annotatedWith(Msn.class)
            .to(MsnNotificationBroadcaster.class);

    bind(WifiTransport.class).to(NioWifiTransport.class);
    bind(BluetoothTransport.class).to(BluetoothTransportImpl.class);
    bind(UsbTransport.class).to(UsbTransportImpl.class);
    bind(UsbPortClient.class);

    bind(NetworkManager.class).to(NetworkManagerImpl.class);
    bind(UpnpManager.class).to(UpnpManagerImpl.class);
    bind(UpdateManager.class).to(UpdateManagerImpl.class);
    bind(ServiceServer.class).to(ServiceServerImpl.class);
    bind(OperatingSystemProcessManager.class).to(OperatingSystemProcessManagerImpl.class);

    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    threadFactoryBuilder.setNameFormat("task-%s");
    threadFactoryBuilder.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override/*  w w  w  . j  a  v  a  2s. c o  m*/
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("Uncaught exception", e);
        }
    });
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4,
            threadFactoryBuilder.build());
    bind(ExecutorService.class).toInstance(executorService);
    bind(ScheduledExecutorService.class).toInstance(executorService);
}

From source file:com.alibaba.wasp.master.BulkAssigner.java

/**
 * Run the bulk assign.//w w  w . j a va  2 s.  c o m
 * 
 * @param sync
 *          Whether to assign synchronously.
 * @throws InterruptedException
 * @return True if done.
 * @throws java.io.IOException
 */
public boolean bulkAssign(boolean sync) throws InterruptedException, IOException {
    boolean result = false;
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setDaemon(true);
    builder.setNameFormat(getThreadNamePrefix() + "-%1$d");
    builder.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
    int threadCount = getThreadCount();
    java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(threadCount, builder.build());
    try {
        populatePool(pool);
        // How long to wait on empty entityGroups-in-transition.  If we timeout, the
        // RIT monitor should do fixup.
        if (sync)
            result = waitUntilDone(getTimeoutOnRIT());
    } finally {
        // We're done with the pool.  It'll exit when its done all in queue.
        pool.shutdown();
    }
    return result;
}

From source file:org.apache.hadoop.hbase.master.BulkAssigner.java

/**
 * Run the bulk assign.//from   www.j  a v  a 2  s . co  m
 * 
 * @param sync
 *          Whether to assign synchronously.
 * @throws InterruptedException
 * @return True if done.
 * @throws IOException
 */
public boolean bulkAssign(boolean sync) throws InterruptedException, IOException {
    boolean result = false;
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setDaemon(true);
    builder.setNameFormat(getThreadNamePrefix() + "-%1$d");
    builder.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
    int threadCount = getThreadCount();
    java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(threadCount, builder.build());
    try {
        populatePool(pool);
        // How long to wait on empty regions-in-transition.  If we timeout, the
        // RIT monitor should do fixup.
        if (sync)
            result = waitUntilDone(getTimeoutOnRIT());
    } finally {
        // We're done with the pool.  It'll exit when its done all in queue.
        pool.shutdown();
    }
    return result;
}

From source file:org.opendaylight.netvirt.vpnmanager.ArpScheduler.java

private ThreadFactory getThreadFactory(String threadNameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat(threadNameFormat);
    builder.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override//from  ww  w.j  a  va 2 s  .  c om
        public void uncaughtException(Thread t, Throwable e) {
            LOG.error("Received Uncaught Exception event in Thread: {}", t.getName(), e);
        }
    });
    return builder.build();
}

From source file:org.eclipse.che.api.vfs.watcher.FileWatcherService.java

@PostConstruct
void start() throws IOException {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    ThreadFactory factory = builder.setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance())
            .setNameFormat(FileWatcherService.class.getSimpleName()).setDaemon(true).build();
    executor = newSingleThreadExecutor(factory);
    executor.execute(this::run);
}

From source file:org.opendaylight.vpnservice.interfacemgr.pmcounters.NodeConnectorStatsImpl.java

private ThreadFactory getThreadFactory(String threadNameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat(threadNameFormat);
    builder.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override/*from  www. j a  v a2  s  .co  m*/
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("Received Uncaught Exception event in Thread: {}", t.getName(), e);
        }
    });
    return builder.build();
}

From source file:org.opendaylight.genius.interfacemanager.pmcounters.NodeConnectorStatsImpl.java

private ThreadFactory getThreadFactory(String threadNameFormat) {
    ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
    builder.setNameFormat(threadNameFormat);
    builder.setUncaughtExceptionHandler((thread, exception) -> LOG
            .error("Received Uncaught Exception event in Thread: {}", thread.getName(), exception));
    return builder.build();
}