Search in sources :

Example 81 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project geode by apache.

the class WanCommands method startGatewaySender.

@CliCommand(value = CliStrings.START_GATEWAYSENDER, help = CliStrings.START_GATEWAYSENDER__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_WAN)
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result startGatewaySender(@CliOption(key = CliStrings.START_GATEWAYSENDER__ID, mandatory = true, optionContext = ConverterHint.GATEWAY_SENDER_ID, help = CliStrings.START_GATEWAYSENDER__ID__HELP) String senderId, @CliOption(key = CliStrings.START_GATEWAYSENDER__GROUP, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.START_GATEWAYSENDER__GROUP__HELP) String[] onGroup, @CliOption(key = CliStrings.START_GATEWAYSENDER__MEMBER, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.START_GATEWAYSENDER__MEMBER__HELP) String[] onMember) {
    Result result = null;
    final String id = senderId.trim();
    try {
        final InternalCache cache = getCache();
        final SystemManagementService service = (SystemManagementService) ManagementService.getExistingManagementService(cache);
        TabularResultData resultData = ResultBuilder.createTabularResultData();
        Set<DistributedMember> dsMembers = CliUtil.findMembers(onGroup, onMember);
        if (dsMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        ExecutorService execService = Executors.newCachedThreadPool(new ThreadFactory() {

            AtomicInteger threadNum = new AtomicInteger();

            public Thread newThread(final Runnable r) {
                Thread result = new Thread(r, "Start Sender Command Thread " + threadNum.incrementAndGet());
                result.setDaemon(true);
                return result;
            }
        });
        List<Callable<List>> callables = new ArrayList<Callable<List>>();
        for (final DistributedMember member : dsMembers) {
            callables.add(new Callable<List>() {

                public List call() throws Exception {
                    GatewaySenderMXBean bean = null;
                    ArrayList<String> statusList = new ArrayList<String>();
                    if (cache.getDistributedSystem().getDistributedMember().getId().equals(member.getId())) {
                        bean = service.getLocalGatewaySenderMXBean(id);
                    } else {
                        ObjectName objectName = service.getGatewaySenderMBeanName(member, id);
                        bean = service.getMBeanProxy(objectName, GatewaySenderMXBean.class);
                    }
                    if (bean != null) {
                        if (bean.isRunning()) {
                            statusList.add(member.getId());
                            statusList.add(CliStrings.GATEWAY_ERROR);
                            statusList.add(CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_ALREADY_STARTED_ON_MEMBER_1, new Object[] { id, member.getId() }));
                        } else {
                            bean.start();
                            statusList.add(member.getId());
                            statusList.add(CliStrings.GATEWAY_OK);
                            statusList.add(CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_STARTED_ON_MEMBER_1, new Object[] { id, member.getId() }));
                        }
                    } else {
                        statusList.add(member.getId());
                        statusList.add(CliStrings.GATEWAY_ERROR);
                        statusList.add(CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_NOT_AVAILABLE_ON_MEMBER_1, new Object[] { id, member.getId() }));
                    }
                    return statusList;
                }
            });
        }
        Iterator<DistributedMember> memberIterator = dsMembers.iterator();
        List<Future<List>> futures = null;
        try {
            futures = execService.invokeAll(callables);
        } catch (InterruptedException ite) {
            accumulateStartResult(resultData, null, CliStrings.GATEWAY_ERROR, CliStrings.format(CliStrings.GATEWAY_SENDER_0_COULD_NOT_BE_INVOKED_DUE_TO_1, new Object[] { id, ite.getMessage() }));
        }
        for (Future<List> future : futures) {
            DistributedMember member = memberIterator.next();
            List<String> memberStatus = null;
            try {
                memberStatus = future.get();
                accumulateStartResult(resultData, memberStatus.get(0), memberStatus.get(1), memberStatus.get(2));
            } catch (InterruptedException ite) {
                accumulateStartResult(resultData, member.getId(), CliStrings.GATEWAY_ERROR, CliStrings.format(CliStrings.GATEWAY_SENDER_0_COULD_NOT_BE_STARTED_ON_MEMBER_DUE_TO_1, new Object[] { id, ite.getMessage() }));
                continue;
            } catch (ExecutionException ee) {
                accumulateStartResult(resultData, member.getId(), CliStrings.GATEWAY_ERROR, CliStrings.format(CliStrings.GATEWAY_SENDER_0_COULD_NOT_BE_STARTED_ON_MEMBER_DUE_TO_1, new Object[] { id, ee.getMessage() }));
                continue;
            }
        }
        execService.shutdown();
        result = ResultBuilder.buildResult(resultData);
    } catch (Exception e) {
        LogWrapper.getInstance().warning(CliStrings.GATEWAY_ERROR + CliUtil.stackTraceAsString(e));
        result = ResultBuilder.createGemFireErrorResult(CliStrings.GATEWAY_ERROR + e.getMessage());
    }
    return result;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) Callable(java.util.concurrent.Callable) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) GatewaySenderMXBean(org.apache.geode.management.GatewaySenderMXBean) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ExecutionException(java.util.concurrent.ExecutionException) ObjectName(javax.management.ObjectName) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DistributedMember(org.apache.geode.distributed.DistributedMember) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 82 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project geode by apache.

