Seasar DI Container with AOP

S2Axis2 基本的な使い方

本ドキュメントはS2Axis2について記述しています。

目次

Webアプリケーションの設定

S2Axis2を使用するには、Axis2およびS2をweb.xmlに設定する必要があります。Axis2およびS2の設定の詳細はそれぞれのドキュメントを参照してください。

S2Axis2はS2コンテナの初期化時にコンポーネントをAxisに登録します。このため、S2よりも先にAxis2が初期化されている必要があります。そのためには、Axis2が提供するサーブレットのload-on-startupをS2よりも小さくします。

以下はS2Axis2Examplesに含まれているweb.xmlです。このサンプルでは、Axis2のload-on-startupを10、S2のload-on-startupを20に設定しています。ただし、S2Axisの場合と、Axis2のサーブレット部分の設定内容が異なっている点に注意してください。

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <display-name>s2axis2-examples</display-name>

    <filter>
        <filter-name>s2filter</filter-name>
        <filter-class>
            org.seasar.framework.container.filter.S2ContainerFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>s2filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<!--<init-param>-->
<!--<param-name>axis2.xml.path</param-name>-->
<!--<param-value>/WEB-INF/conf/axis2.xml</param-value>-->
<!--<param-name>axis2.repository.path</param-name>-->
<!--<param-value>/WEB-INF</param-value>-->
<!--</init-param>-->
<load-on-startup>10</load-on-startup>
</servlet> <servlet> <servlet-name>s2servlet</servlet-name> <servlet-class> org.seasar.framework.container.servlet.S2ContainerServlet </servlet-class> <init-param> <param-name>configPath</param-name> <param-value>app.dicon</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <load-on-startup>20</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>s2servlet</servlet-name> <url-pattern>/s2servlet</url-pattern> </servlet-mapping> <session-config> <!-- Default to 5 minute session timeouts --> <session-timeout>5</session-timeout> </session-config> <!-- currently the W3C havent settled on a media type for WSDL; http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft for now we go with the basic 'it's XML' response --> <mime-mapping> <extension>wsdl</extension> <mime-type>text/xml</mime-type> </mime-mapping> <mime-mapping> <extension>xsd</extension> <mime-type>text/xml</mime-type> </mime-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>

サーバ側の設定

s2axis2.diconファイルのインクルード

S2Axis2を使用するサーバ側では、app.diconファイルの中でs2axis2.diconファイルをインクルードします。

<components>
    <include path="s2axis2.dicon"/>
    ・・・
</components>

s2axis2.dicons2axis2-x.x.x.jarの中に含まれているため、ロケーションを気にする必要はありません。


サービスのデプロイ(コンポーネント定義)

S2コンテナで管理しているコンポーネントをWebサービスとしてAxis2にデプロイするには、そのコンポーネントの<component>要素の子として<meta>要素を指定します。

    <component name="Echo" class="org.seasar.remoting.axis2.examples.ex01.EchoImpl">
        <meta name="axis-service"/>
    </component>

<component>要素のname属性は必須です。公開されるサービスのURLは次のようになります。

diconファイルに名前空間が指定されていない場合
context-path/services/name
diconファイルに名前空間が指定されている場合
context-path/services/namespace/name

サービスのデプロイ(service.xmlでの定義)

Axis2では、service.xmlファイルを使用してサービスをデプロイしますが、S2Axis2でも、そのファイルを指定してサービスをデプロイすることができます。

この場合、<component>要素ではなく、<meta>要素を指定します。

    <meta name="axis-deploy">"org/seasar/remoting/axis2/examples/ex05/service.xml"</meta>

service.xmlの記述の仕方は、Axis2で利用するものと変わりません。

クライアント側の設定

S2Axis2を使うことで、Webサービスをコンポーネントとして使用することが出来ます。

Javaインタフェースを使用してサービスを呼び出すには、diconファイルを次のように記述します。

