動的呼び出しクライアント

サービスのデプロイがひとまず出来たので、ここでクライアントの作成も進めておこう。

既存のS2Axisの説明でも示されていますが、動的呼び出しクライアントとは、Javaインタフェースを介して任意のサービスのメソッドを実行する方法です。

Axis2では「Axis2APIを利用するクライアント」として説明されてますね。また、Axis2では同期呼び出しだけでなく、非同期呼び出しもできるようになっていますが、まずは前者のパターンを処理できるようにしていこう。

Axis2での動的呼び出しクライアント(同期)

こんな感じで実装します。

public class EchoBlockingClient {
    private static EndpointReference targetEPR = new EndpointReference(
            AddressingConstants.WSA_TO ,
            "http://127.0.0.1:8080/axis2/services/MyService");

    public static void main(String[] args) {
        try {
            OMElement payload = ClientUtil.getEchoOMElement();
            Options options = new Options();
            ServiceClient client = new ServiceClient();
            options.setTo(targetEPR);
            //Blocking invocation
            OMElement result = client.sendReceive(payload);
            ...
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
    }
}

メソッド呼び出しの引数/戻り値が、OMElement のオブジェクトなので、それがどうPOJO化できるかがポイントかな。
また、ClientUtil.getEchoOMElement では何をしているかっていうと、以下のような感じで以前に紹介したAXIOMのXMLデータを生成している。

public class ClientUtil {

    public static OMElement getEchoOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://example1.org/example1", "example1");
        OMElement method = fac.createOMElement("echo", omNs);
        OMElement value = fac.createOMElement("Text", omNs);
        value.addChild(fac.createText(value, "Axis2 Echo String "));
        method.addChild(value);

        return method;
    }
}

クライアントの呼び出しも、既存のS2Axisと同様、Axisコネクタで対応できそうですね。