1

我正在尝试远程方法访问的 EJB 3 示例。一个简单的例子,我正在编写代码并将其放入 jar 中。我已经在 C:\jboss-4.2.3.GA\server\default\deploy 中的 jboss 服务器中部署了 jar,jar 名称是01addproject.jar(从 eclipse 导出为 EJB JAR 文件)

我正在使用另一个项目来编写客户端代码,查找服务并以简单的方式使用远程方法。

我觉得这个类没有在 RMI 注册表中注册,当我将客户端代码作为 PSVM 程序运行时,它会给出 NameNotFoundException。

这是错误:

javax.naming.NameNotFoundException: AddRemoteImpl not bound
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
    at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
    at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)

(因空间不足而减少)

封装结构: 客户端和 EJB 代码的包结构

AddRemote.java 中的代码

package com.cluster.remote;

import javax.ejb.Remote;

@Remote
public interface AddRemote {

    void m1();
    int add(int x, int y);

}

AddRemoteImpl.java 中的代码

package com.cluster.remote;

import javax.ejb.Stateless;

@Stateless
public class AdddRemoteImpl implements AddRemote {

    @Override
    public void m1() {
        System.out.println("inside m1");
    }

    @Override
    public int add(int x, int y) {

        return x + y;
    }

}

Client.java 中的客户端代码(psvm 程序)

package clientcode;

import java.util.Hashtable;

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

import com.cluster.remote.AddRemote;

public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {

         try {
            AddRemote remote =  (AddRemote) getInitialContext().lookup("AddRemoteImpl/remote");

            System.out.println("The alue form the session bean is " + remote.add(3, 5));
            System.out.println("Added Successfully");



        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Context getInitialContext() throws NamingException{

        Hashtable hashtable = new Hashtable();
        hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        hashtable.put(Context.PROVIDER_URL, "localhost:1099");
        Context context = new InitialContext(hashtable);


        return context;
    }

}
4

1 回答 1

1

EJB 在绑定时没有被导出,所以它被序列化为它本身,而不是它的存根,所以客户端查找无法加载 AddRemoteImpl 类,这是合理的,因为客户端不应该拥有它。它的构建/声明/部署方式有问题,无法进一步帮助。

于 2012-03-14T10:53:31.000 回答