EJB entry teaching materials

xiaoxiao2021-03-06  106

Gentle Flying 2001-10-17 00: 00: 0021048 Views

1, EJB development

First, general, tell the development steps of EJB. 1.1 SessionBean's development first, write remote interface, inherit the EJBObject interface, write the PUBLIC method you need to call (these methods will be implemented in SessionBean), pay attention to Declaring Throws Java.rmi.RemoteException. For example:.. Package jsper.ejb; import java.rmi *; import javax.ejb *; public interface MyEJB extends EJBObject {public String sayHello () throws java.rmi.RemoteException;} a second step, the write Home Interface (generated EJBObject Quoted Factory generates at least one CREATE method, pay attention to declare throws java.rmi.RemoteException and javax.ejb.createException.

For example: package jsper.ejb; import java.rmi *; import javax.ejb *; public interface MyEJBHome extends EJBHome {MyEJB create () throws java.rmi.RemoteException, javax.ejb.CreateException;} The third step, photo. The implementation of the positive session bean (implementation is defined in a remote interface), you need to implement a javax.ejb.sessionBean interface Note: You cannot directly implement remote interfaces directly, you don't have to throw RemoteExceptionPackage Jsper.ejb; Import Java .rmi.RemoteException; import javax.ejb *;. public class MyEJBClass implements SessionBean {public MyEJBClass () {} // public method defined in SessionBean the void ejbCreate () throws RemoteException, CreateException {} public void ejbActivate () throws RemoteException {} public void ejbPassivate () throws RemoteException {} public void ejbRemove () throws RemoteException {} public void setSessionContext (SessionContext ctx) throws RemoteException {} // here is a specific implementation public String sayHello () {System.out.println ("Hello");}} The fourth step, write a published configuration file EJB-JAR.XML needs to be provided: bean home name - The Nickname That Clients Use to lookup your bean's home object.Enterprise bean class name - the flully qualifi ed name of the enterprise bean class Home interface class name Remote interface class nameRe-entrant -. Whether the enterprise bean allow re-entrant calls This setting must be false for session beans (it applies to entity beans only) stateful or stateless Session. Timeout - The Length of Time (in Seconds) Before aclient SHOULD TIME OUT WHEN CALLING METHODS ON YOUR bean. You can also provide your own configuration information for your own way to control EJB.

Example: helloejbcom.jsper.ejb.myejbhomecom.jsper.ejb.myejbcom.jsper.ejb.myejbclassStateLessContainer fifth step, generate YAR files with JAR tools with JAR tools with jar files EJB-JAR.XML must be on top Meta-Inf subdirectory This sentence is compared to bite mouth, take an example MYLIB ---- Meta-inf - *. Xml | | COM - COUCOUNIU - EJB --- EJBCLASS | - EJBHOME | -EJB is generating .jar files When SH> CD MYLIB / / Note SH> JAR CV0F myejb.jar * Please note: To this step we make things are and unrelated to specific EJB Server, just follow the sixth step in accordance with EJB , Use the published tool for a specific platform to generate a JAR file that is published. Different middleware products This step is very different, and the result is generated to generate remote interfaces and HOME interface implementations that have only its own EJB Server can understand, and packaged in a JAR file is generally very simple, .jar file released to EJB Server This step is very different according to different middleware products, can be divided into two types, which is released when starting, and is generally very simple, with WebLogic as an example: 1, in the weblogic.properties file The configuration is automatically loaded when WebLogic is started. Add an entry such as: WebLogic.ejb.Deploy = C: /WebLogic510/Myserver/ejb_basic_beanmanaged.jar ,/ c: /weblogic510/myserver/ejb_basic_test.jar 2, use deploy or deployertool dynamic loading / uninstall / update the eighth step, write The client's program (I have so far) In the process of publishing EJBs to the EJB Container, we use the publishing tool to bind a directory service of the Container, now we want to call the EJBHOME object from this directory service. Take it, here is divided into both local and external situations: one is the client local call EJB.

For example, the EJB engine and the servlet engine are integrated in the same application server, then there is no need to verify when a servlet is called to call EJB, you can get the implementation of the EJBHOME interface CONTEXT IC = New InitialContext (); system.out.println (" Looking for the EJB Published as 'Hello' "); com.jsper.ejb.myejbhome HomeInterface = (com.jsper.ejb.myejbhome) Ic.lookup (" Hello "); // The name binding when publishing is Hello The implementation of the HOME interface can be obtained from the directory service. It is also our most common way. If the portability is very good external call, you must first pass authentication, such as Oracle8i: String Ejburl = "sess_iiop: // localhost: 2481: ORCL / test / MyEJB "; String username =" scott "; String password =" tiger "; // Setup the environment Hashtable environment = new Hashtable (); // Tell JNDI to speak sess_iiop environment.put (javax.naming.Context.URL_PKG_PREFIXES , "oracle.aurora.jndi"); // Tell sess_iiop who the user is environment.put (Context.SECURITY_PRINCIPAL, username); // Tell sess_iiop what the password is environment.put (Context.SECURITY_CREDENTIALS, password); // Tell sess_iiop to use credential automationenvironment.put (context.security_authentication, servicectx.non_ssl_login); // Lookup the URL com.js per.ejb.MyEJBHome homeInterface = null; try {System.out.println ( "Creating an initial context"); Context ic = new InitialContext (environment); System.out.println ( "Looking for the EJB published as' test / MyEJB '""); HomeInterface = (com.jsper.ejb.myejbhome) Ic.lookup (ejburl);} catch (activationException e) {system.out.println ("Unable to Activate: E.getMessage ()); E.PrintStackTrace (); system.exit (1);} Remaining the way to call: try {// get an inTIALCONTEXT STRING URL = "T3: // localhost: 7001"; Properties H = New Properties ();

h.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put (Context.PROVIDER_URL, url); Context ctx = new InitialContext (h); System.out.println ( "Getting the EJBHome object ..." ); Com.jsper.ejb.ebhome tmp = (com.jsper.ejb.ejbHome) CTX.lookup ("Hello"); // Create Three Element Array Of Count Object EJB EJB = Tmp.Create (); System.out .println (Ejb.sayhello ());} catch (exception e) {E.PrintStackTrace ();} For the specific directory service, the agreement is related, in order to achieve the purpose of portability, I have to do some work, fortunately, no Need to do this. / * Blueski Note: This article comes from the Internet, the author is gentle and flying 2001-5-5 * /

转载请注明原文地址:https://www.9cbs.com/read-125126.html

New Post(0)