Java – how can an EJB client not find an EJB server without a URL?

I'm new to Java EE At present, I am passing the Java EE 6 tutorial Volume 1 (basic concept beta) of Sun Microsystems In order to get rid of monotonous reading time, I play with several Java EE Projects / code written by others

I'm from se My head is still full of se In Se (two tier application) I use

DATABASE_ URL =“jdbc:MysqL://something.db_server.com/db_name”

This is where my client knows the location of the database server

In a 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();

I see that the code has a URL and a port number There's this line

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

The client knows where the server is, the URL and which port I think the code was written in Java EE 5

Today, I found another example of using NetBeans 7, Java EE 6 and GlassFish 3 Client code

@EJB
private static MySessionRemote mySession;

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

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

Link here @ h_ 419_ 23@http ://netbeans. org/kb/docs/javaee/entappclient. html

No URL and port number provided

The development of Java EE 6 and NetBeans 7 by David R. heffelfinger has a similar example in Chapter 7 The author did not explain how it was done in the book I think he did, but I may have missed

My question is how can the client find a server without a URL? Do you want to specify in one of the XML files? Customers can be in California and GlassFish server can be in New York Anyone can explain or point out any tutorial / blog / article to me, can I find the answer?

thank you.

Solution

Here are two things

First, there is no way to get a reference to a remote EJB specified in Java EE Do you get sympathy for what the individual supplier thinks it should do

Although JNDI is a de facto standard for this, it is not mandatory in itself

Example: JBoss up to as7

In JBoss as, until as 7, the following sequence is used to obtain the 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 a bean is retrieved from that context (please note that you cannot add the well-known prefix "Java: /" here, otherwise it will be intercepted by JNDI and resolved locally, although it will be found in the remote context)

Since this method is not standardized as described above, a single vendor can completely change it between implementation versions This is true even for implementations of the same Java EE version

Example: JBoss as7

In JBoss as 7, JBoss wants to leave JNDI (because it is not specified that JNDI must be used). Now it occurs in about following way:

You will first need to use the following context to create JBoss EJB client Put the properties file on your classpath:

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 the following code:

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 point of view, it does not give the URL, but it is statically hidden in a configuration file

Application client container

This is another matter Shown is the so-called application client container (also known as ACC)

This is different from the above example, where the Java se application uses JNDI to contact the remote server The application client container is a bit obscure in Java EE The idea seems to be that you dynamically download client code from a server (such as an applet or Java Web start application) and magically "know" where it comes from Support for (static) injection in the main class is very limited, and remote beans can be injected directly

Application client container was an early idea of Java EE, and as far as I know, it has never attracted attention Over the years, its initial concept has never happened Because it still needs a lot of vendor specific things to do, I think most people don't bother it and only use JNDI

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>