Package org.apache.camel

Examples of org.apache.camel.PollingConsumer


     * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
     * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
     * down the consumer and throws any exceptions thrown.
     */
    public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
        PollingConsumer consumer = endpoint.createPollingConsumer();
        try {
            consumer.start();

            while (true) {
                Exchange exchange = consumer.receive(timeout);
                if (exchange == null) {
                    break;
                } else {
                    processor.process(exchange);
                }
            }
        } finally {
            try {
                consumer.stop();
            } catch (Exception e) {
                LOG.warn("Failed to stop PollingConsumer: " + e, e);
            }
        }
    }
View Full Code Here


    @Test
    public void testUriIssue() throws Exception {
        String uri = "rss:http://api.flickr.com/services/feeds/photos_public.gne?id=23353282@N05&tags=lowlands&lang=en-us&format=rss_200";

        PollingConsumer consumer = context.getEndpoint(uri).createPollingConsumer();
        consumer.start();
        Exchange exchange = consumer.receive();
        log.info("Receive " + exchange);
        assertNotNull(exchange);
        assertNotNull(exchange.getIn().getBody());
        consumer.stop();
    }
View Full Code Here

public class InvalidConfigurationTest extends CamelTestSupport {

    @Test
    public void testSMTPCanNotBeUsedForConsumingMails() throws Exception {
        Endpoint endpoint = context.getEndpoint("smtp://localhost?username=james");
        PollingConsumer consumer = endpoint.createPollingConsumer();
        try {
            consumer.start();
            fail("Should have thrown NoSuchProviderException as stmp protocol cannot be used for consuming mails");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }
View Full Code Here

    }

    @Test
    public void testSMTPSCanNotBeUsedForConsumingMails() throws Exception {
        Endpoint endpoint = context.getEndpoint("smtps://localhost?username=james");
        PollingConsumer consumer = endpoint.createPollingConsumer();
        try {
            consumer.start();
            fail("Should have thrown NoSuchProviderException as stmp protocol cannot be used for consuming mails");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }
View Full Code Here

        prepareFtpServer();
    }

    @Test
    public void testConsumerDeleteNoWritePermission() throws Exception {
        PollingConsumer consumer = context.getEndpoint(getFtpUrl()).createPollingConsumer();
        consumer.start();
        Exchange out = consumer.receive(3000);
        assertNotNull("Should get the file", out);

        try {
            // give consumer time to try to delete the file
            Thread.sleep(1000);
            consumer.stop();
        } catch (GenericFileOperationFailedException fofe) {
            // expected, ignore
        }
    }
View Full Code Here

                        // create a ftp endpoint
                        Endpoint ftp = context.getEndpoint(url);

                        // create a polling consumer where we can poll the myfile from the ftp server
                        PollingConsumer consumer = ftp.createPollingConsumer();

                        // must start the consumer before we can receive
                        consumer.start();

                        // poll the file from the ftp server
                        Exchange result = consumer.receive();

                        // the result is the response from the FTP consumer (the downloaded file)
                        // replace the outher exchange with the content from the downloaded file
                        exchange.getIn().setBody(result.getIn().getBody());

                        // stop the consumer
                        consumer.stop();
                    }
                }).to("mock:result");
                // END SNIPPET: e2
            }
        };
View Full Code Here

     * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
     * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
     * down the consumer and throws any exceptions thrown.
     */
    public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
        PollingConsumer consumer = endpoint.createPollingConsumer();
        try {
            consumer.start();

            while (true) {
                Exchange exchange = consumer.receive(timeout);
                if (exchange == null) {
                    break;
                } else {
                    processor.process(exchange);
                }
            }
        } finally {
            try {
                consumer.stop();
            } catch (Exception e) {
                LOG.warn("Failed to stop PollingConsumer: " + e, e);
            }
        }
    }
View Full Code Here

    @Test
    public void testPollingConsumer() throws Exception {
        template.sendBodyAndHeader(getFtpUrl(), "Hello World", Exchange.FILE_NAME, "hello.txt");

        PollingConsumer consumer = context.getEndpoint(getFtpUrl()).createPollingConsumer();
        consumer.start();
        Exchange exchange = consumer.receive(5000);
        assertNotNull(exchange);
        assertEquals("Hello World", exchange.getIn().getBody(String.class));

        // sleep a bit to ensure polling consumer would be suspended after we have used it
        Thread.sleep(1000);

        // drop a new file which should not be picked up by the consumer
        template.sendBodyAndHeader(getFtpUrl(), "Bye World", Exchange.FILE_NAME, "bye.txt");

        // sleep a bit to ensure polling consumer would not have picked up that file
        Thread.sleep(1000);

        File file = new File(FTP_ROOT_DIR + "polling/bye.txt").getAbsoluteFile();
        assertTrue("File should exist " + file, file.exists());

        consumer.stop();
    }
View Full Code Here

     * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
     * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
     * down the consumer and throws any exceptions thrown.
     */
    public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
        PollingConsumer consumer = endpoint.createPollingConsumer();
        try {
            consumer.start();

            while (true) {
                Exchange exchange = consumer.receive(timeout);
                if (exchange == null) {
                    break;
                } else {
                    processor.process(exchange);
                }
            }
        } finally {
            try {
                consumer.stop();
            } catch (Exception e) {
                LOG.warn("Failed to stop PollingConsumer: " + e, e);
            }
        }
    }
View Full Code Here

    /**
     * Factory method to create a started {@link org.apache.camel.PollingConsumer} to be injected into a POJO
     */
    protected PollingConsumer createInjectionPollingConsumer(Endpoint endpoint, Object bean, String beanName) {
        try {
            PollingConsumer pollingConsumer = endpoint.createPollingConsumer();
            startService(pollingConsumer, bean, beanName);
            return pollingConsumer;
        } catch (Exception e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
View Full Code Here

TOP

Related Classes of org.apache.camel.PollingConsumer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.