2013-04-11 44 views
6

Tôi đang triển khai dịch vụ RESTful (sử dụng thành phần CXFRS) sẽ trả về các tệp cho một số yêu cầu. Mỗi tệp được tìm nạp theo id và đuôi của nó, tức là restfulservice.com/path/file/1/pdf. Mỗi tập tin một lần thêm không bao giờ thay đổi. Không nên di chuyển hoặc xóa các tệp sau khi tìm nạp và thường chúng có thể truy cập đồng thời. Dưới đây là một phần của bối cảnh Camel tôi:Thông tin phong phú về Apache Camel với nội dung tệp theo yêu cầu

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)"); 

Vấn đề với cấu hình này là phản ứng có thân hình không có sản phẩm nào chỉ cho yêu cầu thứ hai (tại sao?), Không có dịch vụ thời gian chờ bộ vào vào vòng lặp vĩnh cửu theo yêu cầu thứ hai với debug nhắn

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

Apace Camel phiên bản 2.10.4

Bất kỳ trợ giúp sẽ được đánh giá

UPD1:
Có cảnh báo trên trang Content Enricher, nói 'pollEnrich không truy cập bất kỳ dữ liệu nào từ Exchange hiện tại'. Nhưng không có gì thay đổi nếu tôi thêm fileName=${body} nộp URL

UPD2:
Nó có vẻ như pollEnrich không hỗ trợ năng động fileName chỉ định trong URL (link). Đường bay tại thời điểm hiện tại:

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
Tôi đang cố gắng để thực hiện xử lý tùy chỉnh để sử dụng PollingConsumer với tên tập tin năng động:

@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()); 
} 

Bây giờ nó sẽ trả về nội dung tập tin trên phản ứng đầu tiên, nhưng trên mỗi yêu cầu thành công, tôi nhận được vòng lặp vĩnh cửu của các thông điệp tường trình ở trên:

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

UPD4
Tôi đã triển khai tuyến đường động được thêm trước khi xử lý và xóa sau đó. Phương pháp này được mô tả trong this bài đăng trong diễn đàn Apache Camel. Tuyến sử dụng bộ xử lý phía trên để tiêu thụ tệp. Kết quả là giống nhau

Trả lời

9

Cách đơn giản thường là cách tốt nhất. Tôi từ chối để đối phó với Apache Camel thành phần hồ sơ trong trường hợp này và thực hiện sau xử lý:

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); 
} 

Bây giờ nó làm việc như một nét duyên dáng

+0

Bạn có thể hiển thị những gì DSL trông giống như sử dụng bộ xử lý tuỳ chỉnh của bạn? Có cách nào để vượt qua một bộ xử lý tùy chỉnh để pollEnrich? – pimlottc