“即時發布是一項令人激動的技術,它使
Web
服務的開發變得如此簡單;然而即時發布并不總是最好的選擇,比如有些應用系統是第三方提供的,我們沒有購買源代碼,只有
.class
文件,但我們又希望將這個應用系統的一些功能對外發布成
Web
服務,使其能夠在更大范圍內產生作用,這個時候即時發布技術就無能為力了。此外,即時發布技術并不靈活,無法進行更多的服務配置,這使得它并不能滿足一些特定系統的需求。”
????? 好啦
到我們啦,我先不說定制發布有什么好,大家先往下看,自然會有體會的:)
首先給出我們的模擬銀行存取款的
java
文件:
Account.java
?
package com.duckur;
public class? Account
{
????????? /*************************************************************
???????? ?/*
從隨機數中獲取帳戶(一次性有效)
*/
???????? ?public static int fund = (int)(java.lang.Math.random()*10000);
???????? ?//
模擬,大家可以從文件中讀取數據,或者從數據庫中取出這個
fund
值
?????????? /*************************************************************
?????????? ?/*
檢查輸入的有效性
*/
????????? ?public boolean checkInput(int money)//
只能取整數
????????? ?{
?????????? ?if(money > fund)
??????????? {
???????????????? ?return false;
???????????? }
???????????? else
??????????? ?{
???? ??????????? return true;
?????????????? }
??????????
?}
????????? ?/*************************************************************
?????????? /*
存款
*/
?????????? public int deposit(int money)
???????? ?{
????????????????
fund? = fund + money;
?????????????? ?return fund;
??????????
}
????????? /*************************************************************
?????????
/*
取款
*/
?????????
public int withdraw(int money)
?????????
{
???????????????????
if(checkInput(money))
????????????????????
{
?????????????????????????????
fund = fund - money;
????????????????????
}
????????????????????
return fund;
??????????
}
?????????
/*************************************************************
??????????
/*
獲取當前的帳戶值
*/
??????????
public int getAccount()
??????????
{
????????????????????
return fund;
?????????
}
}
???? 然后編譯,通過后生成的class文件應該放在Tomcat下的webapps\axis\WEB-INF\com\duckur\下面。
下面發布:
???? 在Tomcat下的webapps\axis\WEB-INF\com\duckur\目錄下新建一個deploy.wsdd文件描述服務的名稱、入口等信息:
wsdd文件的顯示有問題下載吧
?
其中service項重的名稱為該服務的名稱即Account,java:RPC指明了一個JAVA RPC服務,做這個處理的類是org.apache.axis.providers.java.RPCProvider。
?? “我們是通過一個標簽告訴RPC服務應該調用的類,而另外一個標簽則告訴引擎,它可以調用這個類中的任何的Public方法。你也可以指定通過使用名字空間或者一些可以調用的方法列表,來指明那些方法可以被調用。”
??? ?當然也可以用Axis提供的一個客戶端管理工具——AdminClient來完成服務的定制發布。這里不說:)
??
然后在DOS下到該目錄下,
java org.apache.axis.client.AdminClient deploy.wsdd
如果出現:
Processing file deploy.wsdd
Doneprocessing
這表明Capacity服務定制發布完成。
好現在你就可以通過http://localhost:8080/axis/services/Account?wsdl來查看WSDL描述了。
?
Web Service發布好了現在調用它吧.
這里說一個最基本的調用需要用的類和方法:
(取出當前的金額)
...
//新建一個服務對象
Service service = new Service();??
//調用服務對象的createCall()方法返回一個命令
Call call = (Call) service.createCall();??
// Sets the address of the target service endpoint.
call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/axis/services/LxAccount"));
//?Sets the name of the operation to be invoked using this Call
instance
call.setOperationName(“getAccount”);
//Convenience method to invoke a //method with a default (empty) namespace
Integer myFund = (Integer) call.invoke(new Object[] {});
...
?
詳細函數可以參考開發API在下載的axis.zip中的docs里面。
下面是詳細的代碼:
編譯后執行就可以。
?
LxClient.java
?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
?
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
?
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
?
public class LxClient
extends JFrame {
??JTextField jTextField1 = new JTextField();
??JLabel jLabel3 = new JLabel();
??JLabel jLabel1 = new JLabel();
? JButton jButton1 = new JButton();
? JButton jButton2 = new JButton();
??JButton jButton3 = new JButton();
??JLabel jLabel2 = new JLabel();
??JTextField jTextField2 = new JTextField();
?
??public LxClient() {
??? try {
?????? jbInit();
??? }
???? catch (Exception e) {
?????? e.printStackTrace();
???? }
??}
?
??public static void main(String args[]){
?? ?LxClient myframe=new LxClient();
??? myframe.setSize(400,200);
?? ?myframe.setVisible(true);
?
??}
?
?private void jbInit() throws Exception {
?? ?this.getContentPane().setLayout(null);
?? ?jTextField1.setText("");
?? ?jTextField1.setBounds(new Rectangle(131, 51, 80, 24));
?? ?jLabel3.setText("A0317286 李迅");
?? ?jLabel3.setBounds(new Rectangle(102, 15, 108, 17));
??? jLabel1.setText("填入金額:");
??? jLabel1.setBounds(new Rectangle(64, 54, 55, 21));
??? jButton1.setBounds(new Rectangle(50, 96, 58, 23));
??? jButton1.setText("存款");
??? jButton1.addActionListener(new LxClient_jButton1_actionAdapter(this));
??? jButton2.setBounds(new Rectangle(126, 96, 63, 23));
??? jButton2.setText("取款");
??? jButton2.addActionListener(new LxClient_jButton2_actionAdapter(this));
??? jButton3.setBounds(new Rectangle(205, 94, 81, 24));
??? jButton3.setText("查看金額");
??? jButton3.addActionListener(new LxClient_jButton3_actionAdapter(this));
??? jLabel2.setText("當前的金額:");
??? jLabel2.setBounds(new Rectangle(59, 136, 74, 21));
??? jTextField2.setEditable(false);
??? jTextField2.setText("");
??? jTextField2.setBounds(new Rectangle(127, 133, 85, 25));
??? this.getContentPane().add(jTextField1, null);
??? this.getContentPane().add(jLabel3, null);
??? this.getContentPane().add(jLabel1, null);
??? this.getContentPane().add(jButton2, null);
??? this.getContentPane().add(jButton1, null);
??? this.getContentPane().add(jButton3, null);
??? this.getContentPane().add(jLabel2, null);
??? this.getContentPane().add(jTextField2, null);
? ?}
?
?/***************************************************************************
?調用WebService* /
?存款*/
??void jButton1_actionPerformed(ActionEvent e) {
??? try {
??? String endpoint = "http://localhost:8080/axis/services/LxAccount";
??? String method = "deposit";
??? Integer i = Integer.valueOf(jTextField1.getText());
??? Service service = new Service();
??? Call call = (Call) service.createCall();
?
??? call.setTargetEndpointAddress(new java.net.URL(endpoint));
??? call.setOperationName(method);
?
??? Integer myFund = (Integer) call.invoke(new Object[] {i});
??? //更新數據
??? jTextField2.setText(myFund.toString());
??? jLabel1.updateUI();
??? }
??? catch (Exception ex) {
??? System.err.println(ex.toString());
??? }
??}
?/***************************************************************************
? ?取款*/
??void jButton2_actionPerformed(ActionEvent e) {
??? try {
??? String endpoint = "http://localhost:8080/axis/services/LxAccount";
??? String method = "withdraw";
??? Integer i = Integer.valueOf(jTextField1.getText());
????Service service = new Service();
????Call call = (Call) service.createCall();
?
??????? call.setTargetEndpointAddress(new java.net.URL(endpoint));
??????? call.setOperationName(method);
?
??????????? Integer myFund = (Integer) call.invoke(new Object[] {i});
??????? //更新數據
??????? jTextField2.setText(myFund.toString());
??????? jLabel1.updateUI();
?????? }
??????? catch (Exception ex) {
??????? System.err.println(ex.toString());
??? }
??}
??/***************************************************************************
? ?顯示當前金額*/
??void jButton3_actionPerformed(ActionEvent e) {
??? try {
??????? String endpoint = "http://localhost:8080/axis/services/LxAccount";
??????? String method = "getAccount";
?
??????? Service service = new Service();
??????? Call call = (Call) service.createCall();
?
??????? call.setTargetEndpointAddress(new java.net.URL(endpoint));
??????? call.setOperationName(method);
?
??????? Integer myFund = (Integer) call.invoke(new Object[] {});
??????? //更新數據
??????? jTextField2.setText(myFund.toString());
??????? jLabel1.updateUI();
??? }
??? catch (Exception ex) {
??????? System.err.println(ex.toString());
??? }
??}
}
?
class LxClient_jButton1_actionAdapter
??? ?implements java.awt.event.ActionListener {
??LxClient adaptee;
?
??LxClient_jButton1_actionAdapter(LxClient adaptee) {
???? ?this.adaptee = adaptee;
??}
?
??public void actionPerformed(ActionEvent e) {
???? adaptee.jButton1_actionPerformed(e);
??}
}
?
class LxClient_jButton2_actionAdapter
??? ?implements java.awt.event.ActionListener {
?LxClient adaptee;
?
?LxClient_jButton2_actionAdapter(LxClient adaptee) {
??? ?this.adaptee = adaptee;
?}
?
?public void actionPerformed(ActionEvent e) {
??? ?adaptee.jButton2_actionPerformed(e);
??}
}
?
class LxClient_jButton3_actionAdapter
?? ?implements java.awt.event.ActionListener {
?LxClient adaptee;
?
???LxClient_jButton3_actionAdapter(LxClient adaptee) {
??? this.adaptee = adaptee;
??}
?
??public void actionPerformed(ActionEvent e) {
????? adaptee.jButton3_actionPerformed(e);
??}
}
?