2017-11-13 14 views
1

単純なステートレスセッションBeanを使用しようとしていますが、Beanにアクセスする際に問題があります。私はClassCastExceptionを持っています!EJB3 ctx.lookup ClassCastException

私はMavenのEJBモジュールを持っている含まれています。このプロジェクトは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 

に配備されている

//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"; 
    } 
} 

私のクライアントは、Mavenのjavaアプリケーション

である私は私のクライアントに追加EJBプロジェクトを依存関係としてpom.xml

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

とJBoss-のejb-クライアント

<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にアクセスする際に問題がどこにあるのか分かりませんか?

事前に感謝

答えて

0

ClassCastExceptionjavax.naming.Referenceからは、あなたのクライアントのクラスパス上のObjectFactoryクラスを持っていないサインです。

maven依存関係にjbossall-clientが含まれていますが、MANIFEST.MFに表示されているすべてのjarsもjbossall-client.jarのクライアントのクラスパスに必要です。