Example usage for org.springframework.integration.channel DirectChannel subscribe

List of usage examples for org.springframework.integration.channel DirectChannel subscribe

Introduction

In this page you can find the example usage for org.springframework.integration.channel DirectChannel subscribe.

Prototype

@Override
    public boolean subscribe(MessageHandler handler) 

Source Link

Usage

From source file:org.thys.michels.email2sfdc.email.GmailInboundPop3AdapterTestApp.java

public static void main(String[] args) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext(
            "/META-INF/spring/integration/gmail-pop3-config.xml");
    DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
    inputChannel.subscribe(new MessageHandler() {
        public void handleMessage(Message<?> message) throws MessagingException {
            logger.info("Message: " + message.getPayload() + " " + message.getHeaders());
        }//from  www. j a va 2  s.co m
    });
}

From source file:com.apress.prospringintegration.social.mail.ImapIdleMail.java

public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("/spring/mail/imap-idle-mail.xml");

    DirectChannel inputChannel = context.getBean("inputChannel", DirectChannel.class);

    inputChannel.subscribe(new MessageHandler() {
        public void handleMessage(Message<?> message) throws MessagingException {
            LOG.info("Message: " + message);
        }/* w w w . j a  va  2  s .com*/
    });
}

From source file:com.apress.prospringintegration.social.mail.ImapMail.java

public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("/spring/mail/imap-mail.xml");

    DirectChannel inputChannel = context.getBean("inputChannel", DirectChannel.class);

    inputChannel.subscribe(new MessageHandler() {
        public void handleMessage(Message<?> message) throws MessagingException {
            LOG.info("Message: " + message);
        }/*from  w  w  w  . ja va 2 s  .  c  o m*/
    });
}

From source file:com.apress.prospringintegration.social.mail.PopMail.java

public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("/spring/mail/pop-mail.xml");

    DirectChannel inputChannel = context.getBean("inputChannel", DirectChannel.class);

    inputChannel.subscribe(new MessageHandler() {
        public void handleMessage(Message<?> message) throws MessagingException {
            LOG.info("Message: " + message);
        }/*ww w.  ja  v  a 2  s  . com*/
    });
}

From source file:org.thys.michels.email2sfdc.email.GmailInboundImapIdleAdapterTestApp.java

public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "/META-INF/spring/integration/gmail-imap-idle-config.xml");

    final ParseEmail parseEmailMessage = new ParseEmail();

    DirectChannel inputChannel = context.getBean("inputChannel", DirectChannel.class);
    inputChannel.subscribe(new MessageHandler() {
        public void handleMessage(Message<?> message) throws MessagingException {
            //logger.info("Message: " + message);
            parseEmailMessage.printMessage(parseEmailMessage.getEmailMessage(message));

        }//from w w  w. java2 s. c om
    });
}

From source file:com.apress.prospringintegration.channels.directchannel.Main.java

public static void main(String[] args) throws Exception {

    String contextName = "direct-channel.xml";

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextName);
    applicationContext.start();//  w  ww  .java  2s  . c  o  m

    ProblemReporter problemReporter = applicationContext.getBean(ProblemReporter.class);
    TicketGenerator ticketGenerator = applicationContext.getBean(TicketGenerator.class);
    TicketMessageHandler ticketMessageHandler = applicationContext.getBean(TicketMessageHandler.class);

    DirectChannel channel = applicationContext.getBean("ticketChannel", DirectChannel.class);
    channel.subscribe(ticketMessageHandler);

    List<Ticket> tickets = ticketGenerator.createTickets();
    for (Ticket ticket : tickets) {
        problemReporter.openTicket(ticket);
    }
}

From source file:mx.uaq.facturacion.enlace.system.EmailSystemTest.java

private DirectChannel loggerMessages(String channel) {
    DirectChannel directChannel = new DirectChannel();
    directChannel.subscribe((message) -> {
        System.out.println(channel + ": " + message.getPayload());
    });//ww  w. j a va2 s .c  o m
    return directChannel;
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testSendAndReceiveBad() throws Exception {
    RabbitTestBinder binder = getBinder();
    DirectChannel moduleOutputChannel = createBindableChannel("output", new BindingProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", new BindingProperties());
    Binding<MessageChannel> producerBinding = binder.bindProducer("bad.0", moduleOutputChannel,
            createProducerProperties());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("bad.0", "test", moduleInputChannel,
            createConsumerProperties());
    Message<?> message = MessageBuilder.withPayload("bad").setHeader(MessageHeaders.CONTENT_TYPE, "foo/bar")
            .build();/*from   w  w w.  j  ava 2s.  c o m*/
    final CountDownLatch latch = new CountDownLatch(3);
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            latch.countDown();
            throw new RuntimeException("bad");
        }
    });
    moduleOutputChannel.send(message);
    assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
    producerBinding.unbind();
    consumerBinding.unbind();
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testDurablePubSubWithAutoBindDLQ() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

    RabbitTestBinder binder = getBinder();

    ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
    consumerProperties.getExtension().setPrefix(TEST_PREFIX);
    consumerProperties.getExtension().setAutoBindDlq(true);
    consumerProperties.getExtension().setDurableSubscription(true);
    consumerProperties.setMaxAttempts(1); // disable retry
    DirectChannel moduleInputChannel = createBindableChannel("input",
            createConsumerBindingProperties(consumerProperties));
    moduleInputChannel.setBeanName("durableTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override//w w w .j ava2 s.c om
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("durabletest.0", "tgroup", moduleInputChannel,
            consumerProperties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend(TEST_PREFIX + "durabletest.0", "", "foo");

    int n = 0;
    while (n++ < 100) {
        Object deadLetter = template.receiveAndConvert(TEST_PREFIX + "durabletest.0.tgroup.dlq");
        if (deadLetter != null) {
            assertThat(deadLetter).isEqualTo("foo");
            break;
        }
        Thread.sleep(100);
    }
    assertThat(n).isLessThan(100);

    consumerBinding.unbind();
    assertThat(admin.getQueueProperties(TEST_PREFIX + "durabletest.0.tgroup.dlq")).isNotNull();
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testNonDurablePubSubWithAutoBindDLQ() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
    consumerProperties.getExtension().setPrefix(TEST_PREFIX);
    consumerProperties.getExtension().setAutoBindDlq(true);
    consumerProperties.getExtension().setDurableSubscription(false);
    consumerProperties.setMaxAttempts(1); // disable retry
    BindingProperties bindingProperties = createConsumerBindingProperties(consumerProperties);
    DirectChannel moduleInputChannel = createBindableChannel("input", bindingProperties);
    moduleInputChannel.setBeanName("nondurabletest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override//from ww  w. ja va2  s.  co m
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("nondurabletest.0", "tgroup",
            moduleInputChannel, consumerProperties);

    consumerBinding.unbind();
    assertThat(admin.getQueueProperties(TEST_PREFIX + "nondurabletest.0.dlq")).isNull();
}