Camel Consumer not available within a Unit Test - ProducerTemplate

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Camel Consumer not available within a Unit Test - ProducerTemplate

Charles Moulliard-2
Hi,

I'm facing a strange issue that I don't understand. I can execute this unit
test case when I start the Test within IntelliJ but it will fail with mvn
clean test -Dtest= CamelIdempotentTest

As you can see, I use the parent method stopCamelContext() and
startCamelContext() to stop and start the CamelContext. The test fails at
the end as the direct consumer is not available when called by the
producerTemplate

Is there a workaround ?

org.apache.camel.component.direct.DirectConsumerNotAvailableException: No
consumers available on endpoint: Endpoint[direct://data-insert].
Exchange[Message: 333,18-05-2016,Claus,Ibsen,incident camel-333,this is a
report incident for camel-333,[hidden email],+111 10 20 300]
at
org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:47)
at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at
org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
at
org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)
at
org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:375)
at
org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:343)
at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:233)
at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:343)
at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:201)
at
org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:128)

Code

public class CamelIdempotentTest extends CamelSpringTestSupport {

    @EndpointInject(uri="direct:data-insert")
    ProducerTemplate template;

    @EndpointInject(uri="mock:result")
    MockEndpoint mockResult;

    @Test
    public void testStopStartCamelRoute() throws Exception {
        mockResult.expectedMessageCount(1);

        template.requestBodyAndHeader("111,22-04-2016,Claus,Ibsen,incident
camel-111,this is a report incident for camel-111,[hidden email],+111 10
20 300","CamelRecord",1);
        stopCamelContext();

        Connection conn = null;
        try {
            Class.forName("org.h2.Driver");
            conn =
DriverManager.getConnection("jdbc:h2:mem:idempotentReport");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from
CAMEL_MESSAGEPROCESSED");
            while (rs.next()) {
                Assert.assertEquals("1",rs.getString("messageId"));

Assert.assertEquals("DirectConsumer",rs.getString("processorName"));
            }
        } catch(Exception e) {
            System.out.print("Something happened");
        } finally {
            conn.close();
        }

        try {
            template.start();

template.requestBodyAndHeader("111,22-04-2016,Claus,Ibsen,incident
camel-111,this is a report incident for camel-111,[hidden email],+111 10
20 300","CamelRecord",1);

        } catch(CamelExecutionException e) {
            System.out.println("&&&&& The consumer endpoint is not started
so we can't use it");
        }

        startCamelContext();
        context().getRoute("direct-idempotent").getConsumer().start();
        template.requestBodyAndHeader("333,18-05-2016,Claus,Ibsen,incident
camel-333,this is a report incident for camel-333,[hidden email],+111 10
20 300","CamelRecord",1);

        mockResult.assertIsSatisfied();
    }

Regards,

--
Charles Moulliard
Apache Committer & PMC / Architect @RedHat
Twitter : @cmoulliard | Blog :  http://cmoulliard.github.io
Reply | Threaded
Open this post in threaded view
|

Re: Camel Consumer not available within a Unit Test - ProducerTemplate

Charles Moulliard-2
Problem resolved using

context.stopRoute("direct-idempotent");
....
context.startRoute("direct-idempotent");

I suspect that how the thread is paused/resume when using startCamel() &
stopCamelContext() is different from using stop/start route

On Fri, May 27, 2016 at 12:25 PM, Charles Moulliard <[hidden email]>
wrote:

> Hi,
>
> I'm facing a strange issue that I don't understand. I can execute this
> unit test case when I start the Test within IntelliJ but it will fail with
> mvn clean test -Dtest= CamelIdempotentTest
>
> As you can see, I use the parent method stopCamelContext() and
> startCamelContext() to stop and start the CamelContext. The test fails at
> the end as the direct consumer is not available when called by the
> producerTemplate
>
> Is there a workaround ?
>
> org.apache.camel.component.direct.DirectConsumerNotAvailableException: No
> consumers available on endpoint: Endpoint[direct://data-insert].
> Exchange[Message: 333,18-05-2016,Claus,Ibsen,incident camel-333,this is a
> report incident for camel-333,[hidden email],+111 10 20 300]
> at
> org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:47)
> at
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
> at
> org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
> at
> org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)
> at
> org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:375)
> at
> org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:343)
> at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:233)
> at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:343)
> at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:201)
> at
> org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:128)
>
> Code
>
> public class CamelIdempotentTest extends CamelSpringTestSupport {
>
>     @EndpointInject(uri="direct:data-insert")
>     ProducerTemplate template;
>
>     @EndpointInject(uri="mock:result")
>     MockEndpoint mockResult;
>
>     @Test
>     public void testStopStartCamelRoute() throws Exception {
>         mockResult.expectedMessageCount(1);
>
>         template.requestBodyAndHeader("111,22-04-2016,Claus,Ibsen,incident
> camel-111,this is a report incident for camel-111,[hidden email],+111
> 10 20 300","CamelRecord",1);
>         stopCamelContext();
>
>         Connection conn = null;
>         try {
>             Class.forName("org.h2.Driver");
>             conn =
> DriverManager.getConnection("jdbc:h2:mem:idempotentReport");
>             Statement stmt = conn.createStatement();
>             ResultSet rs = stmt.executeQuery("select * from
> CAMEL_MESSAGEPROCESSED");
>             while (rs.next()) {
>                 Assert.assertEquals("1",rs.getString("messageId"));
>
> Assert.assertEquals("DirectConsumer",rs.getString("processorName"));
>             }
>         } catch(Exception e) {
>             System.out.print("Something happened");
>         } finally {
>             conn.close();
>         }
>
>         try {
>             template.start();
>
> template.requestBodyAndHeader("111,22-04-2016,Claus,Ibsen,incident
> camel-111,this is a report incident for camel-111,[hidden email],+111
> 10 20 300","CamelRecord",1);
>
>         } catch(CamelExecutionException e) {
>             System.out.println("&&&&& The consumer endpoint is not started
> so we can't use it");
>         }
>
>         startCamelContext();
>         context().getRoute("direct-idempotent").getConsumer().start();
>         template.requestBodyAndHeader("333,18-05-2016,Claus,Ibsen,incident
> camel-333,this is a report incident for camel-333,[hidden email],+111
> 10 20 300","CamelRecord",1);
>
>         mockResult.assertIsSatisfied();
>     }
>
> Regards,
>
> --
> Charles Moulliard
> Apache Committer & PMC / Architect @RedHat
> Twitter : @cmoulliard | Blog :  http://cmoulliard.github.io
>
>


--
Charles Moulliard
Apache Committer & PMC / Architect @RedHat
Twitter : @cmoulliard | Blog :  http://cmoulliard.github.io