EJB3 ctx.lookup ClassCastException

时间:2017-11-13 10:43:32

标签: java-ee netbeans-8 ejb-3.1 jboss6.x

我正在尝试使用一个简单的无状态sesssion bean,但是我有问题可以访问我的Bean。我得到了ClassCastException!

我有一个maven EJB模块包含:

//My remote interface
@Remote
public interface ServiceRemote {
    public String getBeanName();
}

//My local interface
@Local
public interface ServiceLocal {
    public String getBeanName();
}

//My Session bean
@Stateless
public class Service implements ServiceLocal, ServiceRemote {
    public String getBeanName() {
        return "Service"; 
    }
}

该项目部署在Jboss AS 6上

11:03:21,159 INFO  [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
11:03:21,887 INFO  [org.jboss.ejb3.session.SessionSpecContainer] Starting jboss.j2ee:jar=mavenproject1-1.0-SNAPSHOT.jar,name=Service,service=EJB3
11:03:21,887 INFO  [org.jboss.ejb3.EJBContainer] STARTED EJB: metier.Service ejbName: Service
11:03:21,891 INFO  [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

    Service/remote - EJB3.x Default Remote Business Interface
    Service/remote-metier.ServiceRemote - EJB3.x Remote Business Interface
    Service/local - EJB3.x Default Local Business Interface
    Service/local-metier.ServiceLocal - EJB3.x Local Business Interface

我的客户端是maven java应用程序

我在我的客户端pom.xml中添加了EJB项目作为依赖项:

    <dependency>
        <groupId>org</groupId>
        <artifactId>mavenproject1</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

和jboss-ejb-client

    <dependency>
        <groupId>jboss</groupId>
        <artifactId>jbossall-client</artifactId>
        <version>4.2.2.GA</version>
    </dependency>

我的主要课程:

public class Main {

    public static void main(String[] args) {
        try {
            Properties properties = new Properties();
            properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
            properties.setProperty(Context.PROVIDER_URL, "localhost:1099");

            Context ctx = new InitialContext(properties);

            Object obj = (Object) ctx.lookup("Service/remote");

            System.out.println("service.getClass(): " + obj.getClass());
            System.out.println("service.toString(): " + obj.toString());

            ServiceRemote myService = (ServiceRemote) obj;
            myService.getBeanName();
        } catch (NamingException ex) {
            ex.printStackTrace();
        }

    }
}

当我运行我的客户端时,我有java.lang.ClassCastException:

我的控制台出局:

service.getClass(): class javax.naming.Reference
service.toString(): Reference Class Name: Proxy for: metier.ServiceRemote
Type: ProxyFactoryKey
Content: ProxyFactory/mavenproject1-1.0-SNAPSHOT/Service/Service/remote
Type: EJB Container Name
Content: jboss.j2ee:jar=mavenproject1-1.0-SNAPSHOT.jar,name=Service,service=EJB3
Type: Proxy Factory is Local
Content: false
Type: Remote Business Interface
Content: metier.ServiceRemote
Type: Remoting Host URL
Content: socket://127.0.0.1:3873/?timeout=300000

Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference cannot be cast to metier.ServiceRemote

有人可以帮助我吗,我不知道访问我的会话bean有什么问题?

提前致谢

1 个答案:

答案 0 :(得分:0)

来自ClassCastException

javax.naming.Reference是您在客户端ClassPath上没有ObjectFactory课程的标志。

我发现你已经在你的maven依赖项中包含了jbossall-client,但是你还需要在MANIFEST.MF jbossall-client.jar中列出的所有jars,在客户端的类路径中。 / p>