Portable JNDI Syntax lookup for EJB on Glassfish

自作多情 提交于 2019-11-29 13:02:28

Why is the invocation of the bean without "java:" or anything like that?

success:

thufir@dur:~$ 
thufir@dur:~$ glassfish-4.1/glassfish/bin/asadmin list-applications
Nothing to list.
No applications are deployed to this target server.
Command list-applications executed successfully.
thufir@dur:~$ 
thufir@dur:~$ glassfish-4.1/glassfish/bin/asadmin deploy NetBeansProjects/HelloEAR/dist/HelloEAR.ear 
Application deployed with name HelloEAR.
Command deploy executed successfully.
thufir@dur:~$ 
thufir@dur:~$ glassfish-4.1/glassfish/bin/asadmin list-applications
HelloEAR  <ear, ejb>  
Command list-applications executed successfully.
thufir@dur:~$ 
thufir@dur:~$ glassfish-4.1/glassfish/bin/appclient -client NetBeansProjects/RemoteJNDI/dist/RemoteJNDI.jar 
hello world
thufir@dur:~$ 
thufir@dur:~$ 

client code:

package net.bounceme.dur.jndi;

import greetings.GreetingsBeanRemote;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class RemoteJNDI {

    private static final Logger log = Logger.getLogger(RemoteJNDI.class.getName());

    public static void main(String... args) {
        try {
            InitialContext ctx = new InitialContext();
//            Object obj = ctx.lookup("java:global/HelloEAR/HelloEAR-ejb/greetings.GreetingsBeanRemote");
            Object obj = ctx.lookup("greetings.GreetingsBeanRemote");
            GreetingsBeanRemote gbr = (GreetingsBeanRemote) obj;
            System.out.println(gbr.hi());
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }

}

ejb:

package greetings;

import javax.ejb.Stateless;

@Stateless
public class GreetingsBean implements GreetingsBeanRemote {

    @Override
    public String hi() {
        return "hello world";
    }

    @Override
    public String bye() {
        return "bye";
    }

}

interface:

package greetings;

import javax.ejb.Remote;

@Remote
public interface GreetingsBeanRemote {

    public String hi();

    public String bye();
}

server log:

[2014-09-22T17:55:56.091-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756091] [levelValue: 800] [[
  visiting unvisited references]]

[2014-09-22T17:55:56.233-0700] [glassfish 4.1] [INFO] [AS-EJB-00054] [javax.enterprise.ejb.container] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756233] [levelValue: 800] [[
  Portable JNDI names for EJB GreetingsBean: [java:global/HelloEAR/HelloEAR-ejb/GreetingsBean!greetings.GreetingsBeanRemote, java:global/HelloEAR/HelloEAR-ejb/GreetingsBean]]]

[2014-09-22T17:55:56.235-0700] [glassfish 4.1] [INFO] [AS-EJB-00055] [javax.enterprise.ejb.container] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756235] [levelValue: 800] [[
  Glassfish-specific (Non-portable) JNDI names for EJB GreetingsBean: [greetings.GreetingsBeanRemote#greetings.GreetingsBeanRemote, greetings.GreetingsBeanRemote]]]

[2014-09-22T17:55:56.321-0700] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756321] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2014-09-22T17:55:56.341-0700] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756341] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2014-09-22T17:55:56.349-0700] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756349] [levelValue: 900] [[
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]

[2014-09-22T17:55:56.819-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.core] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1411433756819] [levelValue: 800] [[
  HelloEAR was successfully deployed in 842 milliseconds.]]

thufir@dur:~$ 

If you are using Maven as well then check if "-1.0-SNAPSHOT" (or other version/as per your app) is attached at the end of the application-ejb file name as "application-ejb-1.0-SNAPSHOT", The full name would be "java:global/application-ear/application-ejb-1.0-SNAPSHOT/fileName"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!