从 JUnit 测试用例调用部署在远程服务器中的 Camel 端点
我是 Camel 的新手,必须在很短的时间内交付一个模块。我的问题可能是一个非常基本的问题,但如果有人可以指导我,我将非常感激。
要求是从 jUnit 测试用例调用部署在 Tomcat 服务器中的 Camel 端点服务。该服务已注入 CamelContext,并且它有一组需要调用的公开方法。我们在项目中使用 Spring 2.5 和 Camel 2。 Spring配置如下
<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>serviceEndpoints</header>
</recipientList>
</route>
</camelContext>
<bean id="dispatcherService" class="test.DispatcherServiceImpl">
<property name="context" ref="dispatcherCamelContext" />
</bean>
我找不到的是如何从jUnit调用部署在tomcat服务器(http://someIP:8080)中的端点URI direct:dispatcherChannel
它使用 Spring 配置。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“直接”端点只能从同一虚拟机内访问。如果您需要从外部访问路由,可以使用 JMX 或用另一个包来实现使用 JMS 或 HTTP。这两种方法都允许您手动测试/调试已部署的路由...
使用 JMX ,您只需要导航到 Camel Context MBean(使用 jconsole 等)并
用 HTTP 封装路由,只需添加此路由,然后在浏览器中转到此 URL 即可调用该路由...
如果您需要发送有效负载,您可能会考虑公开通过 JMS(或 WS 等)获取路由,并在调用路由之前转换为预期格式。然后,您可以将消息放入队列中(使用 JMX、AMQ Web 控制台 等) ) 调用直接路由。
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...
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.
您不能这样做,因为
direct:
端点代表直接方法调用(即您需要与应用程序位于同一进程中)。为了能够从测试中调用direct:
端点,您需要在测试本身中启动CamelContext
。显然,只有当您需要测试单独的路由或者您的上下文非常小时,这才可用。与已部署的应用程序交互的测试应被视为集成/系统测试。您可以为这些场景编写
JUnit
测试,但您应该通过公开的接口(http:
端点等)与应用程序交互。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 calldirect:
endpoints from tests, you will need to start up theCamelContext
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.).由于您已经将骆驼上下文注入到 test.DispatcherServiceImpl 中,因此您只需要使用骆驼 ProducerTemplate像这样将请求发送到“direct:dispatcherChannel”。
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.