天天看點

基于java自身技術實作消息方式的系統間通信

這篇部落格基本照搬了分布式java應用基礎與實踐一書的内容

java自帶的遠端調用分兩種一種是rmi,一種是webservice

我們先看rmi(remote method invoke)#

基于java自身技術實作消息方式的系統間通信

使用rmi

看代碼

/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
package book.chapter1.rmi;


import java.rmi.Remote;
import java.rmi.RemoteException;




/**
 * 描述:伺服器端的業務功能類
 *
 * @author bluedavy 
 * 建立時間: 2009-1-4
 */


public interface Business extends Remote{


  /**
   * 顯示用戶端提供的資訊,并傳回
   */
  public String echo(String message) throws RemoteException;
  public int add(int a,int b) throws RemoteException;
}


/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
package book.chapter1.rmi.impl;


import java.rmi.RemoteException;


import javax.jws.WebService;


import book.chapter1.rmi.Business;


/**
 * 描述:
 *
 * @author bluedavy 
 * 建立時間: 2009-1-4
 */
@WebService
public class BusinessImpl implements Business {


  /* (non-Javadoc)
   * @see book.chapter1.rmi.Business#echo(java.lang.String)
   */
  public String echo(String message) throws RemoteException {
    if("quit".equalsIgnoreCase(message.toString())){
      System.out.println("Server will be shutdown!");
      System.exit(0);
    }
    System.out.println("Message from client: "+message);
    return "Server response:"+message;
  }


  @Override
  public int add(int a, int b) throws RemoteException {
    System.out.println("a+b");
    return a+b;
  }


}
package book.chapter1.rmi;
/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


import javax.xml.ws.Endpoint;


import book.chapter1.rmi.impl.BusinessImpl;


/**
 * 描述:基于RMI實作的伺服器端
 *
 * @author bluedavy 
 * 建立時間: 2009-1-4
 */
public class Server {


  /**
   * @param args
   */
  public static void main(String[] args) throws Exception{
    String name="BusinessDemo";
    Business business=new BusinessImpl();
    //UnicastRemoteObject.exportObject的第二個參數我也不知道有什麼用
    UnicastRemoteObject.exportObject(business, 0);
    Registry registry=LocateRegistry.createRegistry(1099);
    registry.rebind(name, business);
  }


}


package book.chapter1.rmi;
/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


/**
 * 描述:基于RMI實作的用戶端
 *
 * @author bluedavy 
 * 建立時間: 2009-1-4
 */
public class Client {


  /**
   * @param args
   */
  public static void main(String[] args) throws Exception{
    //下面我沒有寫端口 那就預設是1099
    Registry registry=LocateRegistry.getRegistry("10.150.0.80");
    String name="BusinessDemo";
    Business business=(Business) registry.lookup(name);
    System.out.println(business.add(5, 4));
  }
}      

我在兩台機子上運作,在我自己機子上運作server端,在朋友電腦上運作的是client端,他那邊有Clinet.java與Business.java#運作時報ClassNotFoundException#

把BusinessImpl也給他發過去後,就一切ok了#

他那麼直接列印出一個9,我這邊列印出a+b#

可見計算的過程确實是在我這邊的#

使用webservice

/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
package book.chapter1.webservice.impl;




/**
 * 描述:服務端對外暴露的接口
 *
 * @author bluedavy 
 * 建立時間: 2009-2-11
 */
public interface Business {


  /**
   * 顯示用戶端提供的資訊,并傳回
   */
  public String echo(String message);
  
}


/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
package book.chapter1.webservice.impl;


import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;




/**
 * 描述:以webservice方式對外暴露的服務
 *
 * @author bluedavy 
 * 建立時間: 2009-2-11
 */
@WebService(name="Business",serviceName="BusinessService",targetNamespace="http://webservice.chapter1.book/client")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class BusinessImpl implements Business {


  /* (non-Javadoc)
   * @see book.chapter1.webservice.Business#echo(java.lang.String)
   */
  public String echo(String message) {
    if("quit".equalsIgnoreCase(message.toString())){
      System.out.println("Server will be shutdown!");
      System.exit(0);
    }
    System.out.println("Message from client: "+message);
    return "Server response:"+message;
  }


}
/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
package book.chapter1.webservice;


import javax.xml.ws.Endpoint;


import book.chapter1.webservice.impl.BusinessImpl;


/**
 * 描述:基于Java Webservice實作的伺服器端
 *
 * @author bluedavy 
 * 建立時間: 2009-2-11
 */
public class Server {


  /**
   * @param args
   */
  public static void main(String[] args) {
    Endpoint.publish("http://localhost:9527/BusinessService", new BusinessImpl());
    System.out.println("Server has beed started");
  }


}      

有了上面三個類之後,我們可以通過jdk bin目錄下的wsimport指令來生成輔助調用代碼,如下:

基于java自身技術實作消息方式的系統間通信

之後,我們發現jdk的工具給我們在book\chapter1\webservice\client下産生了兩個類,Business.java,BusinessService.java#

這個兩個類,我們其實可以不用看裡面的細節(當然你要想看,那就看吧)

看看我們的client端

package book.chapter1.webservice;
/**
 * 《建構高性能的大型分布式Java應用》
 *  書中的示例代碼
 *  版權所有   2008---2009
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;


import book.chapter1.webservice.client.Business;
import book.chapter1.webservice.client.BusinessService;


/**
 * 描述:基于Webservice實作的用戶端
 *
 * @author bluedavy 
 * 建立時間: 2009-1-4
 */
public class Client {


  /**
   * @param args
   */
  public static void main(String[] args) throws Exception{
    BusinessService businessService=new BusinessService();
    Business business=businessService.getBusinessPort();
    BufferedReader systemIn=new BufferedReader(new InputStreamReader(System.in));
        while(true){
      String command=systemIn.readLine();
      if(command==null || "quit".equalsIgnoreCase(command.trim())){
        System.out.println("Client quit!");
        try{
          business.echo(command);
        }
        catch(Exception e){
          // IGNORE
        }
        System.exit(0);
      }
      System.out.println(business.echo(command));
    }
  }


}      

另外如果把代碼改成下面的樣子

@WebService(name="Business12",serviceName="BusinessService12",targetNamespace="http://c.b.a/d")

@SOAPBinding(style=SOAPBinding.Style.RPC)

public class BusinessImpl implements Business {

生成的java類就是Business12與BusinessService12位于a.b.c.d包下

參考資料

分布式java應用基礎與實踐

說實話 下面這個關于rmi的分析 我沒有看懂

​​http://guojuanjun.blog.51cto.com/277646/1423392/​​