Glassfish 4通过独立应用程序访问EJB [英] Glassfish 4 Acessing EJBs via Standalone Application

查看:106
本文介绍了Glassfish 4通过独立应用程序访问EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在查找过程中没有找到JNDI名称,我挣扎了很多。



经过一些没有任何解决方案的研究后,我在Glassfish服务器日志中发现了一条消息,声明如下:
$ b

[glassfish 4.1 ] [INFO] [AS-EJB-00054] [javax.enterprise.ejb.container] [tid:_ThreadID = 47 _ThreadName = admin-listener(4)] [timeMillis:1424811833554] [levelValue:800] [[Portable JNDI names对于EJB HelloBean:java:global / ponteWS / HelloBean!br.com.pontews.HelloRemote,java:global / ponteWS / HelloBean!br.com.pontews.HelloBean]]]



我尝试了远程名称和voilá!它工作。

我尝试了另一个名字,并没有工作。



这里提出的问题: p>

1 - 为什么JNDI名称太奇怪了?有什么我可以做的,以避免在名称前的包名称?
2 - 当直接访问bean而不是HelloRemote接口时,我得到的错误是什么?



以下是HelloRemote:

 包br.com.pontews; 

import javax.ejb.Remote;

@Remote
public interface HelloRemote {

public String sayHello(String name);

$ / code>

以下是Bean:

 包br.com.pontews; 
import java.io.Serializable;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(name =HelloBean)
@LocalBean
@Remote
公共类HelloBean实现HelloRemote,可序列化{

/ **
*
* /
private static final long serialVersionUID = 1L;

/ **
*默认构造函数。
* /
public HelloBean(){
}

public String sayHello(String name){
returnHello,+ name +。 ! ;
}

}

以下是可用的调用:

 对象查找= 
ctx.lookup(java:global / ponteWS / HelloBean!br.com.pontews。 HelloRemote);

以下是无效的调用:

