Search in sources :

Example 96 with ThreadPoolTaskScheduler

use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.

the class OutboundGatewayFunctionTests method testContainerWithDestName.

@Test
public void testContainerWithDestName() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    gateway.setConnectionFactory(getConnectionFactory());
    gateway.setRequestDestination(requestQueue3);
    gateway.setReplyDestinationName("reply3");
    gateway.setCorrelationKey("JMSCorrelationID");
    gateway.setUseReplyContainer(true);
    gateway.afterPropertiesSet();
    gateway.start();
    final AtomicReference<Object> reply = new AtomicReference<Object>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        latch1.countDown();
        try {
            reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
        } finally {
            latch2.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(getConnectionFactory());
    template.setReceiveTimeout(10000);
    javax.jms.Message request = template.receive(requestQueue3);
    assertNotNull(request);
    final javax.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), session -> jmsReply);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    gateway.stop();
    scheduler.destroy();
    exec.shutdownNow();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 97 with ThreadPoolTaskScheduler

use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.

the class OutboundGatewayFunctionTests method testContainerWithTemporaryNoCorrelation.

@Test
public void testContainerWithTemporaryNoCorrelation() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    gateway.setConnectionFactory(getConnectionFactory());
    gateway.setRequestDestination(requestQueue6);
    gateway.setUseReplyContainer(true);
    gateway.afterPropertiesSet();
    gateway.start();
    final AtomicReference<Object> reply = new AtomicReference<>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        latch1.countDown();
        try {
            reply.set(gateway.handleRequestMessage(new GenericMessage<>("foo")));
        } finally {
            latch2.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(getConnectionFactory());
    template.setReceiveTimeout(10000);
    javax.jms.Message request = template.receive(requestQueue6);
    assertNotNull(request);
    final javax.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), session -> {
        jmsReply.setJMSCorrelationID(jmsReply.getJMSMessageID());
        return jmsReply;
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    gateway.stop();
    scheduler.destroy();
    exec.shutdownNow();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 98 with ThreadPoolTaskScheduler

use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.

the class OutboundGatewayFunctionTests method testContainerWithDestNameNoCorrelation.

@Test
public void testContainerWithDestNameNoCorrelation() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    gateway.setConnectionFactory(getConnectionFactory());
    gateway.setRequestDestination(requestQueue4);
    gateway.setReplyDestinationName("reply4");
    gateway.setUseReplyContainer(true);
    gateway.afterPropertiesSet();
    gateway.start();
    final AtomicReference<Object> reply = new AtomicReference<>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        latch1.countDown();
        try {
            reply.set(gateway.handleRequestMessage(new GenericMessage<>("foo")));
        } finally {
            latch2.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(getConnectionFactory());
    template.setReceiveTimeout(10000);
    javax.jms.Message request = template.receive(requestQueue4);
    assertNotNull(request);
    final javax.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), session -> {
        jmsReply.setJMSCorrelationID(jmsReply.getJMSMessageID());
        return jmsReply;
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    gateway.stop();
    scheduler.destroy();
    exec.shutdownNow();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 99 with ThreadPoolTaskScheduler

use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.

the class OutboundGatewayFunctionTests method testContainerWithTemporary.

@Test
public void testContainerWithTemporary() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    gateway.setConnectionFactory(getConnectionFactory());
    gateway.setRequestDestination(requestQueue5);
    gateway.setCorrelationKey("JMSCorrelationID");
    gateway.setUseReplyContainer(true);
    gateway.setComponentName("testContainerWithTemporary.gateway");
    gateway.afterPropertiesSet();
    gateway.start();
    final AtomicReference<Object> reply = new AtomicReference<>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        latch1.countDown();
        try {
            reply.set(gateway.handleRequestMessage(new GenericMessage<>("foo")));
        } finally {
            latch2.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(getConnectionFactory());
    template.setReceiveTimeout(10000);
    javax.jms.Message request = template.receive(requestQueue5);
    assertNotNull(request);
    final javax.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), session -> jmsReply);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    gateway.stop();
    scheduler.destroy();
    exec.shutdownNow();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 100 with ThreadPoolTaskScheduler

use of org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler in project spring-integration by spring-projects.

the class MqttAdapterTests method testReconnect.

@Test
public void testReconnect() throws Exception {
    final IMqttClient client = mock(IMqttClient.class);
    MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
    adapter.setRecoveryInterval(10);
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    given(logger.isDebugEnabled()).willReturn(true);
    final AtomicInteger attemptingReconnectCount = new AtomicInteger();
    willAnswer(i -> {
        if (attemptingReconnectCount.getAndIncrement() == 0) {
            adapter.connectionLost(new RuntimeException("while schedule running"));
        }
        i.callRealMethod();
        return null;
    }).given(logger).debug("Attempting reconnect");
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.start();
    adapter.connectionLost(new RuntimeException("initial"));
    Thread.sleep(1000);
    // the following assertion should be equalTo, but leq to protect against a slow CI server
    assertThat(attemptingReconnectCount.get(), lessThanOrEqualTo(2));
    adapter.stop();
    taskScheduler.destroy();
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) Log(org.apache.commons.logging.Log) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Aggregations

ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)137 Bean (org.springframework.context.annotation.Bean)43 Test (org.junit.Test)42 BeanFactory (org.springframework.beans.factory.BeanFactory)31 Test (org.junit.jupiter.api.Test)26 CountDownLatch (java.util.concurrent.CountDownLatch)19 QueueChannel (org.springframework.integration.channel.QueueChannel)19 GenericMessage (org.springframework.messaging.support.GenericMessage)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 ExecutorService (java.util.concurrent.ExecutorService)10 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 MqttPahoMessageDrivenChannelAdapter (org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)7 MqttPahoMessageHandler (org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 ArrayList (java.util.ArrayList)5 Log (org.apache.commons.logging.Log)5 MessageChannel (org.springframework.messaging.MessageChannel)5 File (java.io.File)4