EJB error? Not panic!
You have Java in their favorite books read on the chapter Enterprise Javabean technology also has practiced a simple HelloWorld bean, and follow the deployment process of the proposed release it. Now you have to write a client to client through the call to this masterpiece. So you write a similar code in Listing 1:
Listing 1. A very simple bean called client
1 InitialContext ic = new InitialContext ();
2 Object or = ic.lookup (”ejb / HelloWorldHome”);
3 if (or! = Null) (
4 / / Narrow the return object to the Home class type
5 HelloWorldHome home =
6 (HelloWorldHome) PortableRemoteObject.narrow (or,
7 HelloWorldHome.class);
8 / / Create an EJB object instance using the home interface.
9 HelloWorld hw = home.create ();
10 / / Invoke the method
11 System.out.Println (hw.hello ());
12)
13
Run the command line client, use the most convenient one hand Java installation - the application server style = “COLOR: # 000000″ href = “http://server.it168.com/” target = _blank> ; the one used by the server. Everything is perfect! With the joy of success, you are transferred to the second computer to run your client. This time, you get a terrible error message. First, you may get java.lang.NoClassDefFoundError: javax / ejb / EJBObject, then a bunch of other NoClassDefFoundError s, because you forgot to submit a tie with the necessary stub and the JAR file, and does not provide or take into account other various EJB-related content. Ultimately, your client is running to the first line of interesting code (InitialContext ic = new InitialContext ();). Arrival in this row are the exception - you will almost certainly be an exception - will be chosen based on your specific context provider vary.
Interpretation of these terms
Before we continue down the definition of several terms would be helpful. Use of the computing world are some strange terminology, vocabulary and trendy acronym, Java technology is no exception (perhaps this should be JavaIsNoException?). If you are experiencing problems mentioned above, this term which may make you feel some loss. So let us discuss in this article will encounter the term to try to understand their meaning is a good idea.
Name space, context, context of the initial context and sub-location of these terms are related - from the perspective of the client when the EJB component where the conceptual position. A name space to imagine a town, cities and towns in the store by the EJB home interface (we will discuss it later) said. Context is a town in one location. When you start the initial context is the location - as it is the way to the town. The sub-context is the street name.
home interfaces (home interface) and remote interface (remote interface) Enterprise JavaBean component has three parts. First is the bean code itself. Then the home interface, which defines the EJB bean to create your own way. home interface is released in the name space. When you have a home interface, you can call the Create () to get remote interface from the application server. Access the remote interface, you can call the actual EJB code form methods.
How these terms apply to your town simulation go? Get the correct town and find the correct address, you'll need into the shop or ring the bell (called Create ()). This process is for you to go all the shops are the same, but the response you receive depends on who is providing services - such as a butcher, a baker or a candlestick maker. This response represents the remote interface. Everyone is different and may require him to provide something different. You must know the person you talk to (the bean) of the career to ask the right questions (ie, call the correct method) - to a butcher to a loaf of bread from time to properly.
CosNaming, LDAP and JNDI Java Naming and Directory Interface (Java Naming and Directory Interface JNDI) provides a standard interface, which indicate how the name of the space you need to interact. We have mentioned is JDNI LDAP and CosNaming namespace type. We now extend the metaphor: JNDI is a template for cities and towns, while the CosNaming, and LDAP is a particular town. They operate in a similar manner, but have different layouts.
Attribute provides a mapping
Let us look at how to use all of these elements in order to successfully call from a remote computer on our way to EJB components. In order to allow customers to connect to your carefully crafted EJB components, need a few things. First of all, it requires the client code for all JAR files, general EJB JAR file, such as J2EE.jar related and when to deploy the generated bean stub and tie. These files allow your client can always reach the initial context.
Next, your client needs information is the value of some properties. First, you will need a few java.naming.factory.initial value. The attribute points to a provision of the initial context factory class. A typical value of the property is com.sun.jndi.cosnaming.CNCtxFactory, that is what we are here a few examples used in value. This class exists in the rt.jar in, so it is a basic part of JVM. Plant is used by the CosNaming name servers, but the JVM also includes an LDAP factory. We will see later, different application servers provide their own initial context factory.
This class together with the name server URL and port number of the detailed information used to generate the name space to interact with the InitialContext class. However, if no provider URL, it will connect to localhost port 900 (or your other context factory default port). To connect to a remote server, you need to have a value attribute java.naming.provider.url.
The new programmers find it hard to understand all of these reasons are: no matter your application server to run anything locally, this stuff usually docile work. This is due to environmental care of everything, when you ask a InitialContext, the environment will give you that you want. But when your customer is transferred to a different computer, to rely on himself. You need to know which JAR file copy, and what settings do. I know that some people make their clients work correctly, the application server all the JAR files are copied to the second computer!
By default, InitialContext factory is defined in jndi.properties, the factory class with the default server URL and port number defaults. This file is in the class path (which generally means in the local directory) or in your class path in any JAR. The application server may be different in different JAR files available to their default values, WebSphere Application Server to store a default in namingclient.jar copy. To specify your own default values, only need to edit the class path in the first copy. This is a way to configure the properties, the absence of the command line or code-driven set, then the client will use jndi.properties values. However, although this may be suitable for simple settings, but if you deal with multiple servers and name space, you may want a customer to configure a client.
These properties are how to use the name of the space we have to use different values for? As mentioned earlier, there are two forms of JNDI name space: CosNaming and LDAP. Each of which is associated with the transmission: are IIOP and LDAP. LDAP name space to use a LDAP transfer (you will use a like ldap: / / myldapnameserver this URL to connect to it), but using a IIOP CosNaming transfer (you will use a like iiop: / / mycosnamingserver this URL to connect to it) . CosNaming default port number is 900, and the default LDAP port number is 389. However, any given namespace server implementations use the default values may be different.
Command line configuration properties
Let us look at how to use command-line configuration properties. If you want to practice it at home, into the JDK installation bin directory. In this folder, you can find a program called tnameserv.exe (for Windows) or just tnameserv (for UNIX-based systems). Through the implementation of this program will start on port 900 a sample CosNmaing name server.
Now just use one can view the CosNaming namespace utility to equip yourself. I use Eclipse as a development environment, I am in the following sections provide references to the JNDI browser plug-in link. In theory, you should be able to be a namespace browser to their computer's port 900 and see the name of a very boring, empty space (although some of the application server by default the content will be filled with a lot of different namespace). In order to enrich our name space, we will now write a simple program to put some content in it, as shown in Listing 2:
Listing 2. A simple cosNaming name of the spatial interaction
1 package example.publisher;
2
3 import javax.naming.InitialContext;
4
5 public class Publish (
6
7 public static void main (String [] args) (
8 / /
9 / / This example creates a subcontext in a namespace
10 / /
11 try (
12 InitialContext ic = new InitialContext ();
13 ic.createSubcontext (”Test”);
14) catch (Exception e) (
15 System.out.println (e);
16 e.printStackTrace ();
17
18)
19)
20)
21
This application will be assumed to have the correct documents needed for the initial up and down all the attributes are available. So now you can run it from the command line and to provide these properties at run time (which URL should be adjusted according to your environment):
1 java-Djava.naming.factory.initial = com.sun.jndi.cosnaming.CNCtxFactory
2-Djava.naming.provider.url = iiop: / / mymachine: 900
3 example.publisher.Publish
4
As usual, our customers will find an example and create a context namespace named Test in the sub-context. You can use the namespace browser to confirm that.
Now try running on a computer named server, using the same command line (of course, made adjustments to the URL again) in another computer running the application in Listing 2. It should be no problem running (you may need to modify this example to change the content of the limit, or even delete the sub-context, rather than create it, so in the second run when you can be sure that it has played a role in a).
In the application configuration properties
So, if you do not want to set these properties on the command line how to do? There is another way. In the program can explicitly declare these attributes. This means that you do not need java command to provide special options. To change the code in Listing 2 to explicitly set the required attributes, it looks the same as the code in Listing 3:
Listing 3. Simple cosNaming name of the spatial interaction, the code in the application set the property
1 package example.publisher;
2
3 import javax.naming.InitialContext;
4
5 public class Publish (
6
7 public static void main (String [] args) (
8 / /
9 / / This example creates a subcontext in a namespace
10 / /
11 try (
12 Properties prop = new Properties ();
13 prop.setProperty (”java.naming.factory.initial”,
14 “com.sun.jndi.cosnaming.CNCtxFactory”);
15 prop.setProperty (”java.naming.provider.url”,
16 “iiop: / / mymachine: 900″);
17 InitialContext ic = new InitialContext (prop);
18 ic.createSubcontext (”Test”);
19) catch (Exception e) (
20 System.out.println (e);
21 e.printStackTrace ();
22
23)
24)
25)
26
Now the program is no longer required a long command line configuration, but keep in mind in this way hard-coded applications written in these settings.
Find the path leading to bean
So far, we have seen a few can prove that we have to connect to a remote name space and complete examples of some tasks, although these tasks are very boring - create a child context. In practice, usually by the tool for you to create and publish all work, you really need to do is to find an object. In this section, we will get in CosNaming namespace HelloWorld bean published in the Home Interface. Then let's look at how the LDAP name space to find its Home Interface.
To illustrate, we assume that you have deployed the HelloWorld bean, its home interface to HelloWorldHome published in the example / HelloWorldHome.
In the previous section, we had a connection to the name server of hard work, now we need only check on the EJB components. This requires us to pass a string to the query method, it is said that from the InitialContext (your starting point in town) to want to go HomeInterface (house or shop) direction. It sounds simple - but here the specific context of your choice would impact the plant. Such as WebSphere Application Server brought the factory class does not always take you into the root namespace. So we need to check the string HomeInterface InitialContext will be based on your location into the town change. And, in the local server, the context factories may be placed with the remote server your different starting position.
For this reason, I recommend that you do not like the hard-coded list of three as used in the query string, but use the command line or property transfer documents. Especially for the architecture of a multi-step should be the case. For example, you may have a component called an EJB client, the bean may then need to call the server may be in different EJB components on a second! In this case, the property should be passed in each step. This experiment was repeated (trial-and-error) query provides a simple mechanism, and only a relatively small change can get the flexibility of the final application. So let's look at an example query application. In Listing 4, the property is set in the program, but it is also based on a command-line value. This command-line and in our previous example using a slightly different, as we see below.
Listing 4. Query interface to a home
1 package example.lookup;
2 import java.util.Properties;
3 import javax.naming.InitialContext;
4 import javax.rmi.PortableRemoteObject;
5
6 import example.HelloWorld;
7 import example.HelloWorldBean;
8 import example.HelloWorldHome;
9 import javax.naming.InitialContext;
10
11 public class Lookup (
12
13 public static void main (String [] args) (
14 / /
15 / / This example looks up the home interface of an EJB to a namespace
16 / /
17 try (
18 Properties prop = new Properties ();
19 prop.setProperty (”java.naming.factory.initial”, args [0]);
20 prop.setProperty (”java.naming.provider.url”, args [1]);
21 InitialContext ic = new InitialContext (prop);
22 Object or = ic.lookup (args [2]);
23 if (or! = Null) (
24 / / Narrow the return object to the Home class type
25 HelloWorldHome home =
26 (HelloWorldHome) PortableRemoteObject.narrow (or,
27 HelloWorldHome.class);
28 / / Create an EJB object instance using the home interface.
29 HelloWorld hw = home.create ();
30 / / Invoke the method
31 System.out.Println (hw.hello ());
32)
33) catch (Exception e) (
34 System.out.println (e);
35 e.printStackTrace ();
36
37)
38)
39)
40
This procedure is called with three arguments: the context to use the factory, provider URL and contains the name of the query string. We already know what the first two, then the third alternative?
If you are still using tnameserv as the name server, then you are likely to be directly posted to the bean / example / HelloWorldHome. In this case, as long as the / example / HelloWorldHome as the third parameter can be a successful query. However, if you use the name server has a more complex name space, then there may be used by a deployment tool to increase the additional layer. For example, WebSphere in default JavaBean deployed ejb /, but this is not the root namespace, and only when using WebSphere context factory, by passing the string / ejb / example / HelloWorldHome will make your space in the name of the in the correct location. If you use a different application server to provide the context factory (for example, a standard Java installed only on computers running the client when required to do so), this problem will worsen. However, the application server's name server queries the document should indicate the name of EJB components will be from the space where to begin. Look at the example document, then view the name of the browser space to determine clients InitialContext will put them somewhere. Namespace tend to cycle, so you can try to follow a branch to the infinite. This means that from the beginning of a home can be found in the context of the road.
In short, the following is the list of passing the appropriate parameters to the application 4 command line:
1 java Lookup com.sun.jndi.cosnaming.CNCtxFactory
2 iiop: / / mymachine: 900 example / HelloWorldHome
3
In CosNaming, the name of the space sub-context by the slash (/) character separation, which is the same as the standard URL. LDAP syntax is different, we will see below.
LDAP Introduction
Now let's add the LDAP. In terms of the content of this article, LDAP is another JNDI name space, but its structure of representation and CosNaming name spaces that are distinctly different. It also requires a different context factory - but this is not a problem, because we will always specify the correct command line in the plant (in our example, we will use the basic JVM is part of the plant, but remember that different applications server may have its own factory). And it needs a pointer to point to different name servers - and, fortunately, we are also specified in the command line it. Of course, that home interface, the location of the string is different, but you guess how? Yes, we specify it in the command line. You can use these command-line call to see the benefits: all have to do is change the call to the way our test procedures, in theory, we can reach any JDNI name server, or even a smooth transfer from the CosNaming LDAP without any code change . Yes, this is only a theory, of course, in any case, the key is to have the correct parameters.
Some name server will protect the part of the name space, which means that can only be released to allow the region. Suppose you have a running LDAP server, its details are as follows:
URL: ldap: / / mymachine: 1389
BaseDN: c = myldap
LDAP name space tree structure in general as follows:
1 ibm-wsnName = MyServer, ibm-wsnName = HelloWorldHome
2
However, when we pass this string to the program, we need to reverse it (do not ask me why). Therefore, we use the string looks like this:
1 ibm-wsnName = HelloWorldHome, ibm-wsnName = MyServer,
2
BaseDN said space in the name of the place you want to start. LDAP name for a given server can be a lot for this position, it depends on how it is constructed. In this example, we go directly to the root c = myldap. But if we want to jump to the name space of a tree, you can specify the ibm-wsnTree = myTree, c = myldap as BaseDN not jump to that point.
In this way, we passed to the program's command line arguments like this:
1 java Lookup com.sun.jndi.ldap.LdapCtxFactory ldap: / / mymachine: 1389 / c = myldap /
2 ibm-wsnName = HelloWorldHome, ibm-wsnName = MyServer
Here we specify an LDAP context factory, and then pass the name of LDAP server, and we want to start position. Then to the EJB component to query the path reversal. We can use this command line call in the CosNaming example using the same piece of code (Listing 4).
Of course, the code used in this article no reason not to constitute a helper class in a way - it with three arguments, and returns the Object or, the object is trying to do anything before the call ic.lookup (args [2]) O'clock returned. Then, when you need to conduct a query, just use this helper class, to pass it for the appropriate parameters at the current situation, you need to retrieve an object reference, and ready to narrow it to the actual class. (Note: I do not guarantee this kind of performance, but rather to provide this is the case the original code, I or IBM makes no guarantee this.) Of course, you can achieve a completely general-purpose reflective way, but it will make things much more complex, but also beyond the scope of this article.
In the end we have one last thing to consider. You can write a combination of 3 and 4 we list the technologies used in the client. It checks whether the command line is given a value; If so, it set these values, if not, it uses hard-coded values. Thus, in the process can have a meaningful default value, but if necessary, can use command-line options override them. Only minor changes to the code.
Security style = “COLOR: # 000000″ href = “http://safe.it168.com/” target = _blank> Security home
As a Review: Here is the EJB query multiple servers and multiple application case, to make any system run and should have or should have known the most important of four things:
At any given stage, the next stage of all stub and tie must be in the class path. Unless the environment to know what this class, otherwise you can not narrow an object to use it.
At each stage and the EJB JAR file related to the general, such as J2EE.jar.
The form of parameters passed to the context of plant type, name server names and JDNI query string.?????????????????br />
???????????????????????JDNI ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? EJB ??????????????? JNDI ?????????????????developerWorks ??? Brett McLaughlin ?????EJB ??????????????????????????????????????br />
????:
MKV to MOV
Report ASP And PHP
Taobao is free of charge commitment charges due to question
On The Centralized Accounting
HP's new CEO took office claiming to be UNWILLING to Hollywood star
Can You Do A Reverse Unlisted Phone Number Search
Only store is not enough
VOB to WMV
YouTube to FLV
Selected Fifth One, Peking University Guanghua New Year's Forum
Source Editors comments
World Rally 3 cheat
Mocha Further Prosecution Of The North Tower “Nine Questions” Cocoon
Who are you looking for a Salesman?