the class GemfireCacheHelper method CreateThreadFactory.

public static ThreadFactory CreateThreadFactory(final ThreadGroup tg, final String threadName) {
    final ThreadFactory threadFactory = new ThreadFactory() {

        public Thread newThread(Runnable command) {
            Thread thread = new Thread(tg, command, threadName);
            thread.setDaemon(true);
            return thread;
        }
    };
    return threadFactory;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory)

Example 83 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project geode by apache.

the class GemFireCacheImpl method getShutdownAllExecutorService.

private ExecutorService getShutdownAllExecutorService(int size) {
    final ThreadGroup threadGroup = LoggingThreadGroup.createThreadGroup("ShutdownAllGroup", logger);
    ThreadFactory threadFactory = new ThreadFactory() {

        private final AtomicInteger threadCount = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = new Thread(threadGroup, runnable, "ShutdownAll-" + this.threadCount.getAndIncrement());
            thread.setDaemon(true);
            return thread;
        }
    };
    return Executors.newFixedThreadPool(shutdownAllPoolSize == -1 ? size : shutdownAllPoolSize, threadFactory);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup)

Example 84 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project wildfly by wildfly.

the class UndertowEventHandlerAdapter method start.

@Override
public void start(StartContext context) {
    UndertowService service = this.service.getValue();
    ContainerEventHandler eventHandler = this.eventHandler.getValue();
    this.connector = new UndertowConnector(this.listener.getValue());
    this.server = new UndertowServer(service, connector);
    // Register ourselves as a listener to the container events
    service.registerListener(this);
    // Initialize mod_cluster and start it now
    eventHandler.init(this.server);
    eventHandler.start(this.server);
    // Start the periodic STATUS thread
    ThreadGroup group = new ThreadGroup(UndertowEventHandlerAdapter.class.getSimpleName());
    ThreadFactory factory = doPrivileged(new PrivilegedAction<ThreadFactory>() {

        public ThreadFactory run() {
            return new JBossThreadFactory(group, Boolean.FALSE, null, "%G - %t", null, null);
        }
    });
    this.executor = Executors.newScheduledThreadPool(1, factory);
    this.executor.scheduleWithFixedDelay(this, 0, statusInterval, TimeUnit.SECONDS);
    suspendController.getValue().registerActivity(this);
}
Also used : ContainerEventHandler(org.jboss.modcluster.container.ContainerEventHandler) ThreadFactory(java.util.concurrent.ThreadFactory) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) UndertowService(org.wildfly.extension.undertow.UndertowService)

Example 85 with ThreadFactory

use of java.util.concurrent.ThreadFactory in project deltaspike by apache.

the class ConcurrencyBugTest method testWithConcurrency.

@Test
public void testWithConcurrency() throws Exception {
    ThreadFactory threadFactory = Executors.defaultThreadFactory();
    ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, SECONDS, new SynchronousQueue<Runnable>(), threadFactory, new BlockPolicy());
    int iterations = 100;
    List<Future<String>> results = new ArrayList<Future<String>>(iterations);
    for (int i = 0; i < iterations; i++) {
        results.add(executor.submit(new BeanCaller(bean)));
    }
    executor.shutdown();
    executor.awaitTermination(60, SECONDS);
    for (int i = 0; i < iterations; i++) {
        results.get(i).get();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)250 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)47 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)46 ExecutorService (java.util.concurrent.ExecutorService)45 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)21 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)19 Test (org.junit.Test)17 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)16 Future (java.util.concurrent.Future)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 ArrayList (java.util.ArrayList)13 LoggingThreadGroup (org.apache.geode.internal.logging.LoggingThreadGroup)12 IOException (java.io.IOException)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 ExecutionException (java.util.concurrent.ExecutionException)9 Executor (java.util.concurrent.Executor)9 ChannelFuture (io.netty.channel.ChannelFuture)8 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)8 SynchronousQueue (java.util.concurrent.SynchronousQueue)7