基于java自身技术实现消息方式的系统间通信
2015-11-13 18:18
1076 查看
这篇博客基本照搬了分布式java应用基础与实践一书的内容
java自带的远程调用分两种一种是rmi,一种是webservice
我们先看rmi(remote method invoke)#
我在两台机子上运行,在我自己机子上运行server端,在朋友电脑上运行的是client端,他那边有Clinet.java与Business.java#运行时报ClassNotFoundException#
把BusinessImpl也给他发过去后,就一切ok了#
他那么直接打印出一个9,我这边打印出a+b#
可见计算的过程确实是在我这边的#
有了上面三个类之后,我们可以通过jdk bin目录下的wsimport命令来生成辅助调用代码,如下:
之后,我们发现jdk的工具给我们在book\chapter1\webservice\client下产生了两个类,Business.java,BusinessService.java#
这个两个类,我们其实可以不用看里面的细节(当然你要想看,那就看吧)
看看我们的client端
另外如果把代码改成下面的样子
@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包下
说实话 下面这个关于rmi的分析 我没有看懂
http://guojuanjun.blog.51cto.com/277646/1423392/
java自带的远程调用分两种一种是rmi,一种是webservice
我们先看rmi(remote method invoke)#
使用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命令来生成辅助调用代码,如下:
之后,我们发现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/
相关文章推荐
- 【leetcode】Find Minimum in Rotated Sorted Array
- MyEclipse中的项目在Manage Deployments窗口中找不到
- Eclipse中的快捷键
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常的处理方法
- Eclipse安装SVN插件
- Java NIO教程(九)ServerSocketChannel
- Spring 读书笔记-----使用Spring容器(一)
- Java每日登陆 和 月卡功能
- Spring读书笔记-----使用Spring容器(二)
- Spring读书笔记-----Spring的Bean之Bean的基本概念
- Java NIO教程(八)SocketChannel
- java递归解迷宫 求所有解
- Debug日志(一)——session失效引发struts死循环
- 【leetcode】Convert Sorted Array to Binary Search Tree
- 没有纳入spring管理的类如何注入spring管理的对象
- 解析Java的InputStream类并借助其读取ppt文件
- Java NIO教程(七)FileChannel
- spring事务管理 详解
- Gson+JsonPath+泛型 java json解析工具类
- jsp+servlet+javabean