<!-- リモート呼び出しのインターセプタ -->
<component name="remoting"
           class="org.seasar.remoting.common.interceptor.RemotingInterceptor"/>

<!-- Axisサービス -->
<component class="org.apache.axis2.description.AxisService" autoBinding="none"/>

<!-- Axisコネクタ -->
<component name="connector"
           class="org.seasar.remoting.axis2.connector.AxisInOutConnector">
    <property name="baseURL">
        new java.net.URL("http://localhost:8080/s2axis2-examples/services/")
    </property>
</component>

<!-- Webサービスのプロキシ -->
<component name="Echo" class="org.seasar.remoting.axis2.examples.ex01.Echo">
    <aspect>remoting</aspect>
</component>

呼び出すWebサービスごとにプロキシが必要です。プロキシの<component>要素のclass属性にはWebサービスのJavaインタフェースを指定します。このコンポーネントにはRemotingInterceptorをアスペクトとして指定します。プロキシはWebサービスのインタフェースごとに一つの<component>定義が必要です。

RemotingInterceptorはプロキシのメソッド呼び出しを横取りし、Axisコネクタを呼び出します。上の例では、S2の自動バインディングによりRemotingInterceptorのプロパティにAxisコネクタが設定されています。RemotingInterceptorは、Axisコネクタごとに一つの<component>定義が必要です。

AxisコネクタはAxisサービスを通じてWebサービスを呼び出します。上の例では、S2の自動バインディングによりAxisコネクタのプロパティにAxisサービスが設定されています。baseURLプロパティには、WebサービスのエンドポイントのベースURLを指定します。このベースURLにプロキシの<component>要素のname属性で指定された名前を付加したURLが実際の通信で使用されます。AxisコネクタはWebサービスのエンドポイントごとに一つの<component>定義が必要です。

メッセージ形式に合わせたAxisコネクタ

Axis2では、同期型だけでなく非同期型のメッセージ形式もサポートしています。S2Axis2では、メッセージ形式に合わせてAxisコネクタを変更します。

各メッセージ形式と、Axisコネクタの対応は以下のようになっています。システムに合わせて、diconファイルで指定するAxisコネクタを適宜変更してください。

メッセージ形式 説明 Axisコネクタ
同期型要求応答形式
(Request/Response)
クライアントから要求を送信した後、サービスから応答が返るまで処理を待ち受ける。 org.seasar.remoting.axis2.connector.AxisInOutConnector
一方向形式
(Oneway)
サービス処理の終了を待たずにクライアントの処理継続する。戻り値はなく、クライアントでは例外の検出もしない。 org.seasar.remoting.axis2.connector.AxisOnewayConnector
非同期型コールバック形式
(Callback)
サービス処理を呼び出す際にコールバックオブジェクトを指定することで、サービス処理終了後にコールバックオブジェクトが呼び出され、応答が通知される。 ※今後、対応予定

diconファイルは、次のように記述します。

<!-- 同期型要求応答形式 -->
<component name="ioRemoting"
           class="org.seasar.remoting.common.interceptor.RemotingInterceptor">
    <property name="connector">ioConnector</property>
</component>
<component name="ioConnector"
           class="org.seasar.remoting.axis2.connector.AxisInOutConnector">
    <property name="baseURL">baseURL</property>
</component>

<!-- 一方向形式 -->
<component name="oneWayRemoting"
           class="org.seasar.remoting.common.interceptor.RemotingInterceptor">
    <property name="connector">oneWayConnector</property>
</component>
<component name="oneWayConnector"
           class="org.seasar.remoting.axis2.connector.AxisOnewayConnector">
    <property name="baseURL">baseURL</property>
</component>

<component class="org.apache.axis2.description.AxisService" autoBinding="none"/>

<component name="baseURL" class="java.net.URL" autoBinding="none">
    <arg>"http://localhost:8080/s2axis2-examples/services/"</arg>
</component>