Search in sources :

Example 21 with ThreadPoolTaskScheduler

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

the class BackToBackAdapterTests method testAddRemoveTopic.

@Test
public void testAddRemoveTopic() {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in");
    QueueChannel outputChannel = new QueueChannel();
    inbound.setOutputChannel(outputChannel);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    inbound.setTaskScheduler(taskScheduler);
    inbound.setBeanFactory(mock(BeanFactory.class));
    inbound.afterPropertiesSet();
    inbound.start();
    inbound.addTopic("mqtt-foo");
    adapter.handleMessage(new GenericMessage<String>("foo"));
    Message<?> out = outputChannel.receive(20_000);
    assertNotNull(out);
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    inbound.addTopic("mqtt-bar");
    adapter.handleMessage(MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build());
    out = outputChannel.receive(20_000);
    assertNotNull(out);
    assertEquals("bar", out.getPayload());
    assertEquals("mqtt-bar", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    inbound.removeTopic("mqtt-bar");
    adapter.handleMessage(MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build());
    out = outputChannel.receive(1);
    assertNull(out);
    try {
        inbound.addTopic("mqtt-foo");
        fail("Expected exception");
    } catch (MessagingException e) {
        assertEquals("Topic 'mqtt-foo' is already subscribed.", e.getMessage());
    }
    inbound.addTopic("mqqt-bar", "mqqt-baz");
    inbound.removeTopic("mqqt-bar", "mqqt-baz");
    inbound.addTopics(new String[] { "mqqt-bar", "mqqt-baz" }, new int[] { 0, 0 });
    inbound.removeTopic("mqqt-bar", "mqqt-baz");
    adapter.stop();
    inbound.stop();
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 22 with ThreadPoolTaskScheduler

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

the class BackToBackAdapterTests method testTwoTopics.

@Test
public void testTwoTopics() {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in", "mqtt-foo", "mqtt-bar");
    QueueChannel outputChannel = new QueueChannel();
    inbound.setOutputChannel(outputChannel);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    inbound.setTaskScheduler(taskScheduler);
    inbound.setBeanFactory(mock(BeanFactory.class));
    inbound.afterPropertiesSet();
    inbound.start();
    adapter.handleMessage(new GenericMessage<String>("foo"));
    Message<?> message = MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build();
    adapter.handleMessage(message);
    Message<?> out = outputChannel.receive(20000);
    assertNotNull(out);
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    out = outputChannel.receive(20000);
    assertNotNull(out);
    inbound.stop();
    assertEquals("bar", out.getPayload());
    assertEquals("mqtt-bar", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    adapter.stop();
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 23 with ThreadPoolTaskScheduler

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

the class BackToBackAdapterTests method testAsync.

@Test
public void testAsync() throws Exception {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.setAsync(true);
    adapter.setAsyncEvents(true);
    EventPublisher publisher = new EventPublisher();
    adapter.setApplicationEventPublisher(publisher);
    adapter.afterPropertiesSet();
    adapter.start();
    MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in", "mqtt-foo");
    QueueChannel outputChannel = new QueueChannel();
    inbound.setOutputChannel(outputChannel);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    inbound.setTaskScheduler(taskScheduler);
    inbound.setBeanFactory(mock(BeanFactory.class));
    inbound.afterPropertiesSet();
    inbound.start();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    adapter.handleMessage(message);
    verifyEvents(adapter, publisher, message);
    Message<?> out = outputChannel.receive(20000);
    assertNotNull(out);
    adapter.stop();
    inbound.stop();
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 24 with ThreadPoolTaskScheduler

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

the class FileTailingMessageProducerTests method testGuts.

private void testGuts(FileTailingMessageProducerSupport adapter, String field) throws Exception {
    this.adapter = adapter;
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.afterPropertiesSet();
    adapter.setTaskScheduler(taskScheduler);
    final List<FileTailingEvent> events = new ArrayList<FileTailingEvent>();
    adapter.setApplicationEventPublisher(event -> {
        FileTailingEvent tailEvent = (FileTailingEvent) event;
        logger.debug(event);
        events.add(tailEvent);
    });
    adapter.setFile(new File(testDir, "foo"));
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(outputChannel);
    adapter.setTailAttemptsDelay(500);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    File file = new File(testDir, "foo");
    File renamed = new File(testDir, "bar");
    file.delete();
    renamed.delete();
    adapter.start();
    waitForField(adapter, field);
    FileOutputStream foo = new FileOutputStream(file);
    for (int i = 0; i < 50; i++) {
        foo.write(("hello" + i + "\n").getBytes());
    }
    foo.flush();
    foo.close();
    for (int i = 0; i < 50; i++) {
        Message<?> message = outputChannel.receive(10000);
        assertNotNull("expected a non-null message", message);
        assertEquals("hello" + i, message.getPayload());
    }
    file.renameTo(renamed);
    file = new File(testDir, "foo");
    foo = new FileOutputStream(file);
    if (adapter instanceof ApacheCommonsFileTailingMessageProducer) {
        Thread.sleep(1000);
    }
    for (int i = 50; i < 100; i++) {
        foo.write(("hello" + i + "\n").getBytes());
    }
    foo.flush();
    foo.close();
    for (int i = 50; i < 100; i++) {
        Message<?> message = outputChannel.receive(10000);
        assertNotNull("expected a non-null message", message);
        assertEquals("hello" + i, message.getPayload());
        assertEquals(file, message.getHeaders().get(FileHeaders.ORIGINAL_FILE));
        assertEquals(file.getName(), message.getHeaders().get(FileHeaders.FILENAME));
    }
    assertThat(events.size(), greaterThanOrEqualTo(1));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileTailingEvent(org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)

Example 25 with ThreadPoolTaskScheduler

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

the class FileTailingMessageProducerTests method testIdleEvent.

@Test
public void testIdleEvent() throws Exception {
    ApacheCommonsFileTailingMessageProducer adapter = new ApacheCommonsFileTailingMessageProducer();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.afterPropertiesSet();
    adapter.setTaskScheduler(taskScheduler);
    CountDownLatch idleCountDownLatch = new CountDownLatch(1);
    CountDownLatch fileExistCountDownLatch = new CountDownLatch(1);
    adapter.setApplicationEventPublisher(event -> {
        if (event instanceof FileTailingIdleEvent) {
            idleCountDownLatch.countDown();
        }
        if (event instanceof FileTailingEvent) {
            FileTailingEvent fileTailingEvent = (FileTailingEvent) event;
            if (fileTailingEvent.getMessage().contains("File not found")) {
                fileExistCountDownLatch.countDown();
            }
        }
    });
    File file = spy(new File(this.testDir, "foo"));
    file.delete();
    adapter.setFile(file);
    adapter.setOutputChannel(new NullChannel());
    adapter.setIdleEventInterval(10);
    adapter.afterPropertiesSet();
    adapter.start();
    boolean noFile = fileExistCountDownLatch.await(10, TimeUnit.SECONDS);
    assertTrue("file does not exist event did not emit ", noFile);
    boolean noEvent = idleCountDownLatch.await(100, TimeUnit.MILLISECONDS);
    assertFalse("event should not emit when no file exit", noEvent);
    verify(file, atLeastOnce()).exists();
    file.createNewFile();
    boolean eventRaised = idleCountDownLatch.await(10, TimeUnit.SECONDS);
    assertTrue("idle event did not emit", eventRaised);
    adapter.stop();
    file.delete();
}
Also used : FileTailingIdleEvent(org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingIdleEvent) FileTailingEvent(org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) NullChannel(org.springframework.integration.channel.NullChannel) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Aggregations

ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)122 Test (org.junit.Test)41 Bean (org.springframework.context.annotation.Bean)32 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 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 ConnectionFactory (javax.jms.ConnectionFactory)4