ITPub博客

首页 > 应用开发 > Java > java与java之间进行交换数据用什么方法

java与java之间进行交换数据用什么方法

Java 作者:lgg78 时间:2013-06-03 01:24:00 0 删除 编辑

    1. 一个VM把数据写到外部数据库里(例如mySql),另一个VM再读取    2. 两个VM都共同访问一个文件(例如/tmp/dataExchange/info.data)注意读写文件时候声明独占,这样防止两个同时读写造成错误    3. EJB核心理念很简单,实践起来很庞大,还要包括容器之类的很哈利波特的概念,你就不用看了,如果有兴趣倒是可以看看jboss, ejb3.0 hibernate之类的    4. 自己写Socket让两个vm进行数据传输。很多程序都有多进程,进程之间交换数据很多用的就是监听本机的某个端口进行的

    我把一个RMI的CLIENT调用类放在SERVER的工程里执行可以正常调用,但是如果把它拷到一个新项目中却无法执行,代码如下:

Java code//服务器代码package server;import java.rmi.Remote;import java.rmi.RemoteException;public interface SayHello extends Remote {// 需要从Remote继承public String sayHello(String info) throws RemoteException; // 需要抛出remote异常n;}package server;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;public class SayHelloImpl extends UnicastRemoteObject implements SayHello {protected SayHelloImpl() throws RemoteException {super();// TODO Auto-generated constructor stub}public String sayHello(String info) throws RemoteException {return "hello=" + info;}}package server;import java.rmi.*;import java.net.*;public class SayRmiServer {public SayRmiServer() {}public static void main(String args[]) throws RemoteException,MalformedURLException {SayHelloImpl add = new SayHelloImpl();Naming.rebind("RMI_Test_2", add);}}//客户端代码,接口文件就不重复拷了package client;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;import server.SayHello;public class SayRmiClient {public SayRmiClient() {}public static void main(String args[]) throws RemoteException,MalformedURLException, NotBoundException {String url = "rmi://110.64.89.140" +"/RMI_Test_2";SayHello add = (SayHello) Naming.lookup(url);String result = add.sayHello("hubin");System.out.println(result);}}执行后抛出如下异常:Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling return; nested exception is:java.lang.ClassNotFoundException: server.SayHello (no security manager: RMI class loader disabled)at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)at java.rmi.Naming.lookup(Unknown Source)at client.SayRmiClient.main(SayRmiClient.java:22)Caused by: java.lang.ClassNotFoundException: server.SayHello (no security manager: RMI class loader disabled)at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)at java.rmi.server.RMIClassLoader$2.loadProxyClass(Unknown Source)at java.rmi.server.RMIClassLoader.loadProxyClass(Unknown Source)at sun.rmi.server.MarshalInputStream.resolveProxyClass(Unknown Source)at java.io.ObjectInputStream.readProxyDesc(Unknown Source)at java.io.ObjectInputStream.readClassDesc(Unknown Source)at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)at java.io.ObjectInputStream.readObject0(Unknown Source)at java.io.ObjectInputStream.readObject(Unknown Source)... 3 more

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23908462/viewspace-1108847/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-05-14