当前位置: 首页 > 知识库问答 >
问题:

使用Springbeans配置Camelendpoint的长URI替代方案?

武晨
2023-03-14

我试图找到一种方法来配置骆驼endpoint,使用从骆驼上下文中路由中的endpoint声明中引用的Spring bean,但它不起作用。

例如,有时用许多参数定义endpointURI是非常可怕的(!!),用bean及其属性配置endpoint会容易得多。(或者更好的是,在XML中配置endpoint时,或者元素应该有像常规bean这样的子元素,我们可以在其中配置endpoint的参数)。

下面的第一种方法运行良好,非常标准且非常简单。第二种方法,是我想使用的方法,但它不起作用。我尝试了许多变体,但没有成功!下面的第三种选择实际上对Camel开发人员来说只是一个有趣的建议,但它也说明了我的观点。

在我下面的例子中,我只为文件endpoint配置了3个参数,但想象一下URI有10个参数!!我的问题是如何让我的第二种方法正常工作,我相信有一个简单的解决方案!?我也尝试过使用工厂bean和工厂方法,但它也不起作用。

1)在XML(春豆)中配置骆驼endpoint的标准方法:

...
<camel:camelContext id="camelContext"  >

    <camel:route id="deviceDataLogsPoller"  >
        <camel:from uri="file://long/path/to/input?preMove=../inprogress&amp;move=../done&amp;moveFailed=../error" />

        <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />

    </camel:route>
</camel:camelContext>

2) 我希望是valide的替代品,但这不起作用(对我来说!):

<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint" >

    <property name="camelContext" ref="camelContext" />
    <property name="localWorkDirectory" value="/long/path/to/input" />
    <property name="preMove"   value="../inprogress" />
    <property name="move"       value="../done" />
    <property name="moveFailed" value="../error" />
    ...
</bean>

...
<camel:camelContext id="camelContext"  >

    <camel:route id="deviceDataLogsPoller"  >
        <camel:from ref="filePoller" />

        <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />

    </camel:route>
</camel:camelContext>

3)未来可能感兴趣的替代方案(混合上述两种替代方案):

...

    <camel:route id="deviceDataLogsPoller"  >
        <camel:from uri="file://long/path/to/input" >
             <property name="preMove"   value="../inprogress" />
             <property name="move"       value="../done" />
             <property name="moveFailed" value="../error" />
             ...
        </camel:from>

        <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />

    </camel:route>
</camel:camelContext>

共有2个答案

瞿和硕
2023-03-14

正如我在上一篇评论中所说的,我能够为一个endpoint进行bean配置(参见上面的评论),但这种方法最终要比简单地使用uri复杂得多!!

如果能像我在上面的第三个备选方案中提出的那样配置endpoint,那会更有趣。也许如果我有时间,我会尝试创建自己的和标记,通过从params元素构造完整的URI来包装现有的和标记。。。!我也可以向Camel开发者提出这个建议。

请参阅下面的一个例子,说明将来配置endpoint(或使用我想编写的XML包装器)可能会有什么好处:

<camel:route id="deviceDataLogsPoller">

    <camel:from uri="file://long/path/to/input" >

         <param name="preMove"   value="../inprogress" />
         <param name="move"       value="../done" />
         <param name="moveFailed" value="../error" />
         ...
    </camel:from>

    ...

</camel:route>

不幸的是,如上所示的endpoint配置目前还不可能实现,但我认为这将是一个很好的选择!目前,唯一的方法是要么将所有参数指定为一个很长的URI中的参数,要么将endpoint配置为一个常规bean,并具有它所暗示的所有复杂性(有关详细信息,请参见上面的注释)。

华宣
2023-03-14

到底是什么不适合你?

以下设置完成了预期的工作:

<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint">
    <property name="file" value="src/data/bean-ref" />
    <property name="move" ref="moveExpression"/>
</bean>

<bean id="moveExpression" class="org.apache.camel.model.language.SimpleExpression">
    <constructor-arg value="${file:parent}/.done/${file:onlyname}" />
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring" id="camelContext">
    <route>
        <from ref="filePoller" />
        <log message="${body}" />
    </route>
</camelContext> 

注:

  • 属性文件是强制性的
  • 属性mobilemoveFailpreMobile不是java.lang.String类型,而是类型org.apache.camel.表达式并且必须相应地初始化。
  • 属性moveExpression需要一个完整的文件表达式。如果仅使用. do而不是${file:家长}/. do/${file: onlyname},则文件将重命名为. do,而不是移动到名为. do的目录。
 类似资料:
  • 如何用配置文件myprops中的文本替换uri。cfg? myprops.cfg: 我的尝试: 然后骆驼按原样读取uri,它不会用属性的值替换它。 另一个尝试: 错误: 组织。osgi。服务蓝图容器ComponentDefinitionException:无法验证xml组织。xml。萨克斯。SAXParseException:cvc复杂类型。2.4.a: 发现以元素“{”开头的内容无效http:/

  • 在缺少<code>web的情况下。xml,欢迎文件配置在哪里? 背景: 10.10欢迎文件 Web应用程序开发人员可以在Web应用程序部署描述符中定义称为欢迎文件的部分URI的有序列表。 JavaServlet规范版本3.0修订版2010年12月 我正在使用 Netbeans 8,但没有意识到 职责现在主要通过注释完成。但是,我似乎找不到与欢迎文件相关的注释。目前,加载,我想将其重新配置为。这可以

  • 我正在寻找在.NET Core中混合使用环境无关、环境特定和秘密配置的选项模式的简单方法。我看到了不必要的复杂的方法,并开发了一种方法,它感觉比我所看到的要简单得多,但仍然需要进一步简化。我会在这个问题的第一个答案中提供这一点。 因此,明确地提出一个问题:将选项对象绑定到.NET Core中的配置节的最简单方法是什么,它在所有类型的.NET Core项目(例如函数应用程序、辅助服务、ASP.NET

  • 我正在开发一些libs供公司内部使用,它使用spring和spring-Boot。 我的bean定义遇到了问题。我想创建一些不属于我的库的对象bean,例如: 然而,每当我这样做时,它都会影响使用我的库并为它们创建bean的服务,这意味着它们将被迫使用我的Gson或通过定义spring来启用bean重写。主要的允许bean定义覆盖=true,这感觉是错误的。 在库中创建这种bean是一种好的做法吗

  • 我试图使用vscode-js-debug,这是在VSCode。 现在我和你共进午餐 然后我用 但这是一个两步的过程。我想在发布中为每个国家创建一个条目。json文件,所以每次我选择它并在“运行和调试”部分运行它时,应用程序都会启动,并附加调试器。那只是一步。 我已经尝试过如何让vscode执行与ng serve相同的构建?但我没能成功。 我想要的可能吗?

  • 在外壳脚本中,这很麻烦: 它看起来也很容易出错。 难道没有更好的方法吗? (顺便说一句,答案出现在一个部分相关问题的评论中。但是,据我所知,该问题和答案尚未作为正确的问答出现在StackOverflow上。)