Apache Camel 根据请求使用文件内容丰富消息

编程入门 行业动态 更新时间:2024-10-24 08:20:18
本文介绍了Apache Camel 根据请求使用文件内容丰富消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在实现 RESTful 服务(使用 CXFRS 组件),它应该为某些请求返回文件.每个文件都通过其 id 和扩展名获取,即 restfulservice/path/file/1/pdf.每个文件一旦添加就永远不会改变.提取后不应移动或删除文件,通常它们应该可以同时访问.这是我的 Camel 上下文的一部分:

I'm implementing RESTful service (using CXFRS component) which should return files for some requests. Each file is fetched by its id and extension, i.e. restfulservice/path/file/1/pdf. Each file once added never changes. Files should not be moved or deleted after fetching and generally they should be accessible concurrently. Here is part of my Camel context:

from("direct:fetchFile")
    .process(fetchFileProcessor) // set file.id & file.extension
    .bean(fileService, "fetchFile(${header.file.id}, ${header.file.extension})") // set body to filename
    .setHeader("CamelFileName", simple("${body}"))
    .choice()
        .when(header("file.extension").isEqualTo("xml"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/xml?noop=true", 500)
        .when(header("file.extension").isEqualTo("pdf"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/pdf?noop=true", 500)
    .end()
    .convertBodyTo(File.class)
    .bean(responseProvider, "getResponse(${body}, 200)");

此配置的问题是响应仅针对第二个(为什么?)请求具有非空主体,没有超时设置服务在带有调试消息的第二个请求时进入永恒循环

The problem with this configuration is that response has non-empty body only for second(why?) request, without timeout set service enters on eternal loop on second request with debug message

DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml

Apace Camel 版本为 2.10.4

Apace Camel version is 2.10.4

任何帮助将不胜感激

UPD1:
Content Enricher 页面上有警告,说pollEnrich 不会访问来自当前 Exchange 的任何数据".但是如果我将 fileName=${body} 添加到文件 URL

UPD1:
There is warning on Content Enricher page, saying 'pollEnrich does not access any data from the current Exchange'. But nothing changes if I add fileName=${body} to file URL

UPD2:
似乎 pollEnrich 不支持 URL 中指定的动态 fileName (链接).当前路线:

from("direct:fetchFile")
    .process(fetchFileProcessor) // set file.id & file.extension
    .bean(fileService, "fetchFile(${header.file.id}, ${header.file.extension})") // set body to filename
    .choice()
        .when(header("file.extension").isEqualTo("xml"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/xml?fileName=${body}&noop=true", 500)
            .setHeader("asset.type", simple(MediaType.APPLICATION_XML))
        .when(header("file.extension").isEqualTo("pdf"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/pdf?fileName=${body}&noop=true", 500)
            .setHeader("asset.type", simple("application/pdf"))
    .end()
    .convertBodyTo(File.class)
    .process(multipartProcessor) // add file ass attachment to multipart body and set it as body
    .bean(responseProvider, "getResponse(${body}, 200)");

UPD3
我正在尝试实现自定义处理器以将 PollingConsumer 与动态文件名一起使用:

UPD3
I'm trying to to implement custom processor to use PollingConsumer with dynamic file names:

@Override
public void process(Exchange exchange) throws Exception {
    Long timeout = exchange.getIn().getHeader("file.timeout", Long.class);
    if (enrichUri == null) {
        throw new FileNotFoundException("'file.url' header not set");
    }

    CamelContext context = exchange.getContext();
    Endpoint endpoint = context.getEndpoint(enrichUri);
    PollingConsumer consumer = endpoint.createPollingConsumer();
    consumer.start();

    Exchange consumedExchange;
    try {
        if (timeout == null || timeout < 0) {
            consumedExchange = consumer.receive();
        } else if (timeout == 0) {
            consumedExchange = consumer.receiveNoWait();
        } else {
            consumedExchange = consumer.receive(timeout);
        }
    } catch (Exception e) {
        throw new AssetNotFoundException(e);
    } finally {
        consumer.stop();
    }
    exchange.getIn().setBody(consumedExchange.getIn().getBody());
}

现在它在第一次响应时返回文件内容,但是在每个成功的请求中,我得到了上述日志消息的永恒循环:

Now it returns file contents on first response, but on each succeeding request I got eternal loop of above log messages:

DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml

UPD4
我已经实现了在处理之前添加并在处理之后删除的动态路由.this 在 Apache Camel 论坛中发帖.路由使用上面的处理器来消费文件.结果是一样的

UPD4
I've implemented dynamic route which is added before processing and removed after it. This method is described in this post in Apache Camel forum. Route uses above processor to consume file. The result is the same

推荐答案

简单的方法往往是最好的方法.在这种情况下,我拒绝处理 Apache Camel 文件组件并实现了以下处理器:

Simple way often is the best way. I refuse to deal with Apache Camel file component in this case and implemented following processor:

public class FileLoadingProcessor implements Processor {

@Override
public void process(Exchange exchange) throws Exception {
    String filename = exchange.getIn().getBody(String.class); // message body contains filename
    String filePath = exchange.getIn().getHeader("fileprocessor.filepath", String.class);

    if (filePath == null || filename == null) {
        // throw some custom exception
    }

    URI uri = new URI(filePath.concat(filename));
    File file = new File(uri);

    if (!file.exists()) {
        throw new FileNotFoundException(String.format("File %s not found on %s", filename, filePath));
    }

    exchange.getIn().setBody(file);
}

现在它就像一个魅力

这篇关于Apache Camel 根据请求使用文件内容丰富消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-19 22:37:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/971460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容丰富   消息   文件   Apache   Camel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!