Search in sources :

Example 61 with ThreadPoolTaskScheduler

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

the class FileTailingMessageProducerTests method canHandleFilenameHavingSpecialCharacters.

@Test
@TailAvailable
public void canHandleFilenameHavingSpecialCharacters() throws Exception {
    File file = File.createTempFile("foo bar", " -c 1");
    file.delete();
    OSDelegatingFileTailingMessageProducer adapter = new OSDelegatingFileTailingMessageProducer();
    adapter.setOptions(TAIL_OPTIONS_FOLLOW_NAME_ALL_LINES);
    adapter.setFile(file);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.afterPropertiesSet();
    adapter.setTaskScheduler(taskScheduler);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(outputChannel);
    adapter.setTailAttemptsDelay(500);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    waitForField(adapter, "stdOutReader");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(("hello foobar\n").getBytes());
    fos.close();
    Message<?> message = outputChannel.receive(10000);
    assertThat(message).as("expected a non-null message").isNotNull();
    assertThat(message.getPayload()).isEqualTo("hello foobar");
    adapter.stop();
    file.delete();
    taskScheduler.destroy();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileOutputStream(java.io.FileOutputStream) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 62 with ThreadPoolTaskScheduler

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

the class OutboundGatewayFunctionTests method testContainerWithDestNoCorrelation.

@Test
public void testContainerWithDestNoCorrelation() 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(connectionFactory);
    gateway.setRequestDestination(requestQueue2);
    gateway.setReplyDestination(replyQueue2);
    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();
        }
    });
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(connectionFactory);
    template.setReceiveTimeout(10000);
    jakarta.jms.Message request = template.receive(requestQueue2);
    assertThat(request).isNotNull();
    final jakarta.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), session -> {
        jmsReply.setJMSCorrelationID(jmsReply.getJMSMessageID());
        return jmsReply;
    });
    assertThat(latch2.await(20, TimeUnit.SECONDS)).isTrue();
    assertThat(reply.get()).isNotNull();
    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.jupiter.api.Test)

Example 63 with ThreadPoolTaskScheduler

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

the class OutboundGatewayFunctionTests method testContainerWithDest.

@Test
public void testContainerWithDest() 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(connectionFactory);
    gateway.setRequestDestination(requestQueue1);
    gateway.setReplyDestination(replyQueue1);
    gateway.setCorrelationKey("JMSCorrelationID");
    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();
        }
    });
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(connectionFactory);
    template.setReceiveTimeout(10000);
    jakarta.jms.Message request = template.receive(requestQueue1);
    assertThat(request).isNotNull();
    final jakarta.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), session -> jmsReply);
    assertThat(latch2.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(reply.get()).isNotNull();
    gateway.stop();
    scheduler.destroy();
    exec.shutdown();
}
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.jupiter.api.Test)

Example 64 with ThreadPoolTaskScheduler

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

the class ByteStreamWritingMessageHandlerTests method initialize.

@Before
public void initialize() {
    stream = new ByteArrayOutputStream();
    handler = new ByteStreamWritingMessageHandler(stream);
    this.channel = new QueueChannel(10);
    this.endpoint = new PollingConsumer(channel, handler);
    scheduler = new ThreadPoolTaskScheduler();
    this.endpoint.setTaskScheduler(scheduler);
    scheduler.afterPropertiesSet();
    trigger.reset();
    endpoint.setTrigger(trigger);
    endpoint.setBeanFactory(mock(BeanFactory.class));
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Before(org.junit.Before)

Example 65 with ThreadPoolTaskScheduler

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

the class CharacterStreamWritingMessageHandlerTests method initialize.

@Before
public void initialize() {
    writer = new StringWriter();
    handler = new CharacterStreamWritingMessageHandler(writer);
    this.channel = new QueueChannel(10);
    trigger.reset();
    this.endpoint = new PollingConsumer(channel, handler);
    scheduler = new ThreadPoolTaskScheduler();
    this.endpoint.setTaskScheduler(scheduler);
    scheduler.afterPropertiesSet();
    trigger.reset();
    endpoint.setTrigger(trigger);
    endpoint.setBeanFactory(mock(BeanFactory.class));
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) StringWriter(java.io.StringWriter) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Before(org.junit.Before)

Aggregations

ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)146 Test (org.junit.jupiter.api.Test)53 Bean (org.springframework.context.annotation.Bean)46 BeanFactory (org.springframework.beans.factory.BeanFactory)33 CountDownLatch (java.util.concurrent.CountDownLatch)23 QueueChannel (org.springframework.integration.channel.QueueChannel)23 Test (org.junit.Test)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 GenericMessage (org.springframework.messaging.support.GenericMessage)14 ExecutorService (java.util.concurrent.ExecutorService)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)11 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)9 Message (org.springframework.messaging.Message)9 MessageChannel (org.springframework.messaging.MessageChannel)9 ArrayList (java.util.ArrayList)8 MessagingException (org.springframework.messaging.MessagingException)8 LogAccessor (org.springframework.core.log.LogAccessor)7 IOException (java.io.IOException)6 Socket (java.net.Socket)6