Search in sources :

Example 1 with TaskExecutor

use of org.springframework.core.task.TaskExecutor in project camel by apache.

the class DefaultJmsMessageListenerContainer method createDefaultTaskExecutor.

/**
     * Create a default TaskExecutor. Called if no explicit TaskExecutor has been specified.
     * <p />
     * The type of {@link TaskExecutor} will depend on the value of
     * {@link JmsConfiguration#getDefaultTaskExecutorType()}. For more details, refer to the Javadoc of
     * {@link DefaultTaskExecutorType}.
     * <p />
     * In all cases, it uses the specified bean name and Camel's {@link org.apache.camel.spi.ExecutorServiceManager}
     * to resolve the thread name.
     * @see JmsConfiguration#setDefaultTaskExecutorType(DefaultTaskExecutorType)
     * @see ThreadPoolTaskExecutor#setBeanName(String)
     */
@Override
protected TaskExecutor createDefaultTaskExecutor() {
    String pattern = endpoint.getCamelContext().getExecutorServiceManager().getThreadNamePattern();
    String beanName = getBeanName() == null ? endpoint.getThreadName() : getBeanName();
    TaskExecutor answer;
    if (endpoint.getDefaultTaskExecutorType() == DefaultTaskExecutorType.ThreadPool) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setBeanName(beanName);
        executor.setThreadFactory(new CamelThreadFactory(pattern, beanName, true));
        executor.setCorePoolSize(endpoint.getConcurrentConsumers());
        // Direct hand-off mode. Do not queue up tasks: assign it to a thread immediately.
        // We set no upper-bound on the thread pool (no maxPoolSize) as it's already implicitly constrained by
        // maxConcurrentConsumers on the DMLC itself (i.e. DMLC will only grow up to a level of concurrency as
        // defined by maxConcurrentConsumers).
        executor.setQueueCapacity(0);
        executor.initialize();
        answer = executor;
    } else {
        SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(beanName);
        executor.setThreadFactory(new CamelThreadFactory(pattern, beanName, true));
        answer = executor;
    }
    taskExecutor = answer;
    return answer;
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) TaskExecutor(org.springframework.core.task.TaskExecutor) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 2 with TaskExecutor

use of org.springframework.core.task.TaskExecutor in project iaf by ibissource.

the class ShadowSender method doSendMessage.

/**
 * We override this from the parallel sender as we should only execute the original and shadowsenders here!
 */
@Override
public String doSendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    Guard guard = new Guard();
    Map<ISender, ParallelSenderExecutor> executorMap = new HashMap<ISender, ParallelSenderExecutor>();
    TaskExecutor executor = createTaskExecutor();
    for (Iterator<ISender> it = getExecutableSenders(); it.hasNext(); ) {
        ISender sender = it.next();
        // Create a new ParameterResolutionContext to be thread safe, see
        // documentation on constructor of ParameterResolutionContext
        // (parameter cacheXmlSource).
        // Testing also showed that disabling caching is better for
        // performance. At least when testing with a lot of large messages
        // in parallel. This might be due to the fact that objects can be
        // garbage collected earlier. OutOfMemoryErrors occur much
        // faster when caching is enabled. Testing was done by sending 10
        // messages of 1 MB concurrently to a pipeline which will process
        // the message in parallel with 10 SenderWrappers (containing a
        // XsltSender and IbisLocalSender).
        ParameterResolutionContext newPrc = new ParameterResolutionContext(prc.getInput(), prc.getSession(), prc.isNamespaceAware(), prc.isXslt2(), false);
        guard.addResource();
        ParallelSenderExecutor pse = new ParallelSenderExecutor(sender, correlationID, message, newPrc, guard, getStatisticsKeeper(sender));
        executorMap.put(sender, pse);
        executor.execute(pse);
    }
    try {
        guard.waitForAllResources();
    } catch (InterruptedException e) {
        throw new SenderException(getLogPrefix() + "was interupted", e);
    }
    ParallelSenderExecutor originalSender = null;
    XmlBuilder resultsXml = new XmlBuilder("results");
    resultsXml.addAttribute("correlationID", correlationID);
    resultsXml.addAttribute("adapter", getPipe().getAdapter().getName());
    XmlBuilder originalMessageXml = new XmlBuilder("originalMessage");
    originalMessageXml.setValue(XmlUtils.skipXmlDeclaration(message), false);
    resultsXml.addSubElement(originalMessageXml);
    // First loop through all (Shadow)Senders and handle their results
    for (Iterator<ISender> it = getExecutableSenders(); it.hasNext(); ) {
        ISender sender = it.next();
        ParallelSenderExecutor pse = (ParallelSenderExecutor) executorMap.get(sender);
        XmlBuilder resultXml;
        if (sender.getName() != null && sender.getName().equalsIgnoreCase(getOriginalSender())) {
            originalSender = pse;
            resultXml = new XmlBuilder("originalResult");
        } else {
            resultXml = new XmlBuilder("shadowResult");
        }
        StatisticsKeeper sk = getStatisticsKeeper(sender);
        resultXml.addAttribute("duration", sk.getLast() + sk.getUnits());
        resultXml.addAttribute("count", sk.getCount());
        resultXml.addAttribute("senderClass", ClassUtils.nameOf(sender));
        resultXml.addAttribute("senderName", sender.getName());
        Throwable throwable = pse.getThrowable();
        if (throwable == null) {
            Object result = pse.getReply();
            if (result == null) {
                resultXml.addAttribute("type", "null");
            } else {
                resultXml.addAttribute("type", ClassUtils.nameOf(result));
                resultXml.setValue(XmlUtils.skipXmlDeclaration(result.toString()), false);
            }
        } else {
            resultXml.addAttribute("type", ClassUtils.nameOf(throwable));
            resultXml.setValue(throwable.getMessage());
        }
        resultsXml.addSubElement(resultXml);
    }
    // cause an SenderException regardless of the results of the ShadowSenders.
    if (originalSender == null) {
        // In theory this should never happen!
        throw new SenderException("no originalSender found");
    }
    // The messages have been processed, now the results need to be stored somewhere.
    try {
        if (resultISender instanceof ISenderWithParameters) {
            ParameterResolutionContext newPrc = new ParameterResolutionContext(resultsXml.toXML(), prc.getSession());
            ((ISenderWithParameters) resultISender).sendMessage(correlationID, resultsXml.toXML(), newPrc);
        } else {
            resultISender.sendMessage(correlationID, resultsXml.toXML());
        }
    } catch (SenderException se) {
        log.warn("failed to send ShadowSender result to [" + resultISender.getName() + "]");
    }
    if (originalSender.getThrowable() != null) {
        throw new SenderException(originalSender.getThrowable());
    } else {
        return originalSender.getReply().toString();
    }
}
Also used : HashMap(java.util.HashMap) ISenderWithParameters(nl.nn.adapterframework.core.ISenderWithParameters) TaskExecutor(org.springframework.core.task.TaskExecutor) ISender(nl.nn.adapterframework.core.ISender) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) StatisticsKeeper(nl.nn.adapterframework.statistics.StatisticsKeeper) Guard(nl.nn.adapterframework.util.Guard) SenderException(nl.nn.adapterframework.core.SenderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Example 3 with TaskExecutor

use of org.springframework.core.task.TaskExecutor in project iaf by ibissource.

the class ParallelSenders method doSendMessage.

public String doSendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    Guard guard = new Guard();
    Map<ISender, ParallelSenderExecutor> executorMap = new HashMap<ISender, ParallelSenderExecutor>();
    TaskExecutor executor = createTaskExecutor();
    for (Iterator<ISender> it = getSenderIterator(); it.hasNext(); ) {
        ISender sender = it.next();
        // Create a new ParameterResolutionContext to be thread safe, see
        // documentation on constructor of ParameterResolutionContext
        // (parameter cacheXmlSource).
        // Testing also showed that disabling caching is better for
        // performance. At least when testing with a lot of large messages
        // in parallel. This might be due to the fact that objects can be
        // garbage collected earlier. OutOfMemoryErrors occur much
        // faster when caching is enabled. Testing was done by sending 10
        // messages of 1 MB concurrently to a pipeline which will process
        // the message in parallel with 10 SenderWrappers (containing a
        // XsltSender and IbisLocalSender).
        ParameterResolutionContext newPrc = new ParameterResolutionContext(prc.getInput(), prc.getSession(), prc.isNamespaceAware(), prc.isXslt2(), false);
        guard.addResource();
        ParallelSenderExecutor pse = new ParallelSenderExecutor(sender, correlationID, message, newPrc, guard, getStatisticsKeeper(sender));
        executorMap.put(sender, pse);
        executor.execute(pse);
    }
    try {
        guard.waitForAllResources();
    } catch (InterruptedException e) {
        throw new SenderException(getLogPrefix() + "was interupted", e);
    }
    XmlBuilder resultsXml = new XmlBuilder("results");
    for (Iterator<ISender> it = getSenderIterator(); it.hasNext(); ) {
        ISender sender = it.next();
        ParallelSenderExecutor pse = (ParallelSenderExecutor) executorMap.get(sender);
        XmlBuilder resultXml = new XmlBuilder("result");
        resultXml.addAttribute("senderClass", ClassUtils.nameOf(sender));
        resultXml.addAttribute("senderName", sender.getName());
        Throwable throwable = pse.getThrowable();
        if (throwable == null) {
            Object result = pse.getReply();
            if (result == null) {
                resultXml.addAttribute("type", "null");
            } else {
                resultXml.addAttribute("type", ClassUtils.nameOf(result));
                resultXml.setValue(XmlUtils.skipXmlDeclaration(result.toString()), false);
            }
        } else {
            resultXml.addAttribute("type", ClassUtils.nameOf(throwable));
            resultXml.setValue(throwable.getMessage());
        }
        resultsXml.addSubElement(resultXml);
    }
    return resultsXml.toXML();
}
Also used : HashMap(java.util.HashMap) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) TaskExecutor(org.springframework.core.task.TaskExecutor) ISender(nl.nn.adapterframework.core.ISender) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) Guard(nl.nn.adapterframework.util.Guard) SenderException(nl.nn.adapterframework.core.SenderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Example 4 with TaskExecutor

use of org.springframework.core.task.TaskExecutor in project spring-integration-samples by spring-projects.

the class TcpClientServerDemoTest method testMultiPlex.

@Test
public void testMultiPlex() throws Exception {
    TaskExecutor executor = new SimpleAsyncTaskExecutor();
    final CountDownLatch latch = new CountDownLatch(100);
    final BlockingQueue<Integer> results = new LinkedBlockingQueue<>();
    for (int i = 100; i < 200; i++) {
        results.add(i);
        final int j = i;
        executor.execute(() -> {
            // first 3 bytes is correlationid
            String result = gw.send(j + "Hello world!");
            assertEquals(j + "Hello world!:echo", result);
            results.remove(j);
            latch.countDown();
        });
    }
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    assertEquals(0, results.size());
}
Also used : SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) TaskExecutor(org.springframework.core.task.TaskExecutor) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 5 with TaskExecutor

