java - 在 pollEnrich 结束策略之前删除文件

标签 java apache-camel

如果我在路由处理交换之前删除文件,我会得到异常: GenericFileOperationFailedException:无法重命名文件。 PollEnrich 的策略尝试将文件从 someFolder 移动到 someFolder/.camel。

from("wmq:queue:someQueue")//Here we get the message with information about the file
...//some logic
.pollEnrich("file:someFolder?fileName=someFile")
...//some logic
.choice()
    .when(...)//Here we compare checksum from message with checksum of the file
    .process(new SomeClassProcess)//And if they are different file will be deleted from someFolder
    .otherwise()
    .to(someAnotherFolder)
.end();    

我正在尝试使用 .rollback("errorMessage")

onException(RollbackExchangeException.class)
.log(LoggingLevel.WARN, "SomeExcptn");

from("wmq:queue:someQueue")
    ...//some logic
    .pollEnrich("file:someFolder?fileName=someFile")
    ...//some logic
    .choice()
        .when(...)
        .process(new SomeClassProcess)//it will delete someFile from someFolder
        .rollback()
        .otherwise()
        .to(someAnotherFolder)
    .end();

但是现在我在应用程序日志中发现了垃圾 - CamelExecutionException。 它可以工作,但是你能用另一种方式帮助解决它吗?

附注我不知道在向 pollEnrich 提供 URI 之前是否需要删除文件,这就是我不使用 noop=true 的原因。在 .otherwise() 中,我需要将文件移动到 .camel。

感谢您的建议!

最佳答案

我认为您不需要使用任何处理器来删除文件。你可以这样做:

@Produce(uri = "direct:start")
ProducerTemplate producerTemplate;

@Autowired
CamelContext camelContext;


@Before
public void before() {
    File outputDir = new File("transfer/outbox");
    File tmpDir = new File("transfer/tmp");
    for (File file : outputDir.listFiles())
        file.delete();
    for (File file : tmpDir.listFiles())
        file.delete();
}

@Override
public RouteBuilder createRouteBuilder() {

    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("file:transfer/inbox?noop=true")
                .to("file:transfer/tmp")
                .pollEnrich("file:transfer/tmp")
                .choice()
                  .when(header(Exchange.FILE_NAME).isEqualTo("Message1.txt"))
                    .log(LoggingLevel.ERROR, "${header[CamelFileName]} This file is discarded")
                  .otherwise()
                    .to("file:transfer/outbox")
                .end();
        }
    };
}

@Test
public void smokeTest() throws Exception {

    NotifyBuilder notifyBuilder = new NotifyBuilder(context)
        .wereSentTo("file:transfer/outbox").whenDone(1)//Just to make the test enough time to complete
        .create();

    notifyBuilder.matches(5, TimeUnit.SECONDS);

    File inputDur = new File("transfer/inbox/");
    assertEquals(inputDur.listFiles().length, 2); // Message1.txt, Message2.txt (no .camel because ?noop=true)

    File outputDir = new File("transfer/outbox/");
    assertEquals(outputDir.listFiles().length, 1); // Message2.txt

    File tmpDir = new File("transfer/tmp/");
    assertEquals(tmpDir.listFiles().length, 1); // .camel


}

或者如果您不关心自己记录它,更合适的方法是使用消息过滤器而不是基于内容的路由器:

from("file:/path/to/your/file")
  .filter(somePredicate)
  .to("file:/where/you/want/it/to/move")

在camel在飞行中的交换中使用文件时删除文件不是什么事情,camel确实用另一个后缀为.camelLock的文件锁定该文件,以防止其他路线消耗他,但用处理器修改这个文件根本不安全。

此外,我在您的示例中没有看到任何 ?noop=true 。如果您想删除或移动文件,则使用 ?noop 不相关。

关于java - 在 pollEnrich 结束策略之前删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36068307/

相关文章:

xml - 使用 Camel 加载多个属性文件

java - 使用没有端点的处理器

java - Android GC使用什么权限

Java - 如何将我的 ArrayList 写入文件,并将该文件读取(加载)到原始 ArrayList?

java - 开源许可证,哪一个?

java - HSSFCell 读取空错误?

java - 我需要一种方法来操作 JTextArea 中的最后一个字符(通过删除它)

java - 如何将交换 ID 和原始路由 ID 传递给 bean?

properties - Camel Exchange 属性不使用 xml 中的简单进行评估

spring - Apache Camel 和 Web 服务