从 JUnit 测试用例调用部署在远程服务器中的 Camel 端点

发布于 2024-12-06 10:14:02 字数 1213 浏览 1 评论 0 原文

我是 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&gt;serviceEndpoints&lt;/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 配置。

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>serviceEndpoints</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:dispatcherChannel deployed in a tomcat server (http://someIP:8080) from a jUnit which uses Spring configuration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眸中客 2024-12-13 10:14:02

“直接”端点只能从同一虚拟机内访问。如果您需要从外部访问路由,可以使用 JMX 或用另一个包来实现使用 JMSHTTP。这两种方法都允许您手动测试/调试已部署的路由...

  • 使用 JMX ,您只需要导航到 Camel Context MBean(使用 jconsole 等)并

  • 用 HTTP 封装路由,只需添加此路由,然后在浏览器中转到此 URL 即可调用该路由...

    from("jetty:http://0.0.0.0:9001/invokeDispatcherChannel")
    .to("直接:dispatcherChannel");
    
  • 如果您需要发送有效负载,您可能会考虑公开通过 JMS(或 WS 等)获取路由,并在调用路由之前转换为预期格式。然后,您可以将消息放入队列中(使用 JMX、AMQ Web 控制台 等) ) 调用直接路由。

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

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");
    
请帮我爱他 2024-12-13 10:14:02

您不能这样做,因为 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 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.).

別甾虛僞 2024-12-13 10:14:02

由于您已经将骆驼上下文注入到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文