WebLogic RMI


 

The WebLogic server includes RMI as an integral service. Thus, Java servlets and applications that wish to support client interactions through this mechanism may do so without having to rely on a separate RMI Registry service.

WebLogic provides a developer's guide for its version of RMI that includes the APIs for its rmi packages.

There are several key steps in creating WebLogic RMI applications or converting conventional Java RMI programs to WebLogic ones.

  1. Replace all references to sun's RMI packages with the corresponding WebLogic RMI packages.
  2. Use WebLogic's version of rmic, referred to as weblogic.rmic, to produce the stub and skeleton files.
  3. Deploy the servlet and applet files, if that is the architecture being used.

Because of the greater function provided by the WebLogic server, there are several nuances involved in the above.

Below is an update of the RMIHelloWorld servlet and applet example discussed with respect to Sun's RMI. Key changes are highlighted.


Interface

package jbsRMIHello;

public interface jbsRMIHelloInt extends weblogic.rmi.Remote {

    String sayHello() throws weblogic.rmi.RemoteException;

}

Implementation

package jbsRMIHello;

import weblogic.rmi.*;
import weblogic.rmi.server.UnicastRemoteObject;
import javax.servlet.*;

public class jbsRMIHelloImpl extends weblogic.rmi.server.UnicastRemoteObject implements jbsRMIHelloInt {

    public jbsRMIHelloImpl ( ) throws weblogic.rmi.RemoteException  {

        super ();

    }

    public String sayHello  ()  throws weblogic.rmi.RemoteException  {

        return ("Hello, World, from RMI Servlet!");

    }  // end sayHello


}  // end jbsRMIHelloImpl

 

Servlet

package jbsRMIHello;

import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import weblogic.rmi.*;

public class jbsRMIHelloServlet extends HttpServlet {


//*****  RMI Servlet
//*****  Expects RMI references on port: 7001

protected boolean firsttime = true;
protected String host;
protected int port;
protected String message;

protected String rmiString;

jbsRMIHelloImpl obj;

