使用VM端点处理文件

时间:2014-11-14 20:23:26

标签: mule

我正在尝试以块的形式读取大文件,创建分块数据列表,然后将其发送到VM进行进一步处理。例如,我在一个文件中有9封电子邮件,我希望一次处理3封,并将3封电子邮件列表放在VM上(每封都作为消息有效负载):

  

[email01 @ gmail.com,email02 @ gmail.com,email03 @ gmail.com]
  [email04@gmail.com,mail05@gmail.com,mail06@gmail.com]
  [email07@gmail.com,mail08@gmail.com,mail09@gmail.com]

<flow name="flow1" doc:name="flow1">
    <file:inbound-endpoint path="emailList" moveToDirectory="processedEmails" pollingFrequency="60000" doc:name="EmailList" connector-ref="File">
        <file:filename-wildcard-filter pattern="*.txt" caseSensitive="true" />
    </file:inbound-endpoint>

    <component class="org.sample.component.EmailAddressFileReader" doc:name="chunk email addresses and send to VM" />
</flow>

<flow name="process" doc:name="process">
    <vm:inbound-endpoint exchange-pattern="one-way" path="in" doc:name="VM"/>
    <logger message="payload is: #[message.payload]" level="INFO" doc:name="Logger"/>   
</flow>

我的EmailAddressFileReader.java组件:

public class EmailAddressFileReader implements Callable{

    private static final Logger logger = LoggerFactory.getLogger(EmailAddressFileReader.class);

    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {

        InputStream fileStream = (InputStream) eventContext.getMessage().getPayload();
        DataInputStream ds = new DataInputStream(fileStream);
        BufferedReader br = new BufferedReader(new InputStreamReader(ds));

        MuleClient muleClient = eventContext.getMuleContext().getClient();

        String emailAddress;
        List<String> emailAddressList = new ArrayList<String>();

        while ((emailAddress = br.readLine()) != null) {
            emailAddressList.add(emailAddress);
            if(emailAddressList.size() > 2) {
                muleClient.dispatch("vm://in", emailAddressList, null);
                logger.info("after dispatch to vm: " + emailAddressList);
                emailAddressList.clear();//???
            }           
        }
        fileStream.close();
        logger.info("chuncked email address file");
        return null;
    }
}

我不知道如何处理和发送到VM 3(在此示例中)。如果我删除list.clear(),我会收到列表中的所有电子邮件[email01,email02 ... mail09],当我有clear()时,我得到一个空列表作为我的有效负载。

2 个答案:

答案 0 :(得分:0)

我个人认为从组件发送消息并不是一个好主意。它使得流程的可读性降低,因为它会为外部读者做出意想不到的事情。

如果您真的想要这样做,请使用component binding

但我建议:

  • 使用Mule Batch您拥有EE许可证。
  • 创建一个转换器,将输入流转换为List的迭代器,然后连接foreach。
  • 为InputStream创建一个自定义拆分器到多个List

答案 1 :(得分:-1)

List<String> emailAddressList = new ArrayList<String>();

        while ((emailAddress = br.readLine()) != null) {
            emailAddressList.add(emailAddress);
            if(emailAddressList.size() > 2) {
                muleClient.dispatch("vm://in", emailAddressList, null);
                logger.info("after dispatch to vm: " + emailAddressList);
                emailAddressList = new ArrayList<String>();
            }           
        }