camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Quinn Stevenson <qu...@pronoia-solutions.com>
Subject Re: Handling the reply of a request in InOut mode
Date Tue, 28 Jun 2016 16:44:39 GMT
I’m not sure exactly what you’re trying to do with the processors, but here’s a simple
unit test that works for me.  You can mess with the processors and make them act more like
your real route.

package com.pronoia.camel;

import java.util.concurrent.TimeUnit;

import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.activemq.junit.EmbeddedActiveMQBroker;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Rule;
import org.junit.Test;

public class JavaRequestResponseTest extends CamelTestSupport {
    @Rule
    public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();

    Processor requestProcessor = new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            String body = exchange.getIn().getBody(String.class);
            exchange.getIn().setBody("Request: " + body);

            return;
        }
    };

    Processor resultProcessor = new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            String body = exchange.getIn().getBody(String.class);
            if ( body.startsWith("Response") ) {
                exchange.getIn().setBody( "We're good");
            } else {
                exchange.getIn().setBody( "Bad response");
            }
            return;
        }
    };

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

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();

        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setBrokerURL( broker.getVmURL() );

        registry.bind( "test-broker", activeMQComponent);

        return registry;
    }

    @Override
    protected RouteBuilder[] createRouteBuilders() throws Exception {
        RouteBuilder builders[] = new RouteBuilder[2];

        builders[1] = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("file://data?fileName=some-file.txt&noop=true")
                        .id( "file-consumer")
                        .log("Processing ${file:name}")
                        .process(requestProcessor)
                        .log("Generated ${body}")
                        .to(ExchangePattern.InOut, "test-broker://queue:testQueue")
                        .process(resultProcessor)
                        .to( "mock://result")
                ;
            }
        };

        builders[0] = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from( "test-broker://queue:testQueue")
                        .id( "dummey-response-generator")
                        .setBody().constant( "Response Message")
                        .to(ExchangePattern.InOnly, "test-broker://queue:dummy-queue")
                ;

            }
        };

        return builders;
    }

    @Test
    public void testRoute() throws Exception {
        result.expectedBodiesReceived("We're good");

        assertMockEndpointsSatisfied(5, TimeUnit.SECONDS);
    }

}

> On Jun 28, 2016, at 8:47 AM, Thomas Hehl <thom@heavyweightsoftware.com> wrote:
> 
> Referring to: http://camel.apache.org/jms.html
> 
> I'm trying to handle a reply in request-reply mode of an active MQ queue. Here is the
code form the page above:
> 
> I have tried to do this:
> 
> from("file://C:\\Users\\Thom\\git\\brms-poc-esb\\rule-cancel\\data").
>                        log("Processing ${file:name}").
>                        process(requestProcess).
>                        log("Generated ${body}").
>                        to(queueUri).inOut().
>                        process(new ResultProcess());
> 
> When I process this, I am still getting the stream I sent to my active MQ.
> 
> How do I process the reply?
> 
> 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message