Domanda

Sto seguendo il libro MasteringEJB4thEdition, che ho scaricato dal sito Web di The Server Site.

C'è un semplice esempio di HelloBean, che funziona perfettamente con l'app server GlassFish V3. Lo stesso esempio quando distribuito su JBOSS non riesce a causa della ricerca del nome JNDI.

Esiste una regola su come vengono decisi i nomi di ricerca JNDI in JBOSS se non li forniamo? Ho scoperto mentre cercavo su google che è " nome-file-ear / nome-classe-bean / remoto " ma non funziona per me.

Ecco il bean


   1. package com.hardik.stateless;  
   2. import javax.ejb.Stateless;  
   3. import javax.ejb.Remote  
   4.   
   5.   
   6.   
   7.   
   8. @Stateless  
   9. @Remote(Hello.class)  
  10. public class HelloBean implements Hello {  
  11.   
  12.     public String hello() {  
  13.         System.out.println("hello()");  
  14.         return "Hello, World!";  
  15.     }  
  16.   
  17. }  

Ecco il client che sto usando:


   1. package com.hardik.stateless;  
   2.   
   3. import javax.naming.Context;  
   4. import javax.naming.InitialContext;  
   5.   
   6.   
   7. /** 
   8.  * This is an example of client code which invokes  
   9.  * methods on a simple, remote stateless session bean 
  10.  * @author hardik 
  11.  * 
  12.  */  
  13. public class HelloClient {  
  14.       
  15.     public static void main(String[] args) throws Exception {  
  16.           
  17.         Context ctx = new InitialContext();  
  18.         // works for Glassfish  
  19.         //Hello hello = (Hello) ctx.lookup("com.hardik.stateless.Hello");  
  20.         // doesn't  work for JBOSS  
  21.         Hello hello = (Hello) ctx.lookup("hello-bean/HelloBean/remote");  
  22.           
  23.         System.out.println(hello.hello());  
  24.     }  
  25.   
  26. }  

Ecco l'errore che visualizzo durante l'esecuzione del client


# $ wsrunclient.sh -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces -Djava.naming.provider.url=jnp://localhost:1099  -cp "lib/hello-bean.jar:dist/hello-client.jar:/home/hardik/apps/jboss/client/*"  com.hardik.stateless.HelloClient  
# log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).  
# log4j:WARN Please initialize the log4j system properly.  
# Exception in thread "main" javax.naming.NameNotFoundException: hello-bean not bound  
#         at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)  
#         at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)  
#         at org.jnp.server.NamingServer.getObject(NamingServer.java:785)  
#         at org.jnp.server.NamingServer.lookup(NamingServer.java:396)  
#         at sun.reflect.GeneratedMethodAccessor260.invoke(Unknown Source)  
#         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)  
#         at sun.rmi.transport.Transport$1.run(Transport.java:159)  
#         at java.security.AccessController.doPrivileged(Native Method)  
#         at sun.rmi.transport.Transport.serviceCall(Transport.java:155)  
#         at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)  
#         at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)  
#         at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)  
#         at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
#         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
#         at java.lang.Thread.run(Thread.java:619)  
#         at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)  
#         at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)  
#         at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)  
#         at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)  
#         at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:722)  
#         at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:682)  
#         at javax.naming.InitialContext.lookup(InitialContext.java:392)  
#         at com.hardik.stateless.HelloClient.main(Unknown Source)
È stato utile?

Soluzione

Ho risolto il problema dopo la ricerca. Ho dovuto aggiungere informazioni sullo spazio dei nomi al mio file ejb-jar.xml.

L'ho cambiato da:

<ejb-jar> 
    <enterprise-beans>
    </enterprise-beans> 
</ejb-jar>

a:

   1. <?xml version="1.0" encoding="UTF-8"?>  
   2. <ejb-jar version="3.0"   
   3.     xmlns="http://java.sun.com/xml/ns/javaee"  
   4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
   5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
   6.     http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">  
   7. </ejb-jar>  

Ho trovato la risposta qui: http://www.jboss.org/index.html?module= BB & amp; op = viewtopic & amp; t = 157022

Altri suggerimenti

Penso che devi essere aggiunto Seguendo Proprietà in Client / log4j.properties :

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p
%c{1}:%L - %m%n
log4j.rootLogger=INFO, stdout

Non sono sicuro di JBoss 5, ma in JBoss 4 la console JMX ha un MBean chiamato JNDIView, che ha operazioni per scaricare l'intero albero JNDI sulla console. Se è ancora presente in JBoss 5, dovresti riuscire a trovare il tuo bean lì.

Ho controllato la vista jndi con la console jmx, il bean è registrato solo internamente, non nel Global JNDI. Vedo il seguente messaggio al momento della distribuzione ...


12:15:44,016 INFO  [JBossASKernel] Added bean(jboss.j2ee:ear=hello-bean.ear,jar=hello-bean.jar,name=HelloBean,service=EJB3) to KernelDeployment of: hello-bean.jar
12:15:44,048 INFO  [SessionSpecContainer] Starting jboss.j2ee:ear=hello-bean.ear,jar=hello-bean.jar,name=HelloBean,service=EJB3
12:15:44,048 INFO  [EJBContainer] STARTED EJB: com.hardik.mejb.HelloBean ejbName: HelloBean
12:15:44,048 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:


12:15:44,078 WARN  [WebServiceDeployerEJB] Ingore ejb deployment with null classname: org.jboss.metadata.ejb.jboss.JBossSessionBeanMetaData@bebe0782{HelloBean}

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top