简体   繁体   English

Apache Camel如何从路由动态添加/删除端点

[英]Apache Camel how to add/remove endpoints dynamically from a route

I am trying to get familiar with EIP and Apache Camel and I have a use case that I am not quite sure how can be expressed or implemented using Camel 我试图熟悉EIP和Apache Camel,但我有一个用例,我不确定如何使用Camel来表达或实现。

Use case: 用例:

Imagine you have designed an integration solution that takes files from an ftp, does some processing and uploads it to a queue. 想象一下,您已经设计了一个集成解决方案,该解决方案可以从ftp获取文件,进行一些处理并将其上传到队列中。 You chose Apache Camel to implement this solution and your route in Java DSL looks something like this: 您选择了Apache Camel来实现此解决方案,并且您在Java DSL中的路由如下所示:

 from("ftp://user@hostname/directoryname")
   .process(new Processor() {
              public void process(Exchange exchange) throws Exception
              {
                //my fantastic prosessing goes here
              }
 }).to("jms:queue:queueName");

the route could be way more complex than this, but it doesn't matter here. 路线可能比这复杂得多,但这并不重要。 Imagine your solution is such a spectacular success that there's a plan to implement a service where anyone could add his ftp server and get the files processed and uploaded to the queue. 想象一下,您的解决方案取得了巨大的成功,以至于计划实施一项服务,任何人都可以添加他的ftp服务器并处理文件并将其上传到队列中。 So what you want is 所以你想要的是

  1. (Flexibility) Being able to dynamically add/remove servers from your app (灵活性)能够从您的应用程序动态添加/删除服务器
  2. (Scaling) Being able to handle potentially large number of such servers (扩展)能够处理大量此类服务器

Let's forget about #2 and focus on Flexibility part. 让我们忘记#2并关注“灵活性”部分。

So the question is, I suppose: 所以问题是,我想:

How to dynamically (at runtime) add/remove endpoints to/from Apache Camel route? 如何动态(在运行时)向Apache Camel路由添加端点或从中删除端点?

What I considered so far: 到目前为止,我认为:

First, I admit I am not that familiar with Integration Patterns, but just scanning the catalogue, the only thing that kind of could fit the bill is the Content Enricher . 首先,我承认我对集成模式不是很熟悉,但是只是扫描目录,唯一可以满足要求的就是Content Enricher It can take a message and just go off to somewhere else and bring sth else. 它可以传递消息,然后转到其他地方然后带走其他东西。 So I was thinking if someone adds an ftp server, the connection details could be encapsulated in the message and a Content Enricher could then connect to that ftp server and fetch files and push it further through the route.... so it would effectively be a Content Enricher capable of connecting to multiple ftp servers.... That kind of sound wrong. 所以我在想,如果有人添加ftp服务器,则连接详细信息可以封装在消息中,然后Content Enricher可以连接到该ftp服务器并获取文件并通过路由将其进一步推送...。一个能够连接到多个ftp服务器的Content Enricher First I don't think this is the intention behind that pattern and secondly since there's ftp Component in Camel, I should somehow be able to use it in that scenario 首先,我不认为这是该模式背后的意图,其次,由于Camel中有ftp 组件 ,因此我应该可以在这种情况下使用它

The second approach would be to break the route into two like using the vm component, like this: 第二种方法是像使用vm组件那样将路由分成两部分,如下所示:

 from("ftp://user@hostname/directoryname").to("vm:internalQ");
 from("vm:internalQ")
   .process(new Processor() {
              public void process(Exchange exchange) throws Exception
              {
                //my fantastic prosessing goes here
              }
 }).to("jms:queue:queueName");

so now, I can create many routes with ftp endpoints that write to that internal queue so it can be picked up. 因此,现在,我可以使用写入到该内部队列的ftp端点创建许多路由,以便可以将其拾取。 Adding route dynamically to CamelContext seems to be possible ( Add camel route at runtime in Java ). 动态地将路由添加到CamelContext似乎是可行的( 在Java运行时添加骆驼路由 )。 Is that the way to go? 那是路要走吗? Or am I just trying to use Camel in a way that it was not designed to? 还是我只是想以一种并非旨在使用的方式使用Camel

You can dynamically add routes to your CamelContext : 您可以动态地将路由添加到CamelContext

MyRouteBuilder trb = new MyRouteBuilder(servletEndpoint, mockEndpoint);
camelContext.addRoutes(trb);

And MyRouteBuilder: 和MyRouteBuilder:

MyRouteBuilder(Endpoint servletEndpointStart, MockEndpoint mockEndpointEnd, String allowedParameters){
        this._servletEndpoint = servletEndpointStart;
        this._mockEndpoint = mockEndpointEnd;
    }

    @Override
    public void configure() throws Exception {
        from(this._servletEndpoint)
        .id(TESTING_ROUTE_NAME)
        .process(new Processor(){ // some processor })
        .to(_mockEndpoint);
    }

You can also modify the route, but you will need to restart it, in order to work properly, checkout how it is done in: org.apache.camel.model.RouteDefinition.adviceWith(ModelCamelContext, RouteBuilder) 您也可以修改路由,但是为了使它正常运行,您需要重新启动它,并在以下位置检查其完成方式: org.apache.camel.model.RouteDefinition.adviceWith(ModelCamelContext, RouteBuilder)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM