java与java之间的相互调用
1 RMI: remote method invoaction 远程方法调用
2 两个位于不同java虚拟机上的程序互相请求访问
3 RMI的参数和返回值:
--(自动化)传递远程对象,实现remote接口
--(自动化)传递可序列化对象,实现(Serializable接口),通过网络将其副本拷 贝到另一台机器上
4 RMI优点:
(1) 跨平台分布式对象调用
(2) 完全对象支持
(3) 安全策略
RMI缺点:
(1) 双方必须是java语言实现
(2) 不如消息传递方便
5 rmi调用示意图
6 代码示例
(1) client端
package warehouse1;
import java.rmi.*;
/**
* 接口类
*
*/
public interface Warehouse extends Remote
{
double getPrice(String description) throws RemoteException;
}
package warehouse1;
import java.rmi.*;
import java.util.*;
import javax.naming.*;
public class WarehouseClient
{
public static void main(String[] args) throws NamingException, RemoteException
{
Context namingContext = new InitialContext();
//开始查找RMI注册表上有哪些绑定的服务
System.out.print("RMI 注册表绑定列表: ");
Enumeration<NameClassPair> e = namingContext.list("rmi://127.0.0.1:8001/");
while (e.hasMoreElements())
System.out.println(e.nextElement().getName());
//获取某一个地址上的服务类
String url = "rmi://127.0.0.1:8001/warehouse1";
Warehouse centralWarehouse = (Warehouse) namingContext.lookup(url);
//System.out.println(centralWarehouse.getClass().getName());
//输入参数 取得结果
String descr = "面包机";
double price = centralWarehouse.getPrice(descr);
System.out.println(descr + ": " + price);
}
}
(2) 服务端
package warehouse1;
import java.rmi.*;
/*服务端对应接口*/
public interface Warehouse extends Remote
{
double getPrice(String description) throws RemoteException;
}
package warehouse1;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
/*服务端实现类*/
public class WarehouseImpl extends UnicastRemoteObject implements Warehouse
{
private Map<String, Double> prices;
public WarehouseImpl() throws RemoteException
{
//物品列表
prices = new HashMap<>();
prices.put("面包机", 24.95);
prices.put("微波炉", 49.95);
}
public double getPrice(String description) throws RemoteException
{
Double price = prices.get(description);
return price == null ? 0 : price;
}
}
package warehouse1;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import javax.naming.*;
/**
* 产生WarehouseImpl对象,并进行注册在8001端口,对外提供服务
*
*/
public class WarehouseServer
{
public static void main(String[] args) throws Exception
{
System.out.println("产生服务器对象");
WarehouseImpl centralWarehouse = new WarehouseImpl();
System.out.println("将服务器对象绑定在8001端口,对外提供服务");
LocateRegistry.createRegistry(8001);//定义端口号
Naming.rebind("rmi://127.0.0.1:8001/warehouse1", centralWarehouse);
System.out.println("等待客户端的调用");
}
}
webservice混合编程
1 由万维网联盟(W3C,World wide Web consortium)提出
2 消除语言差异,平台差异,协议差异和数据结构差异,成为不同构件模型和异构之间的系统继承技术
3 webservice是为了实现跨网络操作而设计的软件系统
4 webService是为了实现跨网络操作而设计的软件系统,提供了相关的应用接口,其他应用以预先指定的方式来与webService进行交互.
5 webService有三个模块类似于java的rmi,服务提供者,服务注册中心,服务请求者
6 webservice就类似我们本地调用别人远程提供的服务,输入相对应的参数返回一些我们需要的东西,如查询天气预报,查询手机号位置!
7 java调用java提供的wsimport工具,自动生成调用webservice服务
该工具在java安装目录的/bin目录下
根据wsdl文档自动生成客户端中间代码
wsimport -keep -verbose
http://ws.webxml.com.cn/WebService/MobileCodeWS.asmx?WSDL
8 代码示例
java调用Web service
–调用wsimport自动生成中间件代码
–提供相应参数
–返回相应结果
package edu.ecnu;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.MobileCodeWS;
import cn.com.webxml.MobileCodeWSSoap;
public class WebServiceClient {
/**
* use wsimport to parse wsdl
* wsimport -keep -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
* copy the generated class to this project, and invoke them
*
* 调用手机号查询web service
* @param args
*/
public static void main(String[] args) {
MobileCodeWS mobileCodeWS = new MobileCodeWS();
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
String tel=mobileCodeWSSoap.getMobileCodeInfo("18710479266",null); //修改为有效号码
ArrayOfString res = mobileCodeWSSoap.getDatabaseInfo();
System.out.println(res.getString());
System.out.println(tel);
}
}
9 http://www.webxml.com.cn/zh_cn/index.aspx web服务提供网站