use of org.springframework.core.task.TaskExecutor in project spring-boot by spring-projects.

the class TaskSchedulingAutoConfigurationTests method enableSchedulingWithNoTaskExecutorAutoConfiguresOne.

@Test
void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
    this.contextRunner.withPropertyValues("spring.task.scheduling.shutdown.await-termination=true", "spring.task.scheduling.shutdown.await-termination-period=30s", "spring.task.scheduling.thread-name-prefix=scheduling-test-").withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(TaskExecutor.class);
        TaskExecutor taskExecutor = context.getBean(TaskExecutor.class);
        TestBean bean = context.getBean(TestBean.class);
        assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
        assertThat(taskExecutor).hasFieldOrPropertyWithValue("waitForTasksToCompleteOnShutdown", true);
        assertThat(taskExecutor).hasFieldOrPropertyWithValue("awaitTerminationMillis", 30000L);
        assertThat(bean.threadNames).allMatch((name) -> name.contains("scheduling-test-"));
    });
}
Also used : TaskExecutor(org.springframework.core.task.TaskExecutor) Test(org.junit.jupiter.api.Test)

Aggregations

TaskExecutor (org.springframework.core.task.TaskExecutor)14 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)5 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)5 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)4 HashMap (java.util.HashMap)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ISender (nl.nn.adapterframework.core.ISender)2 SenderException (nl.nn.adapterframework.core.SenderException)2 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)2 Guard (nl.nn.adapterframework.util.Guard)2 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)2 AsyncExecutor (org.activiti.engine.impl.asyncexecutor.AsyncExecutor)2 SpringAsyncExecutor (org.activiti.spring.SpringAsyncExecutor)2 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)2 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)2 MessagingException (org.springframework.messaging.MessagingException)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ISenderWithParameters (nl.nn.adapterframework.core.ISenderWithParameters)1 StatisticsKeeper (nl.nn.adapterframework.statistics.StatisticsKeeper)1