Search in sources :

Example 71 with Producer

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

the class ProducerTest method testPutWithHeaders.

@Test
public void testPutWithHeaders() throws Exception {
    final long priority = 111;
    final int delay = 5;
    final int timeToRun = 65;
    final byte[] payload = Helper.stringToBytes(testMessage);
    final long jobId = 111;
    when(client.put(priority, delay, timeToRun, payload)).thenReturn(jobId);
    Producer producer = endpoint.createProducer();
    assertNotNull("Producer", producer);
    assertThat("Producer class", producer, instanceOf(BeanstalkProducer.class));
    assertThat("Processor class", ((BeanstalkProducer) producer).getCommand(), instanceOf(PutCommand.class));
    final Exchange exchange = template.send(endpoint, ExchangePattern.InOnly, new // TODO: SetBodyProcessor(?)
    Processor() {

        public void process(Exchange exchange) {
            exchange.getIn().setHeader(Headers.PRIORITY, priority);
            exchange.getIn().setHeader(Headers.DELAY, delay);
            exchange.getIn().setHeader(Headers.TIME_TO_RUN, timeToRun);
            exchange.getIn().setBody(testMessage);
        }
    });
    assertEquals("Job ID in exchange", Long.valueOf(jobId), exchange.getIn().getHeader(Headers.JOB_ID, Long.class));
    verify(client).put(priority, delay, timeToRun, payload);
}
Also used : Exchange(org.apache.camel.Exchange) Producer(org.apache.camel.Producer) Mockito.anyLong(org.mockito.Mockito.anyLong) PutCommand(org.apache.camel.component.beanstalk.processors.PutCommand) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 72 with Producer

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

the class ProducerTest method testTouch.

@Test
public void testTouch() throws Exception {
    final long jobId = 111;
    endpoint.setCommand(BeanstalkCommand.touch);
    Producer producer = endpoint.createProducer();
    assertNotNull("Producer", producer);
    assertThat("Producer class", producer, instanceOf(BeanstalkProducer.class));
    assertThat("Processor class", ((BeanstalkProducer) producer).getCommand(), instanceOf(TouchCommand.class));
    when(client.touch(jobId)).thenReturn(true);
    final Exchange exchange = template.send(endpoint, ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) {
            exchange.getIn().setHeader(Headers.JOB_ID, jobId);
        }
    });
    assertEquals("Op result", Boolean.TRUE, exchange.getIn().getHeader(Headers.RESULT, Boolean.class));
    assertEquals("Job ID in exchange", Long.valueOf(jobId), exchange.getIn().getHeader(Headers.JOB_ID, Long.class));
    verify(client).touch(jobId);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) TouchCommand(org.apache.camel.component.beanstalk.processors.TouchCommand) Mockito.anyLong(org.mockito.Mockito.anyLong) Test(org.junit.Test)

Example 73 with Producer

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

the class ProducerTest method testDelete.

@Test
public void testDelete() throws Exception {
    final long jobId = 111;
    endpoint.setCommand(BeanstalkCommand.delete);
    Producer producer = endpoint.createProducer();
    assertNotNull("Producer", producer);
    assertThat("Producer class", producer, instanceOf(BeanstalkProducer.class));
    assertThat("Processor class", ((BeanstalkProducer) producer).getCommand(), instanceOf(DeleteCommand.class));
    when(client.delete(jobId)).thenReturn(true);
    final Exchange exchange = template.send(endpoint, ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) {
            exchange.getIn().setHeader(Headers.JOB_ID, jobId);
        }
    });
    assertEquals("Op result", Boolean.TRUE, exchange.getIn().getHeader(Headers.RESULT, Boolean.class));
    assertEquals("Job ID in exchange", Long.valueOf(jobId), exchange.getIn().getHeader(Headers.JOB_ID, Long.class));
    verify(client).delete(jobId);
}
Also used : DeleteCommand(org.apache.camel.component.beanstalk.processors.DeleteCommand) Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) Mockito.anyLong(org.mockito.Mockito.anyLong) Test(org.junit.Test)

Example 74 with Producer

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

the class ProducerTest method testRelease.

