Skip navigation

サービスからXMPLLを呼び出す

XMPPトランスポート・プロトコルを使ってESB Muleからサービスを呼び出す例です。XMPPに準拠したインスタント・メッセンジャーを使ってユーザが入力したコマンドの結果をESB Muleが返します。
この例で使うサービスはユーザの指定によって、現在の時刻, 時間,又は日付と時間を返す簡単なPOJOです。ユーザがインスタント・メッセンジャーに「date」と入力した場合は、ESB Muleは現在の日付を返します。「time」と入力した場合は、現在の時間を返します。「datetime」と入力した場合は、現在の日付と時間を返します。その他の文字列を入力した場合は、「不正な入力」と返します。

package org.mulesource.cookbook.xmpp;

import java.util.*;
import java.text.DateFormat;

public class XmppService {
   public String doReply (String req) {
      String reply = "不正な入力";

      if (req.equalsIgnoreCase("date")) {
         Date now = new Date();
         DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.FULL);
         reply = dateFormatter.format(now);
         return reply
      }

      else if (req.equalsIgnoreCase("time")) {
         Date now = new Date();
         DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT);
         reply = timeFormatter.format(now);
         return reply;
      }

      else if (req.equalsIgnoreCase("datetime")) {
         Date now = new Date();
         reply = now.toString();
         return reply;
      }

     else return reply;
   }
}

次にESB Muleをユーザのインスタント・メッセンジャーからリクエストと取得して返すように、このサービスを使うように設定します。以下はこの設定の例です:

<mule-configuration id="XmppService" version="1.0">
  <transformers>
    <transformer name="XmppToString"
                       className="org.mule.providers.xmpp.transformers.XmppPacketToString"
                       returnType="java.lang.String" />
  </transformers>
  <model name="XmppReply">
    <mule-descriptor name="getXmppReply" implementation="org.mulesource.cookbook.xmpp.XmppService">
      <inbound-router>
        <endpoint address="xmpp://muleagent:test@jabber.org:5222/" transformers="XmppToString" />
      </inbound-router>
      <outbound-router>
        <router className="org.mule.routing.outbound.OutboundPassThroughRouter">
          <endpoint address="xmpp://muletest:test@jabber.org/" />
        </router>
      </outbound-router>
    </mule-descriptor>
  </model>
</mule-configuration>

設定に付いて説明をします。ESB Muleはユーザ「muleagent」への入力メッセージを監視します。「muleagent」への全てのメッセージは文字列(String)オブジェクトに変換され、前に作成したPOJOに渡されます。メッセージはPOJOで処理され、結果はリクエストを送信したユーザ「muletest」のインスタント・メッセンジャー・クライアントへ送信されます。送信者と別のユーザに返信したい場合は、出力ルート(outbound-router)のエンドポイント・アドレスに別のユーザを指定します。

Adaptavist Theme Builder Powered by Atlassian Confluence