public FanoutTransport() { // Setup a task that is used to reconnect the a connection async. reconnectTaskFactory = new TaskRunnerFactory(); reconnectTaskFactory.init(); reconnectTask = reconnectTaskFactory.createTaskRunner(new Task() { @Override public boolean iterate() { return doConnect(); } }, "ActiveMQ Fanout Worker: " + System.identityHashCode(this)); }
protected void doInit() throws Exception { taskRunnerFactory.execute(new Runnable() { @Override public void run() { //Need to start in new thread to let startup finish first //We can trigger a read because we know the channel is ready since the SSL handshake //already happened serviceRead(); initialized.countDown(); } }); }
/** * Useful to cleanup when it is known that all brokers and connections are * close and stopped, eg: when un deploying from web container. */ public static void shutdown() { DEFAULT_TASK_RUNNER_FACTORY.shutdown(); }
public void execute(Runnable runnable, String name) { init(); LOG.trace("Execute[{}] runnable: {}", name, runnable); ExecutorService executor = executorRef.get(); if (executor != null) { executor.execute(runnable); } else { doExecuteNewThread(runnable, name); } }
public TaskRunnerFactory getSessionTaskRunner() { synchronized (this) { if (sessionTaskRunner == null) { sessionTaskRunner = new TaskRunnerFactory("ActiveMQ Session Task", ThreadPriorities.INBOUND_CLIENT_SESSION, false, 1000, isUseDedicatedTaskRunner(), maxThreadPoolSize); sessionTaskRunner.setRejectedTaskHandler(rejectedTaskHandler); } } return sessionTaskRunner; }
public CacheEvictionUsageListener(Usage usage, int usageHighMark, int usageLowMark, TaskRunnerFactory taskRunnerFactory) { this.usage = usage; this.usageHighMark = usageHighMark; this.usageLowMark = usageLowMark; evictionTask = taskRunnerFactory.createTaskRunner(new Task() { public boolean iterate() { return evictMessages(); } }, "Cache Evictor: " + System.identityHashCode(this)); }
final TaskRunnerFactory taskRunnerFactory = new TaskRunnerFactory(); taskRunnerFactory.execute(new Runnable() { @Override public void run() { Thread.currentThread().interrupt(); } finally { taskRunnerFactory.shutdownNow();
protected ExecutorService createDefaultExecutor() { ThreadPoolExecutor rc = new ThreadPoolExecutor(getDefaultCorePoolSize(), getMaxThreadPoolSize(), getDefaultKeepAliveTime(), TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() { @Override public Thread newThread(Runnable runnable) { String threadName = name + "-" + id.incrementAndGet(); Thread thread = new Thread(runnable, threadName); thread.setDaemon(daemon); thread.setPriority(priority); if (threadClassLoader != null) { thread.setContextClassLoader(threadClassLoader); } thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread t, final Throwable e) { LOG.error("Error in thread '{}'", t.getName(), e); } }); LOG.trace("Created thread[{}]: {}", threadName, thread); return thread; } }); if (rejectedTaskHandler != null) { rc.setRejectedExecutionHandler(rejectedTaskHandler); } else { rc.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); } return rc; }
@Override public void start() throws Exception { taskRunner = new TaskRunnerFactory(); taskRunner.init(); running.set(true); for (int i = 0; i < services.length; i++) { listener.onServiceAdd(new SimpleDiscoveryEvent(services[i])); } }
@Override protected void doStop(ServiceStopper stopper) throws Exception { if (taskRunnerFactory != null) { taskRunnerFactory.shutdownNow(); taskRunnerFactory = null; } }
public TaskRunnerFactory getTaskRunnerFactory() { if (taskRunnerFactory == null) { taskRunnerFactory = new TaskRunnerFactory("Persistence Adaptor Task", journalThreadPriority, true, 1000, isUseDedicatedTaskRunner()); } return taskRunnerFactory; }
public TaskRunnerFactory getTaskRunnerFactory() { if (this.taskRunnerFactory == null) { this.taskRunnerFactory = new TaskRunnerFactory("ActiveMQ BrokerService["+getBrokerName()+"] Task", getTaskRunnerPriority(), true, 1000, isDedicatedTaskRunner()); this.taskRunnerFactory.setThreadClassLoader(this.getClass().getClassLoader()); } return this.taskRunnerFactory; }
/** * Performs a shutdown only, by which the thread pool is shutdown by not graceful nor aggressively. * * @see ThreadPoolUtils#shutdown(java.util.concurrent.ExecutorService) */ public void shutdown() { ExecutorService executor = executorRef.get(); if (executor != null) { ThreadPoolUtils.shutdown(executor); } clearExecutor(); }
public void init() { if (!initDone.get()) { // If your OS/JVM combination has a good thread model, you may want to // avoid using a thread pool to run tasks and use a DedicatedTaskRunner instead. //AMQ-6602 - lock instead of using compareAndSet to prevent threads from seeing a null value //for executorRef inside createTaskRunner() on contention and creating a DedicatedTaskRunner synchronized(this) { //need to recheck if initDone is true under the lock if (!initDone.get()) { if (dedicatedTaskRunner || "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.UseDedicatedTaskRunner"))) { executorRef.set(null); } else { executorRef.compareAndSet(null, createDefaultExecutor()); } LOG.debug("Initialized TaskRunnerFactory[{}] using ExecutorService: {}", name, executorRef.get()); initDone.set(true); } } } }
public TaskRunner createTaskRunner(Task task, String name) { init(); ExecutorService executor = executorRef.get(); if (executor != null) { return new PooledTaskRunner(executor, task, maxIterationsPerRun); } else { return new DedicatedTaskRunner(task, name, priority, daemon); } }
public Topic(BrokerService brokerService, ActiveMQDestination destination, TopicMessageStore store, DestinationStatistics parentStats, TaskRunnerFactory taskFactory) throws Exception { super(brokerService, store, destination, parentStats); this.topicStore = store; subscriptionRecoveryPolicy = new RetainedMessageSubscriptionRecoveryPolicy(null); this.taskRunner = taskFactory.createTaskRunner(this, "Topic " + destination.getPhysicalName()); this.taskRunnerFactor = taskFactory; }
protected ExecutorService createDefaultExecutor() { ThreadPoolExecutor rc = new ThreadPoolExecutor(getDefaultCorePoolSize(), getMaxThreadPoolSize(), getDefaultKeepAliveTime(), TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() { @Override public Thread newThread(Runnable runnable) { String threadName = name + "-" + id.incrementAndGet(); Thread thread = new Thread(runnable, threadName); thread.setDaemon(daemon); thread.setPriority(priority); if (threadClassLoader != null) { thread.setContextClassLoader(threadClassLoader); } thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread t, final Throwable e) { LOG.error("Error in thread '{}'", t.getName(), e); } }); LOG.trace("Created thread[{}]: {}", threadName, thread); return thread; } }); if (rejectedTaskHandler != null) { rc.setRejectedExecutionHandler(rejectedTaskHandler); } else { rc.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); } return rc; }
@Override public void start() throws Exception { taskRunner = new TaskRunnerFactory(); taskRunner.init(); running.set(true); for (int i = 0; i < services.length; i++) { listener.onServiceAdd(new SimpleDiscoveryEvent(services[i])); } }
@Override protected void doStop(ServiceStopper stopper) throws Exception { initialized.countDown(); if (taskRunnerFactory != null) { taskRunnerFactory.shutdownNow(); taskRunnerFactory = null; } if (channel != null) { channel.close(); channel = null; } super.doStop(stopper); }
public TaskRunnerFactory getPersistenceTaskRunnerFactory() { if (taskRunnerFactory == null) { persistenceTaskRunnerFactory = new TaskRunnerFactory("Persistence Adaptor Task", persistenceThreadPriority, true, 1000, isDedicatedTaskRunner()); } return persistenceTaskRunnerFactory; }