开发者

Invoking a Camel end point deployed in remote Server from JUnit test case

I am a new to Camel and have to deliver a module in a very short notice. My question may be a very basic question but I would really appreciate if someone could guide me on it.

The requirement is to invoke a Camel endpoint service deployed in a Tomcat server from a jUnit test case. The Service has been injected with the CamelContext and it has got a set of exposed methods which needs to be called. We are using Spring 2.5 and Camel 2 in our project. The Spring config is below

<bean name="/DispatcherService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="dispatcherService">
    <property name="serviceInterface" value="test.DispatcherService">
</bean>

<camelContext id="dispatcherCamelContext" trace="true" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:dispatcherChannel" />
            <!-- use comma as a delimiter for String based values -->
            <recipientList delimiter=",">
                <header&gt;serviceEndpoints&lt;/header>
            </recipientList>
    </route>
</camelContext>

<bean id="dispatcherService" class="test.DispatcherServiceImpl">
    <property name="context" ref="dispatcherCamelContext" />
</bean>

What I am not able to find is to find how can we call the end point URI direct:dis开发者_StackOverflow社区patcherChannel deployed in a tomcat server (http://someIP:8080) from a jUnit which uses Spring configuration.


The "direct" endpoint is only accessible from within the same VM. If you need to access a route externally, you can do this with JMX or by wrapping it with another route using JMS or HTTP. Either approach will allow you to manually test/debug a deployed route...

  • with JMX, you just need to go navigate to your Camel Context MBean (using jconsole, etc) and execute the sendBody("direct:dispatcherChannel","test message") operation

  • to wrap the route with HTTP, just add this route, then go to this URL in a browser to invoke the route...

    from("jetty:http://0.0.0.0:9001/invokeDispatcherChannel")
    .to("direct:dispatcherChannel");
    
  • if you need to send a payload, you might consider exposing the route via JMS (or WS, etc) and convert to the expected format before invoking the route. Then you can just drop a message in the queue (using JMX, AMQ web console, etc) to invoke the direct route.

    from("activemq:queue:invokeDispatcherChannel")
    .process(new MyMessageConverterProcessor())
    .to("direct:dispatcherChannel");
    


You can't do that, as direct: endpoints represent direct method calls (i.e. you need to be in the same process as the application). In order to be able to call direct: endpoints from tests, you will need to start up the CamelContext in the tests themselves. Obviously, this is only usable when you need to test separate routes or your context is really small.

Tests interacting with an already deployed application should be regarded as integration/system tests. You can write JUnit tests for these scenarios, but you should interact with the application through the exposed interface (http: endpoints, etc.).


As you already inject the camel context into the test.DispatcherServiceImpl, you just need to use the camel ProducerTemplate to send the request to the "direct:dispatcherChannel" like this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