JBOSS JNDI无状态bean查找字符串

时间:2015-05-30 09:49:41

标签: jboss ejb jndi lookup

在JBOSS 7.1上使用Eclipse工具部署EJB应用程序 什么是要使用的查找字符串的格式。

我看了一下文档,但我真的很困惑

context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

当部署ejb时(例如,当ejb部署到服务器或服务器重新启动时),您应该在输出中看到类似的内容:

2015 Jun 07, 18:07:24.352 (MSC service thread 1-4) INFO  - (AbstractDeploymentUnitService.java:66) start - JBAS015876: Starting deployment of "_ExampleEjbProject.war"
2015 Jun 07, 18:07:24.463 (MSC service thread 1-4) INFO  - (EjbJndiBindingsDeploymentUnitProcessor.java:182) setupJNDIBindings - JNDI bindings for session bean named SayHelloEjb in deployment unit deployment "_ExampleEjbProject.war" are as follows:

    java:global/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.local.SayHelloLocal
    java:app/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.local.SayHelloLocal
    java:module/SayHelloEjb!org.yaorma.examples.ejb.server.local.SayHelloLocal
    java:global/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.remote.SayHelloRemote
    java:app/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.remote.SayHelloRemote
    java:module/SayHelloEjb!org.yaorma.examples.ejb.server.remote.SayHelloRemote
    java:jboss/exported/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.remote.SayHelloRemote

这是可以在jndi查找字符串的不同上下文中使用的字符串列表。在这种情况下,我的项目名称是_ExampleEjbProject,没有子模块,bean类(实现)注释了这个" @Stateless(name =" SayHelloEjb")"给出ejb名称,本地和远程接口是" org.yaorma.examples.ejb.server.local.SayHelloLocal"和" org.yaorma.examples.ejb.server.remote.SayHelloLocal"分别。查找字符串遵循以下模式:

<project_name>/<submodule>/<EJB_name>!<interface_name>.  

从独立应用程序我可以使用此字符串进行连接:

/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.remote.SayHelloRemote

像这样:

    public static void main(String[] args) throws Exception {
        Properties jndiProperties = new Properties();
        String providerURL = "remote://localhost:7447";
        jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, org.jboss.naming.remote.client.InitialContextFactory.class.getName());
        jndiProperties.put(Context.PROVIDER_URL, providerURL);
        jndiProperties.put("jboss.naming.client.ejb.context", true);
        jndiProperties.put(Context.SECURITY_PRINCIPAL, MY_UID);
        jndiProperties.put(Context.SECURITY_CREDENTIALS, MY_PWD);
        InitialContext context = new InitialContext(jndiProperties);
        testLookup(context, "/_ExampleEjbProject/SayHelloEjb!org.yaorma.examples.ejb.server.remote.SayHelloRemote");
        System.out.println("Done!");
    }

    private static void testLookup(InitialContext context, String lookupString) throws Exception {
        Object obj = context.lookup(lookupString);
        SayHelloRemote sayHelloRemote = (SayHelloRemote) obj;
        String msg = sayHelloRemote.sayHello("John");
        System.out.println(msg);
    }