    public void doGet (HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException
	    {

        if ( firsttime )  {

            firsttime = false;
            setHost ( req );
            rmiString = "//" + host + ":7001/jbsRMIObj";

            try {
                obj = new jbsRMIHelloImpl ();
                Naming.rebind(rmiString, obj);
                message = ("jbsRMIHelloImpl bound in WebLogic server: port 7001");
            } catch (Exception e) {
                message = ("jbsRMIHelloImpl binding err: " + e.getMessage());
                e.printStackTrace();
            }

            returnHTML ( resp );
	        message = "jbsRMIHelloImpl already bound";

        }  // end firsttime
        else  {
            returnHTML ( resp );
        }  // end else

    }  // end doGet


    public void returnHTML ( HttpServletResponse resp )  {

    ServletOutputStream out;

        try  {

	        resp.setContentType("text/html");
	        out = resp.getOutputStream();

	        out.println("<html>");
	        out.println("<head><title>jbsServerThreadedCO</title></head>");
	        out.println("<body>");
	        out.println("<center><font color=AA0000>");
	        out.println("<h3>jbsRMIHelloServlet Running on " + host + ":7001</h3>");
	        out.println("<h3>Applet Tag for jbsRMIHelloApplet Returned from WebLogic Server\n</h3>");
	        out.println("<h3>" + message + "</h3>");
	        out.println("</font></center>");
	        out.println("<applet code='jbsRMIHello.jbsRMIHelloApplet.class' codebase='/classes/' width=500 height=400>");
	        out.println("<param name=rmiString value=" + rmiString +">");
	        out.println("</applet>");
	        out.println("</body>");
	        out.println("</html>");
	        out.flush();
	        out.close();
	    }  // end try
        catch ( IOException except)  {
        }  // end catch

    }  // end returnHTML

  public void setHost ( HttpServletRequest res )  {

        try {
            InetAddress here = InetAddress.getLocalHost ();
            host = here.getHostAddress ();
        }  // end try
        catch (java.net.UnknownHostException e)
        { ;}

     }  // end setHost

}  // end jbsRMIHelloServlet

 


Stub

package jbsRMIHello;

/* This is a generated file. Do not edit this file */

/**
* This code was automatically generated at 5:35:11 PM on Mar 29, 2000 from
* jbsRMIHelloImpl
* by weblogic.rmi.rmic.Remote2Java -- do not edit.
*
* @version 4.5.1 09/30/1999 17:41:18 #53704
* @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
*/
public class jbsRMIHelloInt_WLStub extends weblogic.rmi.extensions.Stub
implements jbsRMIHelloInt,
weblogic.rmi.Remote, java.io.Externalizable
{


private static java.lang.reflect.Method $method_0;

java.lang.reflect.Method[] methods = {$method_0 };
private static volatile java.lang.Object lock = new java.lang.Object();
private static volatile boolean initialized = false;
private static void ensureInitialized() {
if (initialized) return;
try {
synchronized (lock) {
if (initialized) return;
initialized = true;
}
$method_0 = jbsRMIHelloInt.class.getMethod("sayHello", new java.lang.Class[] {});
} catch (java.lang.NoSuchMethodException e) {
throw new java.lang.NoSuchMethodError(e.toString());
}
}

private final static boolean verbose_methods = false;

private final static int WEBLOGIC_TEMPLATE_VERSION = 4;

protected final int get_weblogic_template_version() { return WEBLOGIC_TEMPLATE_VERSION; }


public java.lang.reflect.Method[] get_weblogic_methods() {
ensureInitialized();
return methods;
}

// Constructors
public jbsRMIHelloInt_WLStub( ) {
super();
ensureInitialized();
}

public jbsRMIHelloInt_WLStub(weblogic.rmi.extensions.RemoteObjectReference ror) throws weblogic.rmi.RemoteException {

super(ror);
ensureInitialized();
}

// Methods from remote interfaces


// Implementation of sayHello
public java.lang.String sayHello( )
throws weblogic.rmi.RemoteException
{
if (verbose_methods)
java.lang.System.err.println(
"MI:\t" + "sayHello" + "\t" + this + "\t" +
java.lang.System.currentTimeMillis() + "\t");

weblogic.rmi.extensions.Request request = null;
try {
try {
request = ror.getRequest($method_0, 0);

} catch (java.io.IOException e) {
throw new weblogic.rmi.MarshalException("error marshalling arguments", e);
}

weblogic.rmi.extensions.WRMIInputStream response = request.sendReceive();

java.lang.String result;
try {
result = ( java.lang.String ) response.readString();
} catch (java.io.IOException ioe) {
throw new weblogic.rmi.UnmarshalException("Unmarshalling return", ioe);
}

return result;

} catch (java.lang.Throwable thr) {
if (thr instanceof java.lang.Error) throw (java.lang.Error)thr;
if (thr instanceof java.lang.RuntimeException) throw (java.lang.RuntimeException)thr;
if (thr instanceof weblogic.rmi.RemoteException) throw (weblogic.rmi.RemoteException)thr;

throw new weblogic.rmi.UnexpectedException("Undeclared checked exception",thr);
}

}


}

Applet

package jbsRMIHello;

import java.awt.*;
import java.applet.*;
import weblogic.rmi.*;

public class jbsRMIHelloApplet extends Applet {

        String message = "";
        String rmiString;
        String defaultRMIString = "//jbspc.cs.unc.edu:7001/jbsRMIObj";

        public void init() {

            try {
                setRMIString ();
                jbsRMIHelloInt obj = (jbsRMIHelloInt)Naming.lookup("//jbspc.cs.unc.edu:7001/jbsRMIObj");
                message = obj.sayHello();
            } catch (Exception e) {
                System.out.println("jbsRMIHelloApplet exception: " + e.getMessage());
                e.printStackTrace();
            }

        }  // end init

        public void paint(Graphics g) {
            g.setColor ( new Color (196, 0, 0 ) );
            g.setFont ( new Font ( "Helvetica", Font.BOLD, 24 ) );
            g.drawString(message, 50, 50);
        }

        private void setRMIString ()  {
            rmiString = getParameter ( "rmiString" );
            if (rmiString == null) rmiString = defaultRMIString;
        }  // end  setRMIString

}  // end jbsRMIHelloApplet

Run the Servlet ( jbspc )