和风细雨

世上本无难事,心以为难,斯乃真难。苟不存一难之见于心,则运用之术自出。

在Weblogic8上注册并启动RMI程序.

.做两个类Ruler和RulerImpl.
import java.rmi.Remote;

public interface Ruler extends Remote {
    
public String getLength(String str) throws java.rmi.RemoteException;
}

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

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

public class RulerImpl extends UnicastRemoteObject implements Ruler {
    
public RulerImpl() throws RemoteException{
        
super();
    }


    
public String getLength(String str) throws java.rmi.RemoteException {
                 
// 这里用Sb是因为weblogic的1.4的jdk不认StringBuilder
        StringBuffer sb=new StringBuffer();
        sb.append(
"String:");
        sb.append(str);
        sb.append(
"'s length=");
        sb.append(str.length());
        
        
return sb.toString();
    }

    
    
public static void main(String[] args){
        
try{
            RulerImpl rulerImpl
=new RulerImpl();
            
            Context ctx
=new InitialContext();
            ctx.bind(
"StringRuler", rulerImpl);
        }

        
catch(Exception ex){
            ex.printStackTrace();
        }

    }

}

2.用rmic编译Ruler.class和RulerImpl.class 以生成桩类RulerImpl_Stub.class.
c:>C:\jdk1.5.0_09\bin\rmic RulerImpl.

3.将生成的RulerImpl_Stub.class以及原有的Ruler.class和RulerImpl.class拷贝到服务器的目标域中,本人机器上的是C:\bea\user_projects\domains\mydomain

4.通过http://localhost:7001/console 进入Weblogic控制台,并按 yourdomain->Deployment->Startup&Shutdown->Configure a New Startup Class 注册启动类.完毕后结果如下图:


5.重新启动Server(Start Server)

6.通过客户端测试一下:
import java.util.Hashtable;

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

public class RulerClient{
    
public static void main(String[] args) throws Exception{
        Hashtable env
=new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, 
"weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL,
"t3://localhost:7001");
        
        InitialContext ctx
=new InitialContext(env);
        
        Object o
=ctx.lookup("StringRuler");
        Ruler ruler
=(Ruler)o;
        System.out.println(ruler.getLength(
"123"));
    }

}

测试结果为:
String:123's length=3

代码下载(rmic 目录中包括三个已生成类):
http://www.blogjava.net/Files/sitinspring/RmiExample20071106220750.rar

posted on 2008-02-22 11:25 和风细雨 阅读(218) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: