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;
}
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;
}
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);
}
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);
}
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();
}
}
Aggregations