構建 SOAP 服務
如果您覺得構建 Web 服務客戶機的過程相當簡單,事實的確如此。而就很多方面而言,構建服務的過程也同樣簡單。
總體過程
創建 Axis2 Web 服務的整個過程涉及以下步驟:
- 創建服務清單
- 創建類
- 將其打包為 Axis 存檔文件
- 將 Axis 存檔文件上載到 Axis2 Web 應用程序
- 重新啟動服務器(如果有必要)
這就是全部步驟。讓我們首先從服務清單開始。
創建清單
服務清單告知 Axis2 應用程序(就更大的范圍而言,應用服務器)哪個請求與哪個類對應。例如,可以如清單 22 中所示的那樣指定兩個服務函數。
清單 22. 在清單中指定兩個服務函數
<service name="CMSService">
<description>
This is a sample Web Service for the newspaper's
Content Managment System.
</description>
<parameter name="ServiceClass" locked="false"
>CMSService</parameter>
<operation name="getNumberOfArticles">
<messageReceiver class=
"org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="addArticle">
<messageReceiver class=
"org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
</operation>
</service>
|
首先,定義服務,提供其名稱和描述,并指定實際為請求服務的類。接下來,定義實際的操作。請注意,此示例指定了兩種不同類型的 messageReceiver
。第一個 RawXMLINOutMessageReceiver
用于傳統的請求/響應服務。第二個 RawXMLINOnlyMessageReceiver
用于單向消息。操作的名稱與有效負載的根元素以及要執行的方法對應。
將此文件保存為 services.xml。
接下來,讓我們創建實際的應用程序。
創建應用程序
讓我們首先創建模擬前面看到的 echo
函數的類(將直接返回原始有效負載的副本),如清單 23 中所示。
清單 23. CMSService 類
import org.apache.axis2.om.OMElement;
import javax.xml.stream.XMLStreamException;
public class CMSService {
public OMElement getNumberOfArticles(OMElement element)
throws XMLStreamException {
element.build();
element.detach();
return element;
}
}
|
要編譯此應用程序,請確保 <axis2_home>/lib 中的所有 *.jar 文件都在您的 CLASSPATH 上。
此應用程序相當簡單,僅包含一個與 getNumbereOfArticles
操作對應的類。此函數和任何要作為操作的函數一樣,接收單個 OMElement
參數(表示有效負載)。此處,您將首先使用 build()
方法來確定已接收到所有數據——AXIOM 使用一個 pull 方法訪問數據——然后將元素從其當前樹中分離,以便能夠將其返回。
如果喜歡冒險,可以自由地部署服務和訪問服務,以訪問服務并查看結果。應該看到與清單 24 中所示類似的結果輸出。
清單 24. CMSService 類響應
<cms:getNumberOfArticles><cms:category>classifieds</cms:category></cms:
getNumberOfArticles>
|
接下來讓我們了解如何實際處理數據。
提取有效負載
為了從有效負載提取信息,將使用與 DOM 非常類似的技術來對接收到的有效負載元素進行操作(請參見清單 25)。
清單 25. 提取有效負載信息
...
import javax.xml.stream.XMLStreamException;
public class CMSService {
public OMElement getNumberOfArticles(OMElement element)
throws XMLStreamException {
element.build();
element.detach();
String rootName = element.getLocalName();
OMElement categoryElement = element.getFirstElement();
String categoryElementName = categoryElement.getLocalName();
String categoryValue = childElement.getText();
return element;
}
}
|
請記住,有效負載的根是 getNumberOfArticles
函數接收的元素。在此情況下,將提取元素的名稱,然后移動到第一個元素子項(與第一個子項不同,后者可能是空格文本節點)并提取其名稱和值。請注意,使用的是 getText()
方法來提取實際上是 category 元素的文本節點子項的值。這無疑非常簡捷!
創建并返回響應
最后,將需要使用從請求的有效負載提取數據來創建響應。在本例中,將從第二個函數(在實際應用程序中,該函數將進行一些其他的工作)提供響應(請參見清單 26)。
清單 26. 創建響應
...
import javax.xml.stream.XMLStreamException;
public class CMSService {
public OMElement getNumberOfArticles(OMElement element)
throws XMLStreamException {
element.build();
element.detach();
String rootName = element.getLocalName();
OMElement childElement = element.getFirstElement();
String childName = childElement.getLocalName();
String categoryValue = childElement.getText();
SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
OMNamespace namespace = factory.createOMNamespace(
"http://daily-moon.com/cms/", "resp");
OMElement resultElem = factory.createOMElement(
"numberOfArticles",namespace);
String actualValue =
(articleCount(categoryValue)).toString();
resultElem.setText(actualValue);
return resultElem;
}
private Integer articleCount(String catId){
//Perform some function such as searching the CMS
//database, and return the actual value. For our
//purposes, you'll hardcode it.
return new Integer(42);
}
}
|
首先,創建將用于創建所有其他對象的工廠,然后創建將添加到響應的有效負載的命名空間。接下來,創建實際結果元素,在本例中為名為 numberOfArticles
的元素。
numberOfArticles
元素的內容將為 articleCount()
函數返回的一個數字,在本例中,該函數可以為任何內容。在實際的應用程序中,將進行所需進行的任何工作來獲取此數據。獲取了此數據后,會將其設置為 numberOfArticles
元素的內容,并直接返回該元素。
現在剩下的就是部署服務了。
部署服務
為了部署服務,需要創建一個 Axis 存檔文件。此文件和 *.jar 或 *.war 文件類似,實際是使用特殊文件擴展名(在本例中使用的是 .aar)的 zip 文件。請按照以下步驟創建此文件:
- 將 <AXIS2_HOME>/lib 目錄中的所有文件添加到 CLASSPATH 并編譯 CMSService.java 文件。
- 在與 CMSService.class 文件相同的目錄中創建名為 META-INF 的新目錄。
- 從包含 CMSService.class 文件的目錄中發出以下命令:<code type="section" width="100"> jar cvf CMSService.aar ./* </code> 應該看到與以下類似的結果:<code type="section" width="100"> added manifest adding:CMSService.class(in = 513) (out= 330)(deflated 35%) adding:CMSService.java(in = 328) (out= 182)(deflated 44%) ignoring entry META-INF/ adding:META-INF/services.xml(in = 391) (out= 229)(deflated 41%) </code>
- 使用安裝示例服務中列出的步驟將此服務添加到服務器上。(如果看到 Web 接口上有 Servlet 錯誤,請確保登錄到了 Axis2 應用程序。如果會話已過期,應用程序將不一定會通知您,而可能會直接顯示錯誤。)
- 如果有必要,請重新啟動 Geronimo。(將可能不必在添加服務后進行此操作,但在進行更改后可能必須這樣做。)
如果單擊 View services 鏈接,應該看到與圖 4 中所示類似的內容。
圖 4. 可用服務

?
訪問服務
現在已經完成了服務構建,接下來要通過客戶機對其進行訪問。對前面創建的 ClassifiedClient.java 文件進行以下更改(請參見清單 27)。
清單 27. 修改 ClassifiedClient
...
public class ClassifiedClient {
private static EndpointReference targetEPR =
new EndpointReference(
"http://localhost:8080/axis2/services/CMSService");
public static OMElement getEchoOMElement() {
SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
OMNamespace omNs = fac.createOMNamespace(
"http://daily-moon.com/cms", "cms");
OMElement method = fac.createOMElement("getNumberOfArticles", omNs);
OMElement value = fac.createOMElement("category", omNs);
value.addChild(fac.createText(value, "classifieds"));
method.addChild(value);
return method;
}
public static void main(String[] args) {
try {
OMElement payload = ClassifiedClient.getEchoOMElement();
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload);
String response = result.getText();
System.out.println("There are "+response+" classifieds at the moment.");
} catch (Exception e) { //(XMLStreamException e) {
System.out.println(e.toString());
}
}
}
|
編譯并運行了此應用程序后,應看到清單 28 中所示的響應。
清單 28. ClassifiedClient 響應
There are 42 classifieds at the moment.
|
單向服務
繼續討論之前,讓我們了解一下處理單向服務(而非請求/響應服務)時涉及到的不同之處。
服務
創建單向服務非常簡單。此過程與創建請求/響應服務完全類似,至少不會實際返回任何內容。例如,可以為 CMSService
類創建 addArticle
操作,如圖 29 中所示。
清單 29. CMSServiceclass 中的 addArticle 操作...
private Integer articleCount(String catId){
...
}
public void addArticle(OMElement element)
throws XMLStreamException{
element.build();
System.out.println(element);
}
}
|
在 services.xml 文件中,將 addArticle
操作指定為“in only”操作,因此不會等待返回任何內容,但即使這樣,也能看到會實際發生一些事項,會在命令行輸出接收到的有效負載。您將在 Geronimo 窗口中看到此信息。
在實際應用程序中,此方法將從有效負載提取信息,并會實際添加到某種類型的數據庫或其他存儲庫。
客戶機
此服務的客戶機也與請求/響應服務所使用的服務類似(請參見清單 30)。
清單 30. 創建客戶機
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.om.OMElement;
import org.apache.axis2.SOAP.SOAPFactory;
import org.apache.axis2.om.OMAbstractFactory;
import org.apache.axis2.om.OMNamespace;
public class AddArticleClient {
private static EndpointReference targetEPR =
new EndpointReference(
"http://localhost:8080/axis2/services/CMSService");
private static OMElement getOMElement(){
SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
OMNamespace omNs = fac.createOMNamespace(
"http://daily-moon.com", "cms");
OMElement method = fac.createOMElement("addArticle", omNs);
OMElement category = fac.createOMElement("category", omNs);
category.setText("classifieds");
OMElement subcategory =
fac.createOMElement("subcategory", omNs);
category.setText("wantads");
OMElement adtext = fac.createOMElement("article", omNs);
adtext.setText("Do you have good head for numbers"+
" and a great deal of patience? Do you like"+
" to sit for hours sorting objects by their"+
" size? If so, then you could be the"+
" next goober counter in the world famous"+
" Murphy Brothers peanut factory. "+
" Willingness to dress up as our mascot"+
" helpful, but not required.");
method.addChild(category);
method.addChild(subcategory);
method.addChild(adtext);
return method;
}
public static void main(String[] args) {
try {
OMElement payload = AddArticleClient.getOMElement();
ServiceClient serviceClient = new ServiceClient();
Options options = new Options();
serviceClient.setOptions(options);
options.setTo(targetEPR);
serviceClient.fireAndForget(payload);
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}
|
盡管有效負載不同,但正如您在 getOMElement()
方法中看到的,編程方面目前的唯一真正的更改是使用 fireAndForget()
方法替代 sendReceive()
方法。此方法并不會返回響應。
如果運行此客戶機,應該在 Geronimo 窗口中看到與圖 5 中所示類似的輸出。
圖 5. 命令行輸出
posted on 2006-12-29 18:39
SIMONE 閱讀(844)
評論(0) 編輯 收藏 所屬分類:
AXIS 、
JAVA