Search in sources :

Example 6 with PollingConsumer

use of org.apache.camel.PollingConsumer in project camel by apache.

the class ConsumerCache method receive.

public Exchange receive(Endpoint endpoint) {
    LOG.debug("<<<< {}", endpoint);
    PollingConsumer consumer = null;
    try {
        consumer = acquirePollingConsumer(endpoint);
        return consumer.receive();
    } finally {
        if (consumer != null) {
            releasePollingConsumer(endpoint, consumer);
        }
    }
}
Also used : PollingConsumer(org.apache.camel.PollingConsumer)

Example 7 with PollingConsumer

use of org.apache.camel.PollingConsumer in project camel by apache.

the class ConsumerCache method getCapacity.

/**
     * Gets the maximum cache size (capacity).
     * <p/>
     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
     *
     * @return the capacity
     */
public int getCapacity() {
    int capacity = -1;
    if (consumers instanceof LRUCache) {
        LRUCache<String, PollingConsumer> cache = (LRUCache<String, PollingConsumer>) consumers;
        capacity = cache.getMaxCacheSize();
    }
    return capacity;
}
Also used : LRUCache(org.apache.camel.util.LRUCache) PollingConsumer(org.apache.camel.PollingConsumer) Endpoint(org.apache.camel.Endpoint)

Example 8 with PollingConsumer

use of org.apache.camel.PollingConsumer in project camel by apache.

the class FilePollingConsumerTest method testPollingConsumer.

public void testPollingConsumer() throws Exception {
    template.sendBodyAndHeader("file:target/enrich", "Hello World", Exchange.FILE_NAME, "hello.txt");
    PollingConsumer consumer = context.getEndpoint("file:target/enrich").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(500);
    // drop a new file which should not be picked up by the consumer
    template.sendBodyAndHeader("file:target/enrich", "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("target/enrich/bye.txt");
    assertTrue("File should exist " + file, file.exists());
    consumer.stop();
}
Also used : Exchange(org.apache.camel.Exchange) PollingConsumer(org.apache.camel.PollingConsumer) File(java.io.File)

Example 9 with PollingConsumer

use of org.apache.camel.PollingConsumer in project camel by apache.

the class EventDrivenPollingConsumerQueueSizeTest method testQueueSize.

public void testQueueSize() throws Exception {
    // must start context as we do not use route builder that auto-start
    context.start();
    PollingConsumer consumer = context.getEndpoint(uri).createPollingConsumer();
    consumer.start();
    assertNotNull(consumer);
    EventDrivenPollingConsumer edpc = assertIsInstanceOf(EventDrivenPollingConsumer.class, consumer);
    assertEquals(0, edpc.getQueueSize());
    assertEquals(10, edpc.getQueueCapacity());
    assertFalse(edpc.isBlockWhenFull());
    for (int i = 0; i < 10; i++) {
        template.sendBody(uri, "Message " + i);
    }
    assertEquals(10, edpc.getQueueSize());
    try {
        template.sendBody(uri, "Message 10");
        fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
        // queue should be full
        assertIsInstanceOf(IllegalStateException.class, e.getCause());
    }
    Exchange out = consumer.receive(5000);
    assertNotNull(out);
    assertEquals("Message 0", out.getIn().getBody());
    assertEquals(9, edpc.getQueueSize());
    assertEquals(10, edpc.getQueueCapacity());
    // now there is room
    template.sendBody(uri, "Message 10");
    assertEquals(10, edpc.getQueueSize());
    assertEquals(10, edpc.getQueueCapacity());
    ServiceHelper.stopService(consumer);
    // not cleared if we stop
    assertEquals(10, edpc.getQueueSize());
    assertEquals(10, edpc.getQueueCapacity());
    ServiceHelper.stopAndShutdownService(consumer);
    // now its cleared as we shutdown
    assertEquals(0, edpc.getQueueSize());
    assertEquals(10, edpc.getQueueCapacity());
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) Exchange(org.apache.camel.Exchange) PollingConsumer(org.apache.camel.PollingConsumer) Endpoint(org.apache.camel.Endpoint)

Example 10 with PollingConsumer

use of org.apache.camel.PollingConsumer in project camel by apache.

the class InvalidConfigurationTest method testSMTPCanNotBeUsedForConsumingMails.

@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
    }
}
Also used : PollingConsumer(org.apache.camel.PollingConsumer) Endpoint(org.apache.camel.Endpoint) Test(org.junit.Test)

Aggregations

PollingConsumer (org.apache.camel.PollingConsumer)21 Endpoint (org.apache.camel.Endpoint)9 Exchange (org.apache.camel.Exchange)9 Test (org.junit.Test)5 File (java.io.File)2 FailedToCreateConsumerException (org.apache.camel.FailedToCreateConsumerException)2 IsSingleton (org.apache.camel.IsSingleton)2 ArrayList (java.util.ArrayList)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 CamelExecutionException (org.apache.camel.CamelExecutionException)1 Consumer (org.apache.camel.Consumer)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 Processor (org.apache.camel.Processor)1 ProxyInstantiationException (org.apache.camel.ProxyInstantiationException)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 ServicePoolAware (org.apache.camel.ServicePoolAware)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)1