@Test
public void testRelease() throws Exception {
    final long priority = BeanstalkComponent.DEFAULT_PRIORITY;
    final int delay = BeanstalkComponent.DEFAULT_DELAY;
    final long jobId = 111;
    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));
    when(client.release(jobId, priority, delay)).thenReturn(true);
    final Exchange exchange = template.send(endpoint, ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) {
            exchange.getIn().setHeader(Headers.JOB_ID, jobId);
        }
    });
    assertEquals("Op result", Boolean.TRUE, exchange.getIn().getHeader(Headers.RESULT, Boolean.class));
    assertEquals("Job ID in exchange", Long.valueOf(jobId), exchange.getIn().getHeader(Headers.JOB_ID, Long.class));
    verify(client).release(jobId, priority, delay);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) Mockito.anyLong(org.mockito.Mockito.anyLong) ReleaseCommand(org.apache.camel.component.beanstalk.processors.ReleaseCommand) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 75 with Producer

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

the class RestEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    RestProducerFactory apiDocFactory = null;
    RestProducerFactory factory = null;
    if (apiDoc != null) {
        LOG.debug("Discovering camel-swagger-java on classpath for using api-doc: {}", apiDoc);
        // lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
        try {
            FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
            Object instance = finder.newInstance(DEFAULT_API_COMPONENT_NAME);
            if (instance instanceof RestProducerFactory) {
                // this factory from camel-swagger-java will facade the http component in use
                apiDocFactory = (RestProducerFactory) instance;
            }
            parameters.put("apiDoc", apiDoc);
        } catch (NoFactoryAvailableException e) {
            throw new IllegalStateException("Cannot find camel-swagger-java on classpath to use with api-doc: " + apiDoc);
        }
    }
    String cname = getComponentName();
    if (cname != null) {
        Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
        if (comp != null && comp instanceof RestProducerFactory) {
            factory = (RestProducerFactory) comp;
        } else {
            comp = getCamelContext().getComponent(getComponentName());
            if (comp != null && comp instanceof RestProducerFactory) {
                factory = (RestProducerFactory) comp;
            }
        }
        if (factory == null) {
            if (comp != null) {
                throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestProducerFactory");
            } else {
                throw new NoSuchBeanException(getComponentName(), RestProducerFactory.class.getName());
            }
        }
        cname = getComponentName();
    }
    // try all components
    if (factory == null) {
        for (String name : getCamelContext().getComponentNames()) {
            Component comp = getCamelContext().getComponent(name);
            if (comp != null && comp instanceof RestProducerFactory) {
                factory = (RestProducerFactory) comp;
                cname = name;
                break;
            }
        }
    }
    parameters.put("componentName", cname);
    // lookup in registry
    if (factory == null) {
        Set<RestProducerFactory> factories = getCamelContext().getRegistry().findByType(RestProducerFactory.class);
        if (factories != null && factories.size() == 1) {
            factory = factories.iterator().next();
        }
    }
    // and there must only be exactly one so we safely can pick this one
    if (factory == null) {
        RestProducerFactory found = null;
        String foundName = null;
        for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
            Object comp = getCamelContext().getComponent(name, true);
            if (comp != null && comp instanceof RestProducerFactory) {
                if (found == null) {
                    found = (RestProducerFactory) comp;
                    foundName = name;
                } else {
                    throw new IllegalArgumentException("Multiple RestProducerFactory found on classpath. Configure explicit which component to use");
                }
            }
        }
        if (found != null) {
            LOG.debug("Auto discovered {} as RestProducerFactory", foundName);
            factory = found;
        }
    }
    if (factory != null) {
        LOG.debug("Using RestProducerFactory: {}", factory);
        Producer producer;
        if (apiDocFactory != null) {
            // wrap the factory using the api doc factory which will use the factory
            parameters.put("restProducerFactory", factory);
            producer = apiDocFactory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
        } else {
            producer = factory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
        }
        RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
        RestProducer answer = new RestProducer(this, producer, config);
        answer.setOutType(outType);
        answer.setType(inType);
        return answer;
    } else {
        throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
    }
}
Also used : NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestProducerFactory(org.apache.camel.spi.RestProducerFactory) Producer(org.apache.camel.Producer) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component) FactoryFinder(org.apache.camel.spi.FactoryFinder)

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