Search in sources :

Example 6 with ProducerTemplate

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

the class DisruptorConcurrentTest method testDisruptorConcurrentInOutWithAsync.

@Test
public void testDisruptorConcurrentInOutWithAsync() throws Exception {
    final MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(20);
    mock.allMessages().body().startsWith("Bye");
    // should at least take 3 sec
    mock.setMinimumResultWaitTime(3000);
    // use our own template that has a higher thread pool than default camel that uses 5
    final ExecutorService executor = Executors.newFixedThreadPool(10);
    final ProducerTemplate pt = new DefaultProducerTemplate(context, executor);
    // must start the template
    pt.start();
    final List<Future<Object>> replies = new ArrayList<Future<Object>>(20);
    for (int i = 0; i < 20; i++) {
        final Future<Object> out = pt.asyncRequestBody("disruptor:bar", "Message " + i);
        replies.add(out);
    }
    assertMockEndpointsSatisfied();
    assertEquals(20, replies.size());
    for (int i = 0; i < 20; i++) {
        final String out = (String) replies.get(i).get();
        assertTrue(out.startsWith("Bye"));
    }
    pt.stop();
    executor.shutdownNow();
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 7 with ProducerTemplate

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

the class DozerBeanMappingTest method testBeanMapping.

@Test
public void testBeanMapping() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").convertBodyTo(CustomerB.class);
        }
    });
    DozerBeanMapperConfiguration mconfig = new DozerBeanMapperConfiguration();
    mconfig.setMappingFiles(Arrays.asList("bean-to-bean-dozer-mappings.xml"));
    new DozerTypeConverterLoader(context, mconfig);
    CustomerA customerA = new CustomerA("Peter", "Post", "SomeStreet", "12345");
    context.start();
    try {
        ProducerTemplate producer = context.createProducerTemplate();
        CustomerB result = producer.requestBody("direct:start", customerA, CustomerB.class);
        Assert.assertEquals(customerA.getFirstName(), result.getFirstName());
        Assert.assertEquals(customerA.getLastName(), result.getLastName());
        Assert.assertEquals(customerA.getStreet(), result.getAddress().getStreet());
        Assert.assertEquals(customerA.getZip(), result.getAddress().getZip());
    } finally {
        context.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) CustomerA(org.apache.camel.converter.dozer.model.CustomerA) RouteBuilder(org.apache.camel.builder.RouteBuilder) CustomerB(org.apache.camel.converter.dozer.model.CustomerB) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 8 with ProducerTemplate

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

the class DozerBeanMappingTest method testMarshalViaDozer.

@Test
public void testMarshalViaDozer() throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").convertBodyTo(HashMap.class);
        }
    });
    DozerBeanMapperConfiguration mconfig = new DozerBeanMapperConfiguration();
    mconfig.setMappingFiles(Arrays.asList("bean-to-map-dozer-mappings.xml"));
    new DozerTypeConverterLoader(context, mconfig);
    context.start();
    try {
        ProducerTemplate producer = context.createProducerTemplate();
        Map<?, ?> result = producer.requestBody("direct:start", new Customer("John", "Doe", null), Map.class);
        Assert.assertEquals("John", result.get("firstName"));
        Assert.assertEquals("Doe", result.get("lastName"));
    } finally {
        context.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) Customer(org.apache.camel.converter.dozer.model.Customer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 9 with ProducerTemplate

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

the class ExecJava8IssueTest method test.

@Test
public void test() throws Exception {
    if (!OS.isFamilyUnix()) {
        System.err.println("The test 'CamelExecTest' does not support the following OS : " + System.getProperty("os.name"));
        return;
    }
    String tempFilePath = tempDir.getAbsolutePath() + "/" + tempFileName;
    final File script = File.createTempFile("script", ".sh", tempDir);
    writeScript(script);
    final String exec = "bash?args=" + script.getAbsolutePath() + " " + tempFilePath + "&outFile=" + tempFilePath;
    DefaultCamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:source").to("file:" + tempDir.getAbsolutePath() + "?fileName=" + tempFileName).to("exec:" + exec).process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    String output = exchange.getIn().getBody(String.class);
                    assertEquals("hello world\n", output);
                }
            });
        }
    });
    context.start();
    ProducerTemplate pt = context.createProducerTemplate();
    String payload = "hello";
    pt.sendBody("direct:source", payload);
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) File(java.io.File) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with ProducerTemplate

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

the class StopRouteFromRouteTest method testStopRouteFromRoute.

// START SNIPPET: e1
public void testStopRouteFromRoute() throws Exception {
    // create camel, add routes, and start camel
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(createMyRoutes());
    context.start();
    assertTrue("Route myRoute should be started", context.getRouteStatus("myRoute").isStarted());
    assertTrue("Route bar should be started", context.getRouteStatus("bar").isStarted());
    // setup mock expectations for unit test
    MockEndpoint start = context.getEndpoint("mock:start", MockEndpoint.class);
    start.expectedMessageCount(1);
    MockEndpoint done = context.getEndpoint("mock:done", MockEndpoint.class);
    done.expectedMessageCount(1);
    // send a message to the route
    ProducerTemplate template = context.createProducerTemplate();
    template.sendBody("direct:start", "Hello Camel");
    // just wait a bit for the thread to stop the route
    Thread.sleep(1500);
    // the route should now be stopped
    assertTrue("Route myRoute should be stopped", context.getRouteStatus("myRoute").isStopped());
    assertTrue("Route bar should be started", context.getRouteStatus("bar").isStarted());
    // stop camel
    context.stop();
    // unit test assertions
    start.assertIsSatisfied();
    done.assertIsSatisfied();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Aggregations

ProducerTemplate (org.apache.camel.ProducerTemplate)130 Test (org.junit.Test)58 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)53 CamelContext (org.apache.camel.CamelContext)48 Exchange (org.apache.camel.Exchange)36 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)27 Deployment (org.activiti.engine.test.Deployment)16 RouteBuilder (org.apache.camel.builder.RouteBuilder)16 Processor (org.apache.camel.Processor)8 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 HashMap (java.util.HashMap)7 Task (org.activiti.engine.task.Task)7 Endpoint (org.apache.camel.Endpoint)7 File (java.io.File)6 ArrayList (java.util.ArrayList)5 Message (org.apache.camel.Message)4 List (java.util.List)3 ExecutorService (java.util.concurrent.ExecutorService)3 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)3 ConsumerTemplate (org.apache.camel.ConsumerTemplate)3