3

私は Mule 3.3 を初めて使用し、送信者フィールドと件名フィールドに特定のキーワードが含まれている場合、POP3 サーバーから電子メールを取得し、CSV 添付ファイルをダウンロードするために使用しようとしています。Mulesoft Web サイトで提供されている例を使用して、受信トレイをスキャンして新しい電子メールを探し、CSV 添付ファイルのみをダウンロードすることに成功しました。ただし、件名と送信者のフィールドでメールをフィルタリングする方法がわからないため、行き詰まっています。

いくつかの調査を行って、エンドポイントに適用できるメッセージ プロパティ フィルター パターンタグに出くわしましたが、着信または発信のどのエンドポイントに適用するか正確にはわかりません。どちらのアプローチも機能していないようで、このタグの使用方法を示す適切な例が見つかりません。実装したい基本的なアルゴリズムは次のとおりです。

if email is from "Bob"
  if attachment of type "CSV"
    then download CSV attachment

if email subject field contains "keyword"
  if attachment of type CSV
    then download CSV attachment

私がこれまでに持っているMule xmlは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:pop3s="http://www.mulesoft.org/schema/mule/pop3s" xmlns:pop3="http://www.mulesoft.org/schema/mule/pop3" 
xmlns="http://www.mulesoft.org/schema/mule/core" 
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/pop3s http://www.mulesoft.org/schema/mule/pop3s/current/mule-pop3s.xsd 
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/pop3 http://www.mulesoft.org/schema/mule/pop3/current/mule-pop3.xsd ">


<expression-transformer expression="#[attachments-list:*.csv]" 
   name="returnAttachments" doc:name="Expression">
</expression-transformer>

<pop3s:connector name="POP3Connector" 
    checkFrequency="5000" 
    deleteReadMessages="false" 
    defaultProcessMessageAction="RECENT" 
    doc:name="POP3" 
    validateConnections="true">
</pop3s:connector>

<file:connector name="fileName" doc:name="File">
    <file:expression-filename-parser />
</file:connector>

<flow name="incoming-orders" doc:name="incoming-orders">

    <pop3s:inbound-endpoint user="my_username" 
        password="my_password" 
        host="pop.gmail.com" 
        port="995" 
        transformer-refs="returnAttachments"
        doc:name="GetMail" 
        connector-ref="POP3Connector" 
        responseTimeout="10000"/>
    <collection-splitter doc:name="Collection Splitter"/>

    <echo-component doc:name="Echo"/>

    <file:outbound-endpoint path="/attachments" 
        outputPattern="#[function:datestamp].csv"
        doc:name="File" responseTimeout="10000"> 
        <expression-transformer expression="payload.inputStream"/>
        <message-property-filter pattern="from=(.*)(bob@email.com)(.*)" caseSensitive="false"/>
    </file:outbound-endpoint>           
</flow>

この問題に取り組む最善の方法は何ですか?

前もって感謝します。

4

2 に答える 2

8

あなたを助けるために、ここに2つの設定ビットがあります:

  • fromAddress次のフィルターは、 'Bob' で、件名に 'keyword' が含まれるメッセージのみを受け入れます。

    <expression-filter
        expression="#[message.inboundProperties.fromAddress == 'Bob' || message.inboundProperties.subject contains 'keyword']" />
    
  • 次のトランスフォーマーは、名前が「.csv」で終わるすべての添付ファイルを抽出します。

    <expression-transformer
        expression="#[($.value in message.inboundAttachments.entrySet() if $.key ~= '.*\\.csv')]" />
    
于 2012-12-31T17:49:46.633 に答える
3

ミュールへようこそ!数か月前、私は顧客のために同様のプロジェクトを実装しました。あなたのフローを見て、リファクタリングを始めましょう。

  • 受信エンドポイントから transform-refs="returnAttachments" を削除します
  • 次の要素をフローに追加します

    <pop3:inbound-endpoint ... />
    <custom-filter class="com.benasmussen.mail.filter.RecipientFilter"> 
        <spring:property name="regex" value=".*bob.bent@.*" />
    </custom-filter>
    <expression-transformer>
        <return-argument expression="*.csv" evaluator="attachments-list" />
    </expression-transformer>
    <collection-splitter doc:name="Collection Splitter" />
    
  • RecipientFilter を Java クラスとしてプロジェクトに追加します。正規表現パターンに一致しない場合、すべてのメッセージは破棄されます。

    package com.benasmussen.mail.filter;
    
    import java.util.Collection;
    import java.util.Set;
    import java.util.regex.Pattern;      
    import org.mule.api.MuleMessage;
    import org.mule.api.lifecycle.Initialisable;
    import org.mule.api.lifecycle.InitialisationException;
    import org.mule.api.routing.filter.Filter;
    import org.mule.config.i18n.CoreMessages;
    import org.mule.transport.email.MailProperties;
    
    public class RecipientFilter implements Filter, Initialisable
    {
        private String regex;
        private Pattern pattern;
    
        public boolean accept(MuleMessage message)
        {
            String from = message.findPropertyInAnyScope(MailProperties.FROM_ADDRESS_PROPERTY, null);
            return isMatch(from);
        }
    
        public void initialise() throws InitialisationException
        {
            if (regex == null)
            {
                throw new InitialisationException(CoreMessages.createStaticMessage("Property regex is not set"), this);
            }
            pattern = Pattern.compile(regex);
        }
    
        public boolean isMatch(String from)
        {
            return pattern.matcher(from).matches();
        }
    
        public void setRegex(String regex)
        {
            this.regex = regex;
        }
    }
    

ミュール式フレームワークは強力ですが、ユース ケースによっては、独自のビジネス ロジックが好まれます。

改善

  • アプリケーションのプロパティ (mule-app.properties) を使用する > Mule のドキュメント

ドキュメンテーション

于 2012-12-31T17:07:34.310 に答える