Search in sources :

Example 1 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration-samples by spring-projects.

the class Application method runDemo.

private void runDemo(ConfigurableApplicationContext context) {
    MessageChannel toKafka = context.getBean("toKafka", MessageChannel.class);
    System.out.println("Sending 10 messages...");
    Map<String, Object> headers = Collections.singletonMap(KafkaHeaders.TOPIC, this.properties.getTopic());
    for (int i = 0; i < 10; i++) {
        toKafka.send(new GenericMessage<>("foo" + i, headers));
    }
    System.out.println("Sending a null message...");
    toKafka.send(new GenericMessage<>(KafkaNull.INSTANCE, headers));
    PollableChannel fromKafka = context.getBean("fromKafka", PollableChannel.class);
    Message<?> received = fromKafka.receive(10000);
    int count = 0;
    while (received != null) {
        System.out.println(received);
        received = fromKafka.receive(++count < 11 ? 10000 : 1000);
    }
    System.out.println("Adding an adapter for a second topic and sending 10 messages...");
    addAnotherListenerForTopics(this.properties.getNewTopic());
    headers = Collections.singletonMap(KafkaHeaders.TOPIC, this.properties.getNewTopic());
    for (int i = 0; i < 10; i++) {
        toKafka.send(new GenericMessage<>("bar" + i, headers));
    }
    received = fromKafka.receive(10000);
    count = 0;
    while (received != null) {
        System.out.println(received);
        received = fromKafka.receive(++count < 10 ? 10000 : 1000);
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel)

Example 2 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration-samples by spring-projects.

the class SftpInboundReceiveSample method runDemo.

@Test
public void runDemo() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/SftpInboundReceiveSample-context.xml", this.getClass());
    RemoteFileTemplate<LsEntry> template = null;
    String file1 = "a.txt";
    String file2 = "b.txt";
    String file3 = "c.bar";
    new File("local-dir", file1).delete();
    new File("local-dir", file2).delete();
    try {
        PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class);
        @SuppressWarnings("unchecked") SessionFactory<LsEntry> sessionFactory = context.getBean(CachingSessionFactory.class);
        template = new RemoteFileTemplate<LsEntry>(sessionFactory);
        SftpTestUtils.createTestFiles(template, file1, file2, file3);
        SourcePollingChannelAdapter adapter = context.getBean(SourcePollingChannelAdapter.class);
        adapter.start();
        Message<?> received = localFileChannel.receive();
        assertNotNull("Expected file", received);
        System.out.println("Received first file message: " + received);
        received = localFileChannel.receive();
        assertNotNull("Expected file", received);
        System.out.println("Received second file message: " + received);
        received = localFileChannel.receive(1000);
        assertNull("Expected null", received);
        System.out.println("No third file was received as expected");
    } finally {
        SftpTestUtils.cleanUp(template, file1, file2, file3);
        context.close();
        assertTrue("Could note delete retrieved file", new File("local-dir", file1).delete());
        assertTrue("Could note delete retrieved file", new File("local-dir", file2).delete());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) File(java.io.File) Test(org.junit.Test)

Example 3 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration-samples by spring-projects.

the class FeedInboundChannelAdapterSample method runDemo.

@SuppressWarnings("unchecked")
@Test
public void runDemo() {
    ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/spring/integration/FeedInboundChannelAdapterSample-context.xml");
    PollableChannel feedChannel = ac.getBean("feedChannel", PollableChannel.class);
    for (int i = 0; i < 10; i++) {
        Message<SyndEntry> message = (Message<SyndEntry>) feedChannel.receive(1000);
        if (message != null) {
            SyndEntry entry = message.getPayload();
            System.out.println(entry.getPublishedDate() + " - " + entry.getTitle());
        } else {
            break;
        }
    }
    ac.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Message(org.springframework.messaging.Message) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Example 4 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration-samples by spring-projects.

the class HelloWorldApp method main.

public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class);
    MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
    PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
    inputChannel.send(new GenericMessage<String>("World"));
    logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload());
    context.close();
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel)

Example 5 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration-samples by spring-projects.

the class ControlBusDemoTest method demoControlBus.

@Test
public void demoControlBus() {
    ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/ControlBusDemo-context.xml");
    MessageChannel controlChannel = ac.getBean("controlChannel", MessageChannel.class);
    PollableChannel adapterOutputChanel = ac.getBean("adapterOutputChanel", PollableChannel.class);
    logger.info("Received before adapter started: " + adapterOutputChanel.receive(1000));
    controlChannel.send(new GenericMessage<String>("@inboundAdapter.start()"));
    logger.info("Received before adapter started: " + adapterOutputChanel.receive(1000));
    controlChannel.send(new GenericMessage<String>("@inboundAdapter.stop()"));
    logger.info("Received after adapter stopped: " + adapterOutputChanel.receive(1000));
    ac.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Aggregations

PollableChannel (org.springframework.messaging.PollableChannel)210 Test (org.junit.Test)190 MessageChannel (org.springframework.messaging.MessageChannel)89 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)82 QueueChannel (org.springframework.integration.channel.QueueChannel)52 GenericMessage (org.springframework.messaging.support.GenericMessage)52 Message (org.springframework.messaging.Message)40 BeanFactory (org.springframework.beans.factory.BeanFactory)25 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)20 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)19 MessagingException (org.springframework.messaging.MessagingException)16 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)13 MessagingTemplate (org.springframework.integration.core.MessagingTemplate)12 ErrorMessage (org.springframework.messaging.support.ErrorMessage)12 Document (org.w3c.dom.Document)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Date (java.util.Date)10 ArrayList (java.util.ArrayList)9 MessageHistory (org.springframework.integration.history.MessageHistory)9