Java RMI-IIOP 入门

 RMI-IIOP出现以前,只有RMI和CORBA两种选择来进行分布式程序设计。RMI-IIOP综合了RMI 
   和CORBA的优点,克服了他们的缺点,使得程序员能更方便的编写分布式程序设计,实现分布式计算。 
   首先,RMI-IIOP综合了RMI的简单性和CORBA的多语言性(兼容性),其次RMI-IIOP克服了RMI只能 
   用于Java的缺点和CORBA的复杂性(可以不用掌握IDL). 
       下面给出了一个非常的简单的RMI-IIOP程序,该程序是在上一个例子(Java2 RMI入门的基础) 
   上修改完成的,可以对比两个程序的区别。        
    
    
   1. 实现远程接口,生成远程对象,存根(Stub)和框架(Skeleton) 
    
        实现远程接口,远程接口告诉JVM:实现了该接口的对象可以远程调用及有哪些方法可以调用。 
      本例子中定义了sayHello()。由于远程调用会涉及到网络通讯,因此这些方法都要抛出RemoteException. 
      远程接口和远程对象可以由A开发,并把远程接口(Hello)d打包分给Client端开发者B。   
         
        建立f: 
mi_iiop目录,把Hello.java和HelloImpl.java拷贝到该目录中。 
         
        // Hello.java 
    package jdeveloper.rmi; 

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

public interface Hello extends Remote {  
     String sayHello() throws RemoteException;  


生成远程对象. 
// HelloImpl.java 
package jdeveloper.rmi_iiop; 

        import javax.naming.*; 

        import java.rmi.RemoteException; 
        import java.rmi.RMISecurityManager; 
        //import java.rmi.server.UnicastRemoteObject; 
        import javax.rmi.PortableRemoteObject; 

        public class HelloImpl extends PortableRemoteObject 
             implements Hello { 
           public HelloImpl() throws RemoteException { 
             super(); 
           } 

           public String sayHello() { 
             return  "Hello World!"; 
           } 

           public static void main(String args[]) {  

           // Create and install a security manager  
             if (System.getSecurityManager() == null) {  
        System.setSecurityManager(new RMISecurityManager());  
             }  
             try {  
        Hello obj = new HelloImpl();  
        // Bind this object instance to the name "HelloServer"       
        //***** old code for rmi 
        // Naming.rebind("HelloServer", obj);  
     
        //***** new code for rmi-iiop           
        Context initialNamingContext = new InitialContext();      
        initialNamingContext.rebind("HelloServer", obj);  
     
        System.out.println("HelloServer bound in registry");  
             } catch (Exception e) {  
        System.out.println("HelloImpl err: " + e.getMessage());  
        e.printStackTrace();  
             }  
          }  
      } 

  存根(Stub)和框架(Skeleton) 
        f: 
        cd  
mi_iiop 
        set classpath=.;%classpath% 
        javac -d . Hello.java 
        javac -d . HelloImpl.java 
        rmic -iiop -d . jdeveloper.rmi_iiop.HelloImpl        
        这一步将生成<_Interface>_stub.class,<_InterfaceImpl>_Tie.class: 
        _Hello_Stub.class和_HelloImpl_Tie.class 
         
    
   2. 实现Client端程序 
        // HelloClient.java         
         package jdeveloper.rmi_iiop; 

         import java.rmi.RMISecurityManager; 
         import java.rmi.Naming; 
         import java.rmi.RemoteException; 
         import java.rmi.NotBoundException; 

         import javax.rmi.PortableRemoteObject; 
         import javax.naming.*; 

         public class HelloClient { 
            public static void main(String args[]) throws Exception{            
           System.setSecurityManager(new RMISecurityManager()); 
           Context initialNamingContext = new InitialContext(); 
           Hello RemoteObj = (Hello) PortableRemoteObject.narrow( 
                  initialNamingContext.lookup("iiop://"+ args[0] +"/HelloServer"), 
                  Hello.class 
                  );    
           System.out.println(RemoteObj.sayHello()); 
           } 
        } 

把HelloClient.java拷贝到目录f: 
mi_iiop中。 
f: 
        cd  
mi_iiop 
        javac  -d . HelloClient.java 
         
