EJB客户端如何找不到没有url的EJB服务器? [英] How does the EJB client locate the EJB server without url?

查看:271
本文介绍了EJB客户端如何找不到没有url的EJB服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java EE的新手。目前,我正在通过Sun Microsystems的 Java EE 6教程,第1卷(基本概念测试版)。为了摆脱单调的阅读时间,我玩几个Java EE项目/其他人编写的代码。



我来自SE。我的头仍然充满了SE。在SE(两层应用程序)中,我使用



DATABASE_URL =jdbc:mysql://something.db_server。 com / db_name



这是我的客户端知道数据库服务器的位置。



在一个Java EE示例中,我看到

  //访问JNDI初始语境

属性p = new Properties();

p.put(java.naming.factory.initial,org.jnp.interfaces.NamingContextFactory);
p.put(java.naming.provider.url,jnp:// localhost:1099);
p.put(java.naming.factory.url.pkgs,org.jboss.naming:org.jnp.interfaces);

InitialContext ctx = new InitialContext(p);

//根据您的服务器更改jndi名称,ejb

HelloRemote remote =(HelloRemote)ctx.lookup(HelloBean / remote);

msg =来自EJB的消息 - >+ remote.sayHello();

这个我明白了。该代码具有URL和端口号。有这条线

  p.put(java.naming.provider.url,jnp:// localhost:1099 ); 

客户端知道网址在哪里,哪个端口敲。我认为代码是在Java EE 5时编写的。



今天我发现另一个例子,其中使用了Netbeans 7,Java EE 6和GlassFish 3。客户端代码

  @EJB 
private static MySessionRemote mySession;

/ **
* @param args命令行参数
* /

public static void main(String [] args){
JOptionPane.showMessageDialog(null,
result =+ mySession.getResult());
}

这里是链接
http://netbeans.org/kb/docs/javaee/entappclient.html



没有给出URL和端口号。



由David R. Heffelfinger提供的Java EE 6开发与Netbeans 7的开发与第7章有一个类似的例子。作者没有解释它是如何在书中完成我想他已经做了,但我可能错过了...



我的问题是客户端如何找到没有url的服务器?是否在其中一个xml文件中说明?客户可以在加州,GlassFish Server可以在纽约。任何人都可以向我解释,或指向任何教程/博客/文章,我可以找到答案。



谢谢。

解决方案

这里有两件事情。



首先,在Java EE中没有指定获取对远程EJB的引用的方式。您是否有个别供应商认为应该做的事情。



尽管JNDI是用于此的事实上的标准,尽管这本身并不是强制的。 / p>

示例:JBoss直到AS7



在JBoss AS中,直到AS 7,使用以下序列获取一个远程引用:

 属性env = new Properties(); 
env.put(Context.INITIAL_CONTEXT_FACTORY,org.jnp.interfaces.NamingContextFactory);
env.put(Context.URL_PKG_PREFIXES,org.jboss.naming:org.jnp.interfaces);
env.put(Context.PROVIDER_URL,jnp://myserver.example.com:1099);
InitialContext context = new InitialContext(env);

Bean bean =(Bean)context.lookup(myear / MyBean / remote);

这里,远程服务器的URL提供给初始上下文,从该上下文中检索。 (请注意,您必须在此处添加公知的java:/前缀,否则将由JNDI截取并在本地解析,尽管在远程上下文中进行查找) / em>



由于此方法不符合标准,因此单个供应商可以在实现版本之间完全更改它。即使是针对相同Java EE版本的实现。



示例:JBoss AS7



在JBoss AS 7中,JBoss想要离开JNDI(因为没有指定JNDI必须被使用),现在它发生在大约以下方式



您首先需要放一个 jboss-ejb-client.properties 文件在您的类路径中,具有以下上下文:

  endpoint.name = client-endpoint 
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED = false
remote.connections = default
remote.connection.default.host = myserver.example.com
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS = false

并使用代码如下:

  env.put(Context.URL_PKG_PREFIXES,org.jboss.ejb.client.naming); 
InitialContext context = new InitialContext(env);

Bean bean =(Bean)context.lookup(ejb:/ myear / mymodule / MyBean!com.example.Bean);

所以从代码来看,它没有给出URL,但它是静态隐藏在配置文件中。






应用程序客户端容器




今天我发现另一个例子,使用Netbeans 7,Java EE 6和GlassFish 3。客户端代码[...]


这是另一回事。所示的是所谓的应用程序客户端容器(也称为ACC)。



这与上面的示例不同,其中Java SE应用程序使用JNDI联系远程服务器。应用程序客户端容器在Java EE中有点晦涩难懂。这个想法似乎是您从服务器(如Applet或Java Web Start应用程序)动态下载客户端代码,然后神奇地知道它来自哪里。在主类中对(静态)注入的支持非常有限,可以直接注入远程bean。



应用程序客户端容器是从早期的据我所知,Java EE的日子从未受到重视。经过这些年,它的初期概念从未发生过。由于它仍然需要大量的供应商特定的事情来做,所以我认为大多数人不用打扰它,只使用JNDI。


I am new to Java EE. At present I am going through The Java EE 6 Tutorial, Volume 1 (Basic Concepts Beta) by Sun Microsystems. To escape from monotonous reading time to time I play with few Java EE projects/codes written by others.

I came from SE. My head is still filled with SE. In SE (two tier application) I use

DATABASE_URL = "jdbc:mysql://something.db_server.com/db_name"

This is how my client knows where the database server is.

In one Java EE example I saw

// Access JNDI Initial Context.

Properties p = new Properties();

p.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
p.put("java.naming.provider.url","jnp://localhost:1099");
p.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

InitialContext ctx = new InitialContext(p);

// Change jndi name according to your server and ejb

HelloRemote remote = (HelloRemote) ctx.lookup("HelloBean/remote");

msg = "Message From EJB --> " + remote.sayHello();

This I understand. The code has url and port number. There is this line

p.put("java.naming.provider.url","jnp://localhost:1099");

Client side knows where is the server by the url and which port to knock. I think the code was written at the time of Java EE 5.

Today I have found another example where Netbeans 7, Java EE 6, and GlassFish 3 are used. The client side code

@EJB
private static MySessionRemote mySession;

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {
    JOptionPane.showMessageDialog(null, 
            "result = " + mySession.getResult());
}

Here is the link http://netbeans.org/kb/docs/javaee/entappclient.html

No url and port number are given.

Java EE 6 Development with Netbeans 7 by David R. Heffelfinger has a similar example in chapter 7. The author did not explain how it is done in the book. I think he has done it but I probably missed it…

My question is how the client side locate the server without url? Is it stated in one of those xml files? Client can be in California and the GlassFish Server can be in New York. Can anyone explain it to me or point to any tutorial/blog/article where I can find the answer?

Thank you.

解决方案

There are two things that are going on here.

The first thing is that the way in which to obtain a reference to a remote EJB is not specified in Java EE. You are at the mercy of how an individual vendor thinks it should be done.

Although JNDI is the de facto standard used for this, even this itself is not mandated.

Example: JBoss up till AS7

In JBoss AS up till AS 7, the following sequence was used to obtain a remote reference:

Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://myserver.example.com:1099");
InitialContext context = new InitialContext(env);

Bean bean = (Bean) context.lookup("myear/MyBean/remote");

Here, the URL of the remote server is provided to the initial context and from that context a bean is retrieved. (Note that you must NOT add the well known "java:/" prefix here, or else it will be intercepted by JNDI and resolved locally, despite doing the lookup on a remote context)

Since this method was as mentioned not standardized, a single vendor can change it completely between releases of implementations. Even for implementations for the same Java EE version.

Example: JBoss AS7

In JBoss AS 7, JBoss wanted to move away from JNDI (because it was not specified that JNDI had to be used) and now it happens in approximately the following way:

You'll first need to put a jboss-ejb-client.properties file on your classpath with the following context:

endpoint.name = client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED = false
remote.connections = default
remote.connection.default.host = myserver.example.com
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS = false

And use code as the following:

Properties env = new Properties();
env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext context = new InitialContext(env);

Bean bean = (Bean) context.lookup("ejb:/myear/mymodule/MyBean!com.example.Bean");

So from the code it looks like no URL is given, but it's statically hidden in a config file.


Application Client Container

Today I have found another example where Netbeans 7, Java EE 6, and GlassFish 3 are used. The client side code [...]

This is yet another thing. What's demonstrated there is a so-called Application Client Container (aka ACC).

This is different from the example above, where a Java SE application used JNDI to contact the remote server. The Application Client Container is a bit of an obscure thing in Java EE. The idea seems to be that you download the client code dynamically from the Server (like an Applet or Java Web Start app), and that it then magically 'knows' where it originated from. There is very limited support for (static) injection in the main class, which you can use to inject remote beans directly.

The Application Client Container is an idea from the early days of Java EE and as far as I know has never gotten much attention. After all these years it has never advanced much after its initial conception. Since it still requires a ton of vendor specific things to be done, I think most people don't bother with it and just use JNDI.

这篇关于EJB客户端如何找不到没有url的EJB服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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