Search in sources :

Example 66 with Producer

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

the class ServicePoolTest method testThreeEntries.

public void testThreeEntries() throws Exception {
    Endpoint endpoint = context.getEndpoint("mock:foo");
    Producer producer1 = new MyProducer(endpoint);
    Producer producer2 = new MyProducer(endpoint);
    Producer producer3 = new MyProducer(endpoint);
    producer1 = pool.addAndAcquire(endpoint, producer1);
    producer2 = pool.addAndAcquire(endpoint, producer2);
    producer3 = pool.addAndAcquire(endpoint, producer3);
    assertEquals(0, pool.size());
    pool.release(endpoint, producer1);
    assertEquals(1, pool.size());
    pool.release(endpoint, producer2);
    assertEquals(2, pool.size());
    pool.release(endpoint, producer3);
    assertEquals(3, pool.size());
}
Also used : Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer)

Example 67 with Producer

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

the class ServicePoolTest method testConcurrent.

public void testConcurrent() throws Exception {
    final Endpoint endpoint = context.getEndpoint("mock:foo");
    ExecutorService executor = Executors.newFixedThreadPool(5);
    List<Future<Integer>> response = new ArrayList<Future<Integer>>();
    for (int i = 0; i < 5; i++) {
        final int index = i;
        Future<Integer> out = executor.submit(new Callable<Integer>() {

            public Integer call() throws Exception {
                Producer producer = pool.acquire(endpoint);
                if (producer == null) {
                    producer = pool.addAndAcquire(endpoint, new MyProducer(endpoint));
                }
                assertNotNull(producer);
                pool.release(endpoint, producer);
                return index;
            }
        });
        response.add(out);
    }
    for (int i = 0; i < 5; i++) {
        assertEquals(i, response.get(i).get().intValue());
    }
    executor.shutdownNow();
}
Also used : Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Endpoint(org.apache.camel.Endpoint)

Example 68 with Producer

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

the class ServicePoolTest method testAcquireAddRelease.

public void testAcquireAddRelease() throws Exception {
    Endpoint endpoint = context.getEndpoint("mock:foo");
    for (int i = 0; i < 10; i++) {
        Producer producer = pool.acquire(endpoint);
        if (producer == null) {
            producer = pool.addAndAcquire(endpoint, new MyProducer(endpoint));
        }
        assertNotNull(producer);
        pool.release(endpoint, producer);
    }
}
Also used : Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint)

Example 69 with Producer

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

the class ServicePoolTest method testConcurrentStress.

public void testConcurrentStress() throws Exception {
    final Endpoint endpoint = context.getEndpoint("mock:foo");
    ExecutorService executor = Executors.newFixedThreadPool(5);
    List<Future<Integer>> response = new ArrayList<Future<Integer>>();
    for (int i = 0; i < 5; i++) {
        final int index = i;
        Future<Integer> out = executor.submit(new Callable<Integer>() {

            public Integer call() throws Exception {
                for (int j = 0; j < 100; j++) {
                    Producer producer = pool.acquire(endpoint);
                    if (producer == null) {
                        producer = pool.addAndAcquire(endpoint, new MyProducer(endpoint));
                    }
                    assertNotNull(producer);
                    pool.release(endpoint, producer);
                }
                return index;
            }
        });
        response.add(out);
    }
    for (int i = 0; i < 5; i++) {
        assertEquals(i, response.get(i).get().intValue());
    }
    executor.shutdownNow();
}
Also used : Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Endpoint(org.apache.camel.Endpoint)

Example 70 with Producer

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

the class ProducerTest method testReleaseNoJobId.

@Test
public void testReleaseNoJobId() throws Exception {
    endpoint.setCommand(BeanstalkCommand.release);
    Producer producer = endpoint.createProducer();
    assertNotNull("Producer", producer);
    assertThat("Producer class", producer, instanceOf(BeanstalkProducer.class));
    assertThat("Processor class", ((BeanstalkProducer) producer).getCommand(), instanceOf(ReleaseCommand.class));
    final Exchange exchange = template.send(endpoint, ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) {
        }
    });
    assertTrue("Exchange failed", exchange.isFailed());
    verify(client, never()).release(anyLong(), anyLong(), anyInt());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) ReleaseCommand(org.apache.camel.component.beanstalk.processors.ReleaseCommand) Test(org.junit.Test)

Aggregations

Producer (org.apache.camel.Producer)198 Endpoint (org.apache.camel.Endpoint)140 Exchange (org.apache.camel.Exchange)138 Test (org.junit.Test)72 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)69 Processor (org.apache.camel.Processor)34 RouteBuilder (org.apache.camel.builder.RouteBuilder)23 Message (org.apache.camel.Message)21 CountDownLatch (java.util.concurrent.CountDownLatch)16 File (java.io.File)12 CamelContext (org.apache.camel.CamelContext)12 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)10 DefaultExchange (org.apache.camel.impl.DefaultExchange)9 Mockito.anyLong (org.mockito.Mockito.anyLong)9 Consumer (org.apache.camel.Consumer)8 FileDataSource (javax.activation.FileDataSource)7 AsyncProcessor (org.apache.camel.AsyncProcessor)7 DataHandler (javax.activation.DataHandler)6 Field (java.lang.reflect.Field)5 ExchangePattern (org.apache.camel.ExchangePattern)5