簡體   English   中英

互聯網上的rmi iiop

[英]rmi iiop over the internet

我試圖使Oracle RMI-IIOP示例正常工作,但是在Netbeans中這樣做卻遇到了問題。

我的代碼如下:

介面

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

public interface HelloInterface extends Remote {
    public void sayHello(String from) throws RemoteException;
}

接口實現

import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;

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

    public void sayHello(String from) throws RemoteException
    {
        System.out.println("Hello from " + from + "!!!");
        System.out.flush();
    }
}

服務器主

import javax.naming.Context;
import javax.naming.InitialContext;

public class HelloServer {

    public static void main(String[] args) {
        try {

            System.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
            System.setProperty("java.naming.provider.url", "iiop://localhost:1050");
            // Step 1: Instantiate the Hello servant
            HelloImpl helloRef = new HelloImpl();
            // Step 2: Publish the reference in the Naming Service
            // using JNDI API
            Context initialNamingContext = new InitialContext();
            initialNamingContext.rebind("HelloService", helloRef);
            System.out.println("Hello Server: Ready...");
        } catch (Exception e) {
            System.out.println("Trouble: " + e);
            e.printStackTrace();
        }
    }
}

和客戶代碼

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class HelloRMIIIOPClient {

    public static void main(String[] args) {
        Context ic;
        Object objref;
        HelloInterface hi;
        try {
            ic = new InitialContext();
            System.setProperty("classpath", ".");
            System.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
            System.setProperty("java.naming.provider.url", "iiop://localhost:1050");
        // STEP 1: Get the Object reference from the Name Service
        // using JNDI call.
            objref = ic.lookup("HelloService");
            System.out.println("Client: Obtained a ref. to Hello server.");
        // STEP 2: Narrow the object reference to the concrete type and
        // invoke the method.
            hi = (HelloInterface) PortableRemoteObject.narrow(
                objref, HelloInterface.class);
            hi.sayHello( " MARS " );

        } catch( Exception e ) {
            System.err.println( "Exception " + e + "Caught" );
            e.printStackTrace( );
            return;
        }
    }
}

我已經使用rmic生成存根和存根,並且服務器部分的代碼工作正常,但是當我運行客戶端代碼時,我得到了:

Exception javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initialCaught
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at hellormiiiopnew.HelloRMIIIOPClient.main(HelloRMIIIOPClient.java:33)
BUILD SUCCESSFUL (total time: 0 seconds)

盡管這只是我的工作方式,但最終代碼將用作模板,因此我可以通過Internet在分布式系統上傳輸簡單對象。 我一直在嘗試使RMI在Internet上無法成功運行,因此這是我的最新嘗試。 任何幫助,尤其是示例,將不勝感激。

您需要創建InitialContext. 之前設置系統屬性InitialContext.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM