Use EjbProxy
Call a simple and effective class of remote EJB
Summary:
This tip tells you how to use EjbProxy, it provides some cases, calling remote EJB is not directly engaged with your code to perform a special EJB.
By gorsen huang
Translator Biggie
If you often use EJB development, you may write code from the client program to access EJB or write code to access other EJBs. Normally, implementing these code only requires some few changes. A good programmer is handled in most cases this issue with copy and ads. However, after copying and attachment, it will make people bother. That's why I decided to develop a class to help me do it. I called EjbProxy. I used it to keep the code clean and neat. This class does not need to know the jJB JNDI Name, and can be on the client Or use it in EJB.
You have to follow 4 basic measures to call EJB
1. Create a JNDI context (Initial Context)
2. Find EJB Name (Look Up The EJB Name in Context) in the context
3. Get EJB object
4. Create an EJB instance object via the home () method of the Home
1-3 Steps are implemented as follows:
InitialContext CTX = New InitialContext (Prop); Object Home = CTX.lookup (beanname); ejbhome obHome = (ejbhome) PortableRemoteObject.Narrow (Home, Ejbhome.class);
As you can see, you get an EJB Name Object does not need to know the specific implementation details, except for JNDI lookup.
Step 4 is not easy to understand, when you try to create an EJB object with HOME logs, once the EJBHOME interface does not define the create () method, you cannot call the create () method through the Home object, just actually The EJB implementation is implemented.
As a viable solution, you can define an interface to inherit the EJB Home and the Create () method, then you can simply use the HOME object and access the create () method. However, this method has a disadvantage: it requires all EJB implementation this interface, from any HOME interface, call the CREATE () method you want to call.
A better solution is that you can use Java's reflexibility (for details on reflection API, please refer to resources) Use the Reflection API you can load the specified class with the class.forname method, then access the getDeclaredMethod () method to re- Declare Class, you can use Method.Invoke to perform this method.
The following code is to use the reflection to obtain an instance of EJB from the Home object:
. Method m = obHome.getClass () getDeclaredMethod ( "create", new Class [0]); Object objRemote = m.invoke (obHome, new Object [0]); Object obj = PortableRemoteObject.narrow (objRemote, theClass);
That is, this segmentation is obtained from the Home object, and obtains Create's Method object by accessing getDeclaredMethod () by the Create () method name. In addition, use the invoke () method to perform the CREATE method, return to the instance of EJB. The EjbProxy Class
EJBPROXY CLASS, source code EJBPROXY.JAVA, handles these annoying details.
Use ejbProxy quite simple, first create an EJBProxy object, you can pass parameters, such as the initial context object with the Initial Context factory and the Provider URL. Then access the getobj () method with JNDI to find EJB to get the EJB instance object. Note that when you call another EJB from an EJB involved name, you may need to fill a environment string of the Lookup Name, such as java.comp / env /.
Example Download It will display an EJB to call you how to use EJBPROXY. Test purposes, I also execute an EJB, named EJBPROXYEXAMPLE, it has a Pulbic method
Public String Hello (String Name) throws RemoteException;
This method passes into a name string and returns a string variable consisting of "Hello". EjbProxy's JNDI name is as follows: com.javaworldtip.ejbproxyexample.ejbProxyexample, assuming you to construct and publish EJB with WebLogic 7001 port EjbProxyExample, client code:
EjbProxy myproxy = new EjbProxy ( "weblogic.jndi.WLInitialContextFactory", "t3: // localhost: 7001"); EjbProxyExample obj = (EjbProxyExample) myproxy.getObj ( "com.javaworldtip.ejbproxyexample.EjbProxyExample"); // now get "Hello, World!" String and show itString strhello = obj.hello ("world"); system.out.println (strhello);
If you use this EJB to call another EJB inside the same server, the code is as follows:
EjbProxy myproxy = new ebjproxy (); ejbproxyexample obj = (ejbproxyexample) MyProxy.Getobj ("com.javaworldtip.ejbproxyexample);
If you have specified EJBPROXYEXAMPLE in the deployment file, you can use the getobj () method directly:
EjbProxyExample Obj = (ejbproxyexample) MyProxy.getobj ("java: comp / env / ejbproxyexmaple");
How it works (how is it works)
You can extend the constructor or setContextProperties () method;
public void setContextProperties (String initContextFactory, String providerUrl, String user, String password) {_prop = new Properties (); _prop.put (Context.INITIAL_CONTEXT_FACTORY, initContextFactory); _prop.put (Context.PROVIDER_URL, providerUrl); if (user! = NULL) {_prop.put (Context.security_principal, user); if (password == null) password = ""; _prop.security_credentials, password);} All parameters are created an initcontext environment, Including the initcontext factory, provider URL, user name, password, if you use this class to reomete EJB in the same container, you can simply pass null or simply ignore them.
Now let's take a look at important methods in our agent class, getobj ()
public Object getObj (String beanJndiLookupName) throws EJBException {try {EJBHome obHome = getHome (beanJndiLookupName); // get the method of create Method m = obHome.getClass () getDeclaredMethod ( "create", new Class [0]);. / / invoke the create method Object obj = m.invoke (obHome, new Object [0]); return obj;} catch (NoSuchMethodException ne) {throw new EJBException (ne);} catch (InvocationTargetException ie) {throw new EJBException (ie }} Catch (illegaaccessException IAE) {throw new ejbexception (ie);}}
In the getobj () method, the only required parameter is the JNDI Name of EJB. This method first calls a gethome () method to get the EJBHOME object, and then call the getDeclaredMethod () method to get the create method. Once CREATE is captured, you can call it and get an instance of EJB, if an unexpected, will throw an exception.
You must handle some exceptions that may happen, for example, if the EJB does not have a name crete method, method getDeclaredMethod () will throw NosuchMethodeXception exception, in which a variety of capture will process these possibilities by creating new EjBexception An exception occurring, this method, program calling getobj () method must throw EJBEXCEPTION and appropriate processing. The following: The method code of the gethome () method of the getojb () method is as follows:
public EJBHome getHome (String beanJndiLookupName) throws EJBException {try {InitialContext ctx = null; if (_prop = null!) ctx = new InitialContext (_prop); else ctx = new InitialContext (); Object home = ctx.lookup (beanJndiLookupName); Ejbhome obHome = (ejbhome) PortableRemoteObject.narrow (Home, Ejbhome.class); return.com (namingexception ne) {throw new ejbexception (ne);}}
This method simply limits the first step to the third step to call the EJB method, I use the gethome () method to get the Home object I want. I.E You want to get an EJB object, and then call its method (for example, call the CREATE method to create a session bean)
Extend EjbProxy
For some cases, you may want to inherit EJBPROXY, for example, assume that the EJB's create () method does not have any input parameters, you can simply use Getobj to deal with most cases by inheriting this class:
Public Object Getobj (String Beanjndilookupname, Class [] Classes, Object [] Objects) throws EJBEXCEPTION;
Inside the method, the extra parameter is applied to the getDeclaredMethod () and Invoke () method:
Method m = obHome.getClass (). GetDeclaredMethod ("Create", Classes); Object Obj = M.Ivoke (Obhome, Objects);
You can perform any EJB method by extending this class:
Public Object ExecuteMethod (String MethodName, Class [] class, object [] Objects) throws EJBEXCEPTION;
MethodName is a method name that you may need to hide EJB objects in the class before getting an EJB object. The Getobj method is then modified with similar processes.
SIMPLIFY YOURYDAY CODING
With Java reflex mechanism, you can use ordinary flexible EJBProxy to call local or remote or another EJB. If you see, you can use ejbProxy or expand it to instantiate and call EJB. Resources
Obtain the source file for this tip: http://www.javaworld.com/javaworld/javatips/javatip118/ejbproxytip.zip
Sun's Enterprise JavaBeans Information: http://java.sun.com/products/ejb
Sun's Java Reflection API Documentation: http://java.sun.com/products/jdk/1.1/docs/guide/reflection/
Also See "make an ejb from any java class with java reflection," Tony Loton (javaworld, december 2000): http://www.javaworld.com/jw-12-2000/jw-1215-anyclass.html
For more ejb articles, check out javaworld's topical index: http://www.javaworld.com/channel_content/jw-ejbs-index.shtml
For more articles on jndi, Visit Our Topical Index: http://www.javaworld.com/channel_content/jw-jndi-index.shtml
View All Previous Java Tips and Submit Your Own: http://www.javaworld.com/javatips/jw-javatips.index.html
Learn Java from the Ground Up in javaworld's java 101 column: http://www.javaworld.com/javaworld/topicalindex/jw-ti-java101.html
Java Experts Answer your Toughst Java Questions in JavaWorld's Java Q & a column: http://www.javaworld.com/javaworld/javaqa/javaqa-index.html
Sign Up for javaworld's free weekly email newsletters: http://www.idg.net/jw-subscribe
You'll Find A Wealth of It-Related Articles from Our Sister Publications At IDG.Net