   3. 运行程序    
      启动DOS窗口 
      运行 tnameserv     
          
          
      把 server.bat 和 policy 放到f: 
mi_iiop                            
      启动新的DOS窗口 
      运行 server    
          
          
      启动新的DOS窗口 
      把 client.bat 放到f: 
mi_iiop                   
      运行 client hostname 
       
      server.bat 
      set CP=%classpath% 
      set classpath=.;%classpath% 
      java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory   
           -Djava.naming.provider.url=iiop://hjc:900 -Djava.security.policy=policy  jdeveloper.rmi_iiop.HelloImpl 
      set classpath=%CP% 
      client.bat 
      set CP=%classpath% 
      set classpath=.;%classpath% 
      java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory  
           -Djava.naming.provider.url=iiop://hjc:900 -Djava.security.policy=policy  jdeveloper.rmi_iiop.HelloClient %1 
      set classpath=%CP% 
      policy 
      grant { 
// Allow everything for now 
permission java.security.AllPermission; 


阅读更多

rmi-iiop 入门问题,高手赐教啊

05-09

下面四个文件分别是接口 server,client,policyrnimport java.rmi.*;rnpublic interface Hello extends Remotern String sayHello()throws RemoteException;rnrnimport javax.naming.*;rnimport java.rmi.*;rnimport java.rmi.server.UnicastRemoteObject;rnimport javax.rmi.PortableRemoteObject;rnpublic class HelloImpl extends PortableRemoteObject implements Hellorn public HelloImpl()throws RemoteExceptionrn super();rn rn public String sayHello()rn return "Hello World";rn rn public static void main(String arp[])rn if(System.getSecurityManager()==null)rn System.setSecurityManager(new RMISecurityManager());rn rn tryrn Hello obj=new HelloImpl();rn Context initialNamingContext=new InitialContext();rn initialNamingContext.rebind("helloServer", obj);rn System.out.println("HelloServer bound in registry");rn catch(Exception e)rn System.out.println("HelloImpl err:"+e.getMessage());rn e.printStackTrace();rn rn rnrnimport java.rmi.*;rnimport javax.rmi.*;rnimport javax.naming.*;rnpublic class HelloClient rn public static void main(String arp[])throws Exceptionrn System.setSecurityManager(new RMISecurityManager());rn Context initialNamingContext=new InitialContext();rn Hello RemoteObj=(Hello)PortableRemoteObject.narrow(initialNamingContext.lookup("iiop://localhost:7001/helloServer"), Hello.class);rn System.out .println(RemoteObj.sayHello());rn rnrngrantrn//Allow everything for nowrnpermission java.security.AllPermission;rn;rn下面启动服务器顺利rnF:\JAVA APPLICATION\网络\11\RMI-IIOP JNDI\src\jdeveloper\rmi_iiop>java -Djava.narnming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.naming.providernr.url=iiop://localhost:7001 -Djava.security.policy=policy HelloImplrnHelloServer bound in registryrn但是启动client的时候出错rnF:\JAVA APPLICATION\网络\11\RMI-IIOP JNDI\src\jdeveloper\rmi_iiop>java -Djava.narnming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.provider.url=irniop://localhost:7001 -Djava.security.policy=policy HelloClientrnException in thread "main" java.rmi.ConnectException: Connection refused: connect; nested exception is:rn java.net.ConnectException: Connection refused: connectrn at weblogic.iiop.Utils.mapToRemoteException(Utils.java:739)rn at weblogic.iiop.IIOPRemoteRef.invoke(IIOPRemoteRef.java:171)rn at Hello_IIOP_WLStub.sayHello(Unknown Source)rn at HelloClient.main(HelloClient.java:9)rnCaused by: java.net.ConnectException: Connection refused: connectrn at java.net.PlainSocketImpl.socketConnect(Native Method)rn at java.net.PlainSocketImpl.doConnect(Unknown Source)rn at java.net.PlainSocketImpl.connectToAddress(Unknown Source)rn at java.net.PlainSocketImpl.connect(Unknown Source)rn at java.net.SocksSocketImpl.connect(Unknown Source)rn at java.net.Socket.connect(Unknown Source)rn at weblogic.socket.SocketMuxer.newSocket(SocketMuxer.java:311)rn at weblogic.socket.ChannelSocketFactory.createSocket(ChannelSocketFactory.java:82)rn at weblogic.socket.AbstractMuxableSocket.createSocket(AbstractMuxableSocket.java:209)rn at weblogic.socket.AbstractMuxableSocket.createSocket(AbstractMuxableSocket.java:200)rn at weblogic.iiop.MuxableSocketIIOP.newSocket(MuxableSocketIIOP.java:262)rn at weblogic.iiop.MuxableSocketIIOP.createSocket(MuxableSocketIIOP.java:249)rn at weblogic.socket.AbstractMuxableSocket.connect(AbstractMuxableSocket.java:181)rn at weblogic.iiop.MuxableSocketIIOP.connect(MuxableSocketIIOP.java:234)rn at weblogic.iiop.MuxableSocketIIOP.createConnection(MuxableSocketIIOP.java:218)rn at weblogic.iiop.EndPointManager.createEndPoint(EndPointManager.java:450)rn at weblogic.iiop.EndPointManager.findOrCreateEndPoint(EndPointManager.java:237)rn at weblogic.iiop.EndPointManager.findOrCreateEndPoint(EndPointManager.java:254)rn at weblogic.iiop.IIOPRemoteRef.locateIORForRequest(IIOPRemoteRef.java:383)rn at weblogic.iiop.IIOPRemoteRef.locateRequest(IIOPRemoteRef.java:377)rn at weblogic.iiop.IIOPRemoteRef.invoke(IIOPRemoteRef.java:156)rn ... 2 morernrn

关于rmi-iiop和jndi的问题

12-30

我在学习rmi-iiop和jndi中碰到问题,请高手帮忙看一下rn有四个文件rnIPKGenerator.java:rnimport java.rmi.Remote;rnimport java.rmi.RemoteException;rnpublic interface IPKGenerator extends Remotern public long generate()throws RemoteException;rnrnrnPKGenerator.java:rnimport java.rmi.RemoteException;rnimport javax.rmi.PortableRemoteObject;rnpublic class PKGenerator extends PortableRemoteObject implements IPKGeneratorrn public PKGenerator()throws Exception,RemoteExceptionrn super();rn rn public synchronized long generate()throws RemoteExceptionrn return i++;rn rn private static long i=System.currentTimeMillis();rnrnrnStartup.java:rnimport javax.naming.*;rnpublic class Startuprn public static void main(String[] args)throws Exceptionrn PKGenerator generator=new PKGenerator();rn Context ctx=new InitialContext(System.getProperties());rn ctx.rebind("PKGenerator",generator);rn System.out.println("PKGenerator bound to JNDI tree.");rn synchronized(generator)rn generator.wait();rn rn rnrnrnClient.java:rnimport javax.naming.*;rnimport java.rmi.*;rnpublic class Clientrn public static void main(String[] args)throws Exceptionrn Context ctx=new InitialContext(System.getProperties());rn Object remoteObject=ctx.lookup("PKGenerator");rn IPKGenerator generator=(IPKGenerator)javax.rmi.PortableRemoteObject.narrow(remoteObject,IPKGenerator.class);rn System.err.println(generator.generate());rn rnrnrn我用rmic编译了PKGenerator生成了根程序和程序框架,在运行Startup.java时抛出了NoInitialContextException异常,那位高手能告诉我该怎么样编译和执行这几个文件,小弟我刚学j2ee,请多指点,希望能尽量详细点,谢谢!

没有更多推荐了,返回首页