日本語のページみてもまともに最後まで動くドキュメントがないから、本家のStartGuide?を和訳する。たぶん途中で、和訳に飽きるから、だれか気が向いたら手伝ってくれたらうれしい。
URLはこちら
http://ws.apache.org/axis2/1_5_1/quickstartguide.html#deploy
サービスをスタートさせよう!。以下、簡単な例を示します。それによって、サービスの構築方法や配置方法が理解できます。 StockQuoteService? という名前のサービスを用意しますが、これは、サービス構築に必須の例です。 では、以下の コード1をみてみましょう。
package samples.quickstart.service.pojo; import java.util.HashMap; public class StockQuoteService { private HashMap map = new HashMap(); public double getPrice(String symbol) { Double price = (Double) map.get(symbol); if(price != null){ return price.doubleValue(); } return 42.00; } public void update(String symbol, double price) { map.put(symbol, new Double(price)); } }
これは、呼び出し可能なシンプルな2つのサービスです。1つ目のgetPriceはメッセージの入出力が可能な例で。もう片方のupdateは、メッセージの戻り値がない例です。後ほど、4つの異なった方法を例に示すつもりです。
手始めに、単純なJavaクラスをサービス化する方法をみていきましょう。
Axis2を使って作業する前に、ちょっと準備必要です。 まず第一に、Axis2が動作する環境が整っている必要があるからです。
大抵は, さらにサービス用のWSDL fileが必要になります。 Axis2に含まれているJava2WSDLは WSDL作成ツールです。JAVAクラスからWSDLファイルを生成します。やり方の手順を示します。
%AXIS2_HOME%\bin\java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl
$AXIS2_HOME/bin/java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl
いったんWSDLを生成していれば、お好みに合わせて修正することができます。たとえば、 オリジナルな名前への変更したい時や不都合があった時にです。
たとえば、 AXIS2_HOME/samples/quickstartadb/resources/META-INF フォルダにできたStockQuoteService?.wsdl では, which we'll be using throughout the rest of this guide, replaces the generic parameters created by the generation process.
Before we build anything, it's helpful to understand what the finished product looks like.
The server side of Axis2 can be deployed on any Servlet engine, and has the following structure. Shown in Code Listing 2.
Code Listing 2: The Directory Structure of axis2.war
axis2-web META-INF WEB-INF classes conf axis2.xml lib activation.jar ... xmlSchema.jar modules modules.list addressing.mar ... soapmonitor.mar services services.list aservice.aar ... version.aar web.xml
Starting at the top, axis2-web is a collection of JSPs that make up the Axis2 administration application, through which you can perform any action such as adding services and engaging and dis-engaging modules. The WEB-INF directory contains the actual java classes and other support files to run any services deployed to the services directory.
The main file in all this is axis2.xml, which controls how the application deals with the received messages, determining whether Axis2 needs to apply any of the modules defined in the modules directory.
Services can be deployed as *.aar files, as you can see here, but their contents must be arranged in a specific way. For example, the structure of this service will be as follows:
- StockQuoteService - META-INF - services.xml - lib - samples - quickstart - service - pojo - StockQuoteService.class
Here, the name of the service is StockQuoteService?, which is specified in the services.xml file and corresponds to the top-level folder of this service. Compiled Java classes are placed underneath this in their proper place based on the package name. The lib directory holds any service-specific JAR files needed for the service to run (none in this case) besides those already stored with the Axis2 WAR file and the servlet container's common JAR directories. Finally, the META-INF directory contains any additional information about the service that Axis2 needs to execute it properly. The services.xml file defines the service itself and links the Java class to it (See Code Listing 3).
<service name="StockQuoteService" scope="application"> <description> Stock Quote Sample Service </description> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/> </messageReceivers> <parameter name="ServiceClass"> samples.quickstart.service.pojo.StockQuoteService </parameter> </service>
Here the service is defined, along with the relevant messageReceiver types for the different message exchange patterns.
The META-INF directory is also the location for any custom WSDL files you intend to include for this application.
You can deploy a service by simply taking this hierarchy of files and copying it to the webapps/axis2/WEB-INF/services directory of your servlet engine. (Note the Axis2 WAR file must be installed first in the servlet engine.) This is known as the "exploded" format. You can also compress your documents into an *.aar file, similar to a *.jar file, and place the *.aar file directly in the servlet engine's webapps/axis2/WEB-INF/services directory.
Now that you understand what we're trying to accomplish, we're almost ready to start building.
First, download and unzip the appropriate version of Axis2 Standard Binary Distribution. Make sure that you set the value of the AXIS2_HOME variable to match the location into which you extracted the contents of this release.
Let's look at some different ways to create clients and services.
この章では, 5通りのWebサービスの作り方示していきます。(訳:この和訳ではPOJO(deploying Plain Old Java Objects のこと)だけだけどね)。
POJOsをつかってWebサービスを配備する手順を示します。 注意: ディレクトリ構成は AXIS2_HOME/samples/quickstart で(XMLファイルのservices.xml はこのガイドの1番最初にでてきたものです。
- quickstart - README.txt - build.xml - resources - META-INF - services.xml - src - samples - quickstart - service - pojo - StockQuoteService.java
注意: that you can generate a WSDL from the quickstart directory by typing:
ant generate.wsdl
However, creating StockQuoteService?.wsdl is optional. It can be the version generated directly from the Java class, or a customized version of that file, and that services.xml is the same file referenced earlier in this document.
Now build the project by typing ant generate.service in the quickstart directory, which creates the following directory structure:
- quickstart/build/classes - META-INF - services.xml - samples - quickstart - service - pojo - StockQuoteService.class
If you want to deploy the service in an exploded directory format, rename the classes directory to StockQuoteService?, and copy it to the webapps/axis2/WEB-INF/services directory in your servlet engine. Otherwise, copy the build/StockQuoteService?.aar file to the webapps/axis2/WEB-INF/services directory in your servlet engine. Then check to make sure that the service has been properly deployed by viewing the list of services at:
http://localhost:8080/axis2/services/listServices
You can also checkout the WSDL at:
http://localhost:8080/axis2/services/StockQuoteService?wsdl
And the schema at:
http://localhost:8080/axis2/services/StockQuoteService?xsd
Once the URLs are working, quickly test the service. Try pointing your browser to the following URL:
http://localhost:8080/axis2/services/StockQuoteService/getPrice?symbol=IBM
You will get the following response:
<ns:getPriceResponse xmlns:ns="http://pojo.service.quickstart.samples/xsd"><ns:return>42</ns:return></ns:getPriceResponse>
If you invoke the update method as,
http://localhost:8080/axis2/services/StockQuoteService/update?symbol=IBM&price=100
and then execute the first getPrice URL, you will see that the price has got updated.