【问题标题】:Using apache camel xml route, how can I set a customized fileName from a JMS message?使用 apache camel xml 路由,如何从 JMS 消息中设置自定义文件名?
【发布时间】:2012-09-14 20:33:53
【问题描述】:

使用 java JMS API,我从 DB 中获取了一个字节数组,然后将其作为 javax.jms.BytesMessage 发送到 ActiveMQ。之后用骆驼我想把文件放在一个位置,

我有这条骆驼路线:

    <route>
        <from uri="activemq:queue.fileOuput"/>
        <convertBodyTo type="java.nio.ByteBuffer"/>
        <to uri="file://C:/output/"/>
    </route>

但我的问题是我在c:\output\目录下的文件,我得到的文件以message id为文件名,比如 queue-preVerificacion-fileoutput-ID-jmachine-57401-1347652410053-0-1-1-1-1 但我想把我的名字放在数据库里,比如MyFile.xml

我尝试设置一个消息属性,如 fileName 和 file:name,而且我在 apache 文档中看到我需要放置一个标题“org.apache.camel.file.name”,但我没有使用 jms不知道怎么做。

所以我的问题是如何在骆驼路线中放置自定义名称?

谢谢大家。

【问题讨论】:

    标签: jms activemq apache-camel


    【解决方案1】:

    只需将文件名放在 jms 消息中(作为字符串属性)。

    // Something like this if you send the message using plain java/jms:
    msg.setStringProperty("filename","MyFile.xml");
    ..//Send msg
    

    那么你可以在骆驼中做这样的事情

    <to uri="file://C:/output/?fileName=${header.filename}"/>
    

    【讨论】:

    • 这将不起作用,因为在构建路由时会评估 to uri。上面带有 CamelFileName 标头的解决方案应该可以工作。
    • @ChristianSchneider:它就像一个魅力。文件名支持“文件表达式语言”=简单语言在运行时进行解释,而不是路由构建时间。刚刚测试了这条路线:&lt;route&gt; &lt;from uri="activemq:queue:foo.in"/&gt; &lt;to uri="file://C:/output/?fileName=${header.filename}"/&gt; &lt;/route&gt; 使用两条不同的消息(其中字符串属性 filename=MyFile.txt 和 filename=YourFile.txt),它们在 c:\output\MyFile.txt 和 c:\output 中按预期创建\YourFile.txt)
    【解决方案2】:

    您只需要设置“CamelFileName”标头值(基于消息标头等)

    <route>
        <from uri="activemq:queue.fileOuput"/>
        <convertBodyTo type="java.nio.ByteBuffer"/>
        <setHeader headerName="CamelFileName">
            <constant>${header.fileName}</constant>
        </setHeader>
        <to uri="file://C:/output/"/>
    </route>
    

    【讨论】:

      【解决方案3】:

      我认为“org.apache.camel.file.name”适用于骆驼 1.x,在 2.x 版本中 CamelFileName 工作正常。但我想要一个更动态的文件名,根据内容命名。这个使用处理器的例子运行良好(camel 2.18)

       <route>
          <from uri="MQ:MY_Q_NAME" />
          <process ref="MyMessageProcessor"/>
          <to uri="file://E:\OUTPUT" />
      </route>
      
          Inside the Processor :     
      
      exchange.getIn().setHeader(Exchange.FILE_NAME, myFileName);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 2019-10-08
        • 2023-04-01
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多