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

从文件名表达式调用Apache Camel Spring xml-bean方法

顾光明
2023-03-14

我有以下处理文件(输入)的Apache Camel Spring XML配置。我尝试在复制文件之前重命名它(移动选项)。我希望文件名包含一个字符串,这是返回字符串(getHash)的bean的方法调用的结果。

Apache Camel版本

<dependency>
    <groupId>org.apache.camel</groupId>
    <version>3.0.0</version>
    <type>pom</type>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camelContext-89c763e9" xmlns="http://camel.apache.org/schema/spring">

    <route id="FileConsumption" shutdownRoute="Defer">
        <from id="_from2" uri="file:/home/spool_in/?move=.done&amp;moveFailed=.bad&amp;fileName={bean:videoProcessor.getHash}.{file:name.ext}"/>
        <bean
            beanType="org.mediaprocessor.VideoProcessor" id="_videoProcessor" ref="videoProcessor"/>
    </route>
</camelContext>

<bean id="videoProcessor" class="org.mediaprocessor.VideoProcessor" />
</beans>
<from id="_from2" uri="file:/home/matthieu/spool_in/?move=.done&amp;moveFailed=.bad&amp;fileName=${bean:videoProcessor.getHash}.{file:name.ext}"/>
@Bean
public static String getHash(File file) throws NoSuchAlgorithmException, IOException {
    //Use MD5 algorithm
    MessageDigest md5Digest = MessageDigest.getInstance("MD5");

    //Get the checksum
    String checksum = getFileChecksum(md5Digest, file);
    return checksum;
}
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route FileConsumption at: >>> Bean[] <<< in route: Route(FileConsumption)[From[file:/home/spool_in/?mo... because of bean, ref or beanType must be provided
...
Caused by: java.lang.IllegalArgumentException: bean, ref or beanType must be provided
    at org.apache.camel.component.bean.DefaultBeanProcessorFactory.createBeanProcessor(DefaultBeanProcessorFactory.java:67)
    at org.apache.camel.reifier.BeanReifier.createProcessor(BeanReifier.java:47)
    at org.apache.camel.reifier.ProcessorReifier.makeProcessorImpl(ProcessorReifier.java:571)
    at org.apache.camel.reifier.ProcessorReifier.makeProcessor(ProcessorReifier.java:537)
    at org.apache.camel.reifier.ProcessorReifier.addRoutes(ProcessorReifier.java:250)
    at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:384)
    ... 37 more

最后,我们还可以使用bean表达式调用POJO类,该类生成一些要使用的字符串输出(或可转换为字符串):

filename=“uniquefile-${bean:MyGuidGenerator.GenerateID}.txt”

https://camel.apache.org/manual/latest/file-language.html

对我遗漏了什么有什么想法吗?谢了!

更新:在“${bean:videoprocessor.gethash}”中添加了“$”符号(错别字)

共有1个答案

劳亦
2023-03-14

我不熟悉Camel XML,因为我发现Camel DSL更容易阅读和使用。但是,在来自camel的文档中,您提供了${bean:myguidgenerator.generateid},而您在路由中没有使用“$”符号:filename={bean:videoprocessor.gethash}

您还需要在文件扩展名中添加一个“$”登录。${file:name.ext}

我还复制了您的项目,在做了这些“$”更改后,应用程序在我这边启动。然而,我相信您的实现有更多的问题,即使您启动了它,它也不会达到您期望的效果。

以下是我认为你的问题:

  1. filename中使用该beangethash将设置act作为您要使用的文件名的筛选器。从您所说的来看,这不是您想要的。
  2. 如果我的理解是正确的,您希望从spool_in文件夹中提取一个视频文件,用视频处理器处理它,一旦处理完毕,将它移到.done文件夹中,同时更改名称。为此,您需要在移动选项下移动文件表达式,如下所示:

uri=“file://home/matthieu/spool_in/?move=.done/${bean:videoprocessor.gethash}.${file:name.ext}

@Component
@Slf4j
public class VideoProcessor {
    public void process(Exchange exchange) {
        log.info("Processing {} video ", ((GenericFile) exchange.getIn().getBody()).getFileName());
    }

    public String getHash(File file) {
        return FilenameUtils.removeExtension(file.getName()) + "_copy";
    }
}
2020-01-03 11:19:50.444  INFO 27988 --- [/temp/spool_in/] c.v.t.g.webservice.camel.VideoProcessor  : Processing Video_1.mpg video 
2020-01-03 11:20:41.284  INFO 27988 --- [/temp/spool_in/] c.v.t.g.webservice.camel.VideoProcessor  : Processing Video_2.mpg video 
2020-01-03 11:20:53.342  INFO 27988 --- [/temp/spool_in/] c.v.t.g.webservice.camel.VideoProcessor  : Processing Video_3.mpg video 

以及移动的文件:

 类似资料:
  • 我在JPanel上有一个文本字段,这个文本字段上有一个文档侦听器。由于文档侦听器包含三个方法(removeUpdate,insertUpdate,changeUpdate),所以如何使用Lambda表达式调用特定方法。 如何使用像field.getDocument()这样的Lambda表达式调用此文档侦听器insertUpdate方法。AddDocumentListener(Lambda表达式);

  • 问题内容: 我有一个简单的Spring Bean表达式,当我在应用程序上下文文件中定义它时,它的计算效果很好: 现在,我想以编程方式进行相同的评估。我使用了以下代码: 抛出一个异常: 我想我必须以某种方式设置一个根对象,该根对象允许像属性一样配置的bean。但是我还没有开始工作。任何人都已经这样做并且可以提供提示了吗? 问题答案: 实现BeanFactoryAware以获得对bean工厂的引用;然

  • 上一讲,主要介绍了用%表达的一种输出格式化表达式。在那一讲最后又拓展了一点东西,拓展的那点,名曰:格式化方法。因为它知识上是使用了str的__format__方法。 现在我们就格式化方法做一个详细一点的交代。 基本的操作 所谓格式化方法,就是可以先建立一个输出字符串的模板,然后用format来填充模板的内容。 >>> #先做一个字符串模板 >>> template = "My name is {0

  • 问题内容: 在学习过程中,我遇到以下错误: 在我的测试程序(https://play.golang.org/p/PW9SF4c9q8)中: 有人愿意帮助我继续前进吗? 问题答案: 不幸的是,您的错误有些令人误解。问题在于它是一个实例方法,您在调用它时就好像它是程序包作用域中的一个方法一样。 您需要这样的东西; 我猜该错误的措辞是这样的,因为接收器(函数名称左侧位置中的括号中的内容)的处理方式与在后

  • 本文向大家介绍Java8使用lambda表达式调用静态方法,包括了Java8使用lambda表达式调用静态方法的使用技巧和注意事项,需要的朋友参考一下 Java中的Lambda表达式允许您将功能作为参数传递给方法。您还可以使用lambda表达式调用现有的方法。 方法引用是简单的、易于阅读的lambda表达式,可以通过lambda表达式中的名称调用/引用现有的方法。可以使用方法引用引用类中定义的静态

  • 问题内容: 我是打字稿的新手,并且有两节课。在父类中,我有: 在儿童班,我有: showMore和ShowLess都给我一个错误,“无法调用类型缺少调用签名的表达式”。 但是,我认为setProp返回的函数确实具有呼叫签名吗?我认为我误解了有关函数类型的一些重要信息,但我不知道它是什么。 谢谢! 问题答案: 它返回的函数具有一个调用签名,但是您告诉Typescript通过添加其签名来完全忽略它。