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);
}
}
}
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;
}
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();
}
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());
}
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
}
}
Aggregations