JNDI查找远程无状态bean(hello world)

时间:2014-09-20 15:35:12

标签: java java-ee netbeans glassfish ejb

对于在localhost上连接到Glassfish的两个Java SE客户端,只有the ACC client examplethe Netbeans Creating and Running an Application Client on the GlassFish Server tutorial非常相似,才会连接。通过CORBA连接的RemoteJNDI与显式JNDI连接参数一起获得NamingException。如何RemoteJNDI连接到localhost上的Netbeans,正确查找bean,并执行bean?

RemoteJNDI从Glassfish appclient运行。 Glassfish显示:

thufir@dur:~/NetBeansProjects/HelloClient$ 
thufir@dur:~/NetBeansProjects/HelloClient$ /home/thufir/glassfish-4.1/glassfish/bin/asadmin list-applications
HelloEJB     <ejb>        
Command list-applications executed successfully.
thufir@dur:~/NetBeansProjects/HelloClient$ 

在ACC客户端示例上运行ant clean;ant run可以提供良好的输出:

-run:
     [java] hello world

run:

BUILD SUCCESSFUL
Total time: 26 seconds
thufir@dur:~/NetBeansProjects/HelloClient$ 

(这也将HelloClient部署到Glassfish。)

但是,尝试使用appclient和JNDI查找复制该输出会导致NamingException: Lookup failed for 'hello.HelloBean',而ACC客户端能够通过ACC查找该bean。为什么不能RemoteJNDI查找bean?堆栈跟踪:

thufir@dur:~/NetBeansProjects/HelloClient$ /home/thufir/glassfish-4.1/glassfish/bin/appclient -client /home/thufir/NetBeansProjects/RemoteJNDI/dist/RemoteJNDI.jar 
Sep 20, 2014 8:05:12 AM net.bounceme.dur.jndi.RemoteJNDI main
INFO: {org.omg.CORBA.ORBInitialPort=4848, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1}
org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Connection abort  vmcid: OMG  minor code: 8 completed: Maybe
    at com.sun.proxy.$Proxy35.connectionAbort(Unknown Source)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrategy(ConnectionImpl.java:1288)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.doWork(ConnectionImpl.java:846)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Caused by: org.omg.CORBA.COMM_FAILURE: FINE: 00410011: IOException received when reading from connection SocketOrChannelConnectionImpl[ java.nio.channels.SocketChannel[connected local=/127.0.0.1:59218 remote=localhost/127.0.0.1:4848] ESTABLISHED true true]  vmcid: OMG  minor code: 11  completed: No
    at com.sun.proxy.$Proxy35.ioexceptionWhenReadingConnection(Unknown Source)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(ConnectionImpl.java:1475)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrategy(ConnectionImpl.java:1242)
    ... 3 more
Caused by: java.io.IOException: End-of-stream
    at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(ConnectionImpl.java:1468)
    ... 4 more
Sep 20, 2014 8:05:46 AM net.bounceme.dur.jndi.RemoteJNDI main
SEVERE: null
javax.naming.NamingException: Lookup failed for 'hello.HelloBean' in SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=4848, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1} [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=4848, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Connection abort  vmcid: OMG  minor code: 8 completed: Maybe]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at net.bounceme.dur.jndi.RemoteJNDI.main(RemoteJNDI.java:17)
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=4848, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Connection abort  vmcid: OMG  minor code: 8 completed: Maybe]
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:334)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:477)
    ... 3 more
Caused by: org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Connection abort  vmcid: OMG  minor code: 8 completed: Maybe
    at com.sun.proxy.$Proxy35.connectionAbort(Unknown Source)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrategy(ConnectionImpl.java:1288)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.doWork(ConnectionImpl.java:846)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Caused by: org.omg.CORBA.COMM_FAILURE: FINE: 00410011: IOException received when reading from connection SocketOrChannelConnectionImpl[ java.nio.channels.SocketChannel[connected local=/127.0.0.1:59218 remote=localhost/127.0.0.1:4848] ESTABLISHED true true]  vmcid: OMG  minor code: 11  completed: No
    at com.sun.proxy.$Proxy35.ioexceptionWhenReadingConnection(Unknown Source)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(ConnectionImpl.java:1475)
    at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrategy(ConnectionImpl.java:1242)
    ... 3 more
