Apache Camel将电子邮件附件上传到ftp

时间:2017-05-15 10:43:24

标签: apache-camel

我正在使用以下处理器来获取电子邮件的第一个附件并将其上传到ftp服务器。

路线配置

<from uri="imaps://...
<to uri="ejb:java:global/Dms/MailProcessor"/>
<to uri="ftp://....

MailProcessor

@Named("MailProcessor")
@Stateless
public class MailProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {

        exchange.getOut().setHeaders(exchange.getIn().getHeaders());

        Map<String, DataHandler> attachments = exchange.getIn().getAttachments();
        if (attachments.size() > 0) {
            for (String name : attachments.keySet()) {
                DataHandler dataHandler = attachments.get(name);

                // SET ATTACHMENT FILENAME TO OUTPUT FILENAME HEADER
                String filename = dataHandler.getName();
                filename = MimeUtility.decodeText(filename);
                exchange.getOut().setHeader("filename", filename);

                // SET INPUT ATTACHMENT TO OUTPUT BODY
                byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, dataHandler.getInputStream());
                exchange.getOut().setBody(data);

                // SET ONLY THE FIRST ATTACHMENT
                break;
            }
        }else{
            exchange.getOut().setBody(exchange.getIn().getBody());
    }

}

这一般起作用,但对于“大”附件,它实际上是永远的。 (只需5 mb附件,半小时)

日志

TRACE [org.apache.camel.component.file.remote.FtpOperations] (Camel (example) thread #119 - imaps://mail.example.com) Changing directory: upload
TRACE [org.apache.camel.component.file.remote.FtpOperations] (Camel (example) thread #119 - imaps://mail.example.com) doStoreFile(ID-example-local-59752-1494841993139-21-11)
DEBUG [org.apache.camel.component.file.remote.FtpOperations] (Camel (example) thread #119 - imaps://mail.example.com) About to store file: ID-example-local-59752-1494841993139-21-11 using stream: com.sun.mail.util.BASE64DecoderStream@601a11dc
TRACE [org.apache.camel.component.file.remote.FtpOperations] (Camel (example) thread #119 - imaps://mail.example.com) Client storeFile: ID-example-local-59752-1494841993139-21-11
 -- long pause --

我也试过像这样转换附件:

byte[] data = exchange.getContext().getTypeConverter().convertTo( byte[].class, dataHandler.getInputStream() );
exchange.getOut().setBody(data);

但这给了我以下内容:

2017-05-15 11:23:20,968 TRACE [org.apache.camel.impl.converter.DefaultTypeConverter] (Camel (example) thread #133 - imaps://mail.example.com) Converting org.apache.camel.Processor$$$view63 -> org.apache.camel.Processor with value: Proxy for view class: org.apache.camel.Processor of EJB: MailProcessor
2017-05-15 11:23:20,968 TRACE [org.apache.camel.component.bean.BeanProcessor] (Camel (example) thread #133 - imaps://mail.example.com) Using a custom adapter as bean invocation: Proxy for view class: org.apache.camel.Processor of EJB: MailProcessor
2017-05-15 11:23:20,990 TRACE [org.apache.camel.impl.converter.DefaultTypeConverter] (Camel (example) thread #133 - imaps://mail.example.com) Converting com.sun.mail.util.BASE64DecoderStream -> byte[] with value: com.sun.mail.util.BASE64DecoderStream@28442828
2017-05-15 11:23:20,990 TRACE [org.apache.camel.impl.converter.DefaultTypeConverter] (Camel (example) thread #133 - imaps://mail.example.com) Using converter: StaticMethodTypeConverter: public static byte[] org.apache.camel.converter.IOConverter.toBytes(java.io.InputStream) throws java.io.IOException to convert [class com.sun.mail.util.BASE64DecoderStream=>class [B]
2017-05-15 11:23:20,990 TRACE [org.apache.camel.util.IOHelper] (Camel (example) thread #133 - imaps://mail.example.com) Copying InputStream: java.io.BufferedInputStream@24b2cffb -> OutputStream:  with buffer: 4096 and flush on each write false
-- long pause --

使用Filezilla上传到ftp服务器就像魅力一样。此外,当我在另一条路线中使用camel-ftp(由文件上传触发)时,上传到ftp服务器的速度非常快。 所以我感觉这是附件的转换,这会降低速度。

问题:

我的假设是否正确,我怎样才能加快速度?

1 个答案:

答案 0 :(得分:1)

解决方案是使用<from uri="imaps://...&mail.imaps.partialfetch=false

https://community.oracle.com/thread/2604843?start=0&tstart=0