“即时发布是一项令人激动的技术,它使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);
  }
}