Caused by: java.io.IOException: End-of-stream
    at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(ConnectionImpl.java:1468)
    ... 4 more

thufir@dur:~/NetBeansProjects/HelloClient$ 

ACC客户端代码:

package helloclient;

import hello.HelloBeanRemote;
import javax.ejb.EJB;

public class Main {
    @EJB
    private static HelloBeanRemote helloBean;

    public static void main(String... args) {
        System.out.println(helloBean.Hi());
    }

}

界面:

package hello;

import javax.ejb.Remote;

@Remote
public interface HelloBeanRemote {

    public String Hi();

    public String Bye();
}

EJB:

package hello;

import javax.ejb.Stateless;

@Stateless
public class HelloBean implements HelloBeanRemote {

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

    @Override
    public String Bye() {
        return "goodbye";
    }

}

RemoteJNDI应明确具有与HelloClient相同的配置,ACC客户端, implicitly has. Why is the ACC client able to execute the bean, while RemoteJNDI`不能?

RemoteJNDI的代码:

package net.bounceme.dur.jndi;

import hello.HelloBean;
import java.util.logging.Level;
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();
            log.info(ctx.getEnvironment().toString());
            HelloBean bean = (HelloBean) ctx.lookup("hello.HelloBean");
            log.info(bean.Hi());
        } catch (NamingException ex) {
            Logger.getLogger(RemoteJNDI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

对于在localhost上运行的Glassfish,连接参数是否正确?

thufir@dur:~/NetBeansProjects/HelloClient$ 
thufir@dur:~/NetBeansProjects/HelloClient$ /home/thufir/glassfish-4.1/glassfish/bin/appclient -client /home/thufir/NetBeansProjects/RemoteJNDI/dist/RemoteJNDI.jar 
Sep 20, 2014 8:05:12 AM net.bounceme.dur.jndi.RemoteJNDI main
INFO: {org.omg.CORBA.ORBInitialPort=4848, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1}

----------------------------------------------- -------编辑-----------------------------

迹:

thufir@dur:~$ 
thufir@dur:~$ /home/thufir/glassfish-4.1/glassfish/bin/appclient -client /home/thufir/NetBeansProjects/RemoteJNDI/dist/RemoteJNDI.jar 
javax.naming.NamingException: Lookup failed for 'HelloBeanRemote' in SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1} [Root exception is javax.naming.NameNotFoundException: HelloBeanRemote not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at net.bounceme.dur.jndi.RemoteJNDI.main(RemoteJNDI.java:18)
Caused by: javax.naming.NameNotFoundException: HelloBeanRemote not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:66)
    at com.sun.enterprise.naming.impl.RemoteSerialContextProviderImpl.lookup(RemoteSerialContextProviderImpl.java:109)
    at sun.reflect.GeneratedMethodAccessor113.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
thufir@dur:~$ 
thufir@dur:~$ 

ex.printstacktrace;

package net.bounceme.dur.jndi;

import hello.HelloBean;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class RemoteJNDI {

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

    public static void main(String... args) {
        try {
            InitialContext ctx = new InitialContext();
            log.fine(ctx.getEnvironment().toString());
            Object obj = ctx.lookup("HelloBeanRemote");
            HelloBean bean = (HelloBean) PortableRemoteObject.narrow(obj, HelloBean.class);
            log.info(bean.Hi());
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }

}

----------------------------------------编辑------ --------------------------------

1 个答案:

答案 0 :(得分:1)

使用远程接口名称通过JNDI进行查找。 然后使用PortableRemoteObject.narrow()方法将其强制转换为远程bean。

Object obj = ctx.lookup("HelloBeanRemote");
HelloBean bean = (HelloBean) PortableRemoteObject.narrow(obj, HelloBean.class);