  Object lookup = 
ctx.lookup(java:global / ponteWS / HelloBean!br.com.pontews.HelloBean);

最后我得到的错误是无效的:



线程main中的异常javax.naming.CommunicationException:SerialContext的通信异常[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming .factory.url.pkgs = com.sun.enterprise.naming,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [根异常是java.rmi.RemoteException:CORBA NO_IMPLEMENT 1398079489也许;嵌套异常是:
org.omg.CORBA.NO_IMPLEMENT:---------- BEGIN服务器端堆栈跟踪----------
org.omg。 CORBA.NO_IMPLEMENT:警告:01000001:缺少本地值执行vmcid:SUN次要代码:1已完成:可能



引起:java.lang.ClassNotFoundException: br> EJB31_Generated__HelloBean__Intf ____ Bean (没有安全管理器:禁用RMI类加载器)

感谢

解决方案

您用于lookup()的名称具有EJB规范指定的格式。 b $ b

对于glassfish,应该也可以使用远程业务接口的全限定名来查找bean( https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment )。在你的情况下,这将是

  br.com.pontews.HelloRemote 

如果您在查找过程中仍然遇到问题,这里的说明可能会有帮助:(from https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB



< blockquote>

第1步。在代码中使用no-arg InitialContext()构造函数。



开发人员遇到的最常见问题是传递特定JNDI引导属性到InitialContext(args)。其他一些供应商需要这一步,但GlassFish不需要。相反,使用no-arg InitialContext()构造函数。 第二步:将远程EJB的全局JNDI名称传递给InitialContext.lookup()。

单独的java客户端无法访问组件命名环境(java:comp / env)或@EJB注释,因此它们必须显式使用全局JNDI名称来查找远程EJB。 (有关如何将全局JNDI名称分配给EJB组件的更多信息,请参阅此处)假设远程EJB的全局JNDI名称为FooEJB:

对于带有a 3.x远程业务接口:

Foo foo =(Foo)new InitialContext()。lookup(FooEJB);

请注意,在EJB 3.x情况下,查找的结果可以直接转换为远程业务接口类型,而无需使用PortableRemoteObject.narrow()。

对于EJB 2.1及更早的会话/实体bean:

对象homeObj = new InitialContext()。lookup FooEJB);



FooHome fooHome =(FooHome)
PortableRemoteObject.narrow(homeObj,FooHome.class);



Foo foo = fooHome.create(...)
第3步。在java客户端的类路径中包含适当的GlassFish .jars。



用于GlassFish 3.



在客户端的类路径中包含$ GLASSFISH_HOME / glassfish / lib / gf-client.jar。假设应用程序类位于/ home / user1 / myclasses中,并且主客户端类为acme.MyClient:





java -classpath $ GLASSFISH_HOME / glassfish / lib / gf-client.jar:/ home / user1 / myclasses acme.MyClient 注意,Java EE 6 API类自动包含在gf-client.jar中,因此不需要将javaee.jar显式添加到类路径中。 gf-client.jar引用GlassFish安装目录中的许多其他.jar,因此最好从安装目录本身中引用它,而不是将其复制(和所有其他.jar)到另一个位置。注:gf-client.jar位于GlassFish v3中的$ GLASSFISH_HOME / modules / gf-client.jar中。


>

I am trying to access an EJB via JSE Standalone application.

I was struggling a lot with JNDI Names not being found during lookup.

After some research without any solution I found in the Glassfish server log after a deploy a message stating something like:

"[glassfish 4.1] [INFO] [AS-EJB-00054] [javax.enterprise.ejb.container] [tid: _ThreadID=47 _ThreadName=admin-listener(4)] [timeMillis: 1424811833554] [levelValue: 800] [[ Portable JNDI names for EJB HelloBean:java:global/ponteWS/HelloBean!br.com.pontews.HelloRemote, java:global/ponteWS/HelloBean!br.com.pontews.HelloBean]]] "

I tried the remote name and voilá!!! It works.

I tried the other name and did not work.

Here comes the questions:

1-Why the JNDI name is so weird???? Is there something I can do in order to avoid the package name in front of the name of the bean? 2-What is the error I get when acessing the bean directly instead the HelloRemote Interface?

Here is the HelloRemote:

package br.com.pontews;

import javax.ejb.Remote;

@Remote
public interface HelloRemote {

    public String sayHello(String name);
}

Here is the Bean:

package br.com.pontews;
import java.io.Serializable;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(name="HelloBean")
@LocalBean
@Remote
public class HelloBean implements HelloRemote, Serializable {

    /**
     * 
     */
    private static final long   serialVersionUID    = 1L;

    /**
     * Default constructor.
     */
    public HelloBean() {
    }

    public String sayHello(String name) {
        return "Hello," + name + ".!" ;
    }

}

Here is the call that works:

Object lookup = 
ctx.lookup("java:global/ponteWS/HelloBean!br.com.pontews.HelloRemote");

Here is the call that does not works:

Object lookup = 
ctx.lookup("java:global/ponteWS/HelloBean!br.com.pontews.HelloBean");

And finally the error I get with call that does not work:

Exception in thread "main" javax.naming.CommunicationException: Communication exception for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.rmi.RemoteException: CORBA NO_IMPLEMENT 1398079489 Maybe; nested exception is: org.omg.CORBA.NO_IMPLEMENT: ----------BEGIN server-side stack trace---------- org.omg.CORBA.NO_IMPLEMENT: WARNING: 01000001: Missing local value implementation vmcid: SUN minor code: 1 completed: Maybe : : : Caused by: java.lang.ClassNotFoundException: br.com.pontews.EJB31_Generated__HelloBean__Intf____Bean (no security manager: RMI class loader disabled)

Thanks

解决方案

The name you are using to lookup () has the format as specified by the EJB specification.

In case of glassfish it should also be possible to use the fully qualified name of the remote business interface to lookup the bean (https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment). In your case this would be

br.com.pontews.HelloRemote

If you still have problems on the lookup the instructions here might help: (from https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB)

Step 1. Use the no-arg InitialContext() constructor in your code.

The most common problem developers run into is passing specific JNDI bootstrapping properties to InitialContext(args). Some other vendors require this step but GlassFish does not. Instead, use the no-arg InitialContext() constructor.

Step 2. Pass the global JNDI name of the Remote EJB to InitialContext.lookup()

Stand-alone java clients do not have access to a component naming environment (java:comp/env) or to the @EJB annotation, so they must explicitly use the global JNDI name to lookup the Remote EJB. (See here for more information on how global JNDI names are assigned to EJB components) Assuming the global JNDI name of the Remote EJB is "FooEJB" :

For Beans with a 3.x Remote Business interface :

Foo foo = (Foo) new InitialContext().lookup("FooEJB");

Note that in the EJB 3.x case the result of the lookup can be directly cast to the remote business interface type without using PortableRemoteObject.narrow().

For EJB 2.1 and earlier session/entity beans :

Object homeObj = new InitialContext().lookup("FooEJB");

FooHome fooHome = (FooHome) PortableRemoteObject.narrow(homeObj,FooHome.class);

Foo foo = fooHome.create(...) Step 3. Include the appropriate GlassFish .jars in the java client's classpath.

For GlassFish 3.

Include $GLASSFISH_HOME/glassfish/lib/gf-client.jar in the client's classpath.

E.g., assuming the application classes are in /home/user1/myclasses and the main client class is acme.MyClient :

java -classpath $GLASSFISH_HOME/glassfish/lib/gf-client.jar:/home/user1/myclasses acme.MyClient

Note that the Java EE 6 API classes are automatically included by gf-client.jar so there is no need to explicitly add javaee.jar to the classpath. gf-client.jar refers to many other .jars from the GlassFish installation directory so it is best to refer to it from within the installation directory itself rather than copying it(and all the other .jars) to another location.

Note: gf-client.jar is located in $GLASSFISH_HOME/modules/gf-client.jar in GlassFish v3.

这篇关于Glassfish 4通过独立应用程序访问EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