Program analysis example (UML analysis of EJB)
Client program
/ *
* @ (#) ConverterClienter.java
* /
Import javax.naming.context;
Import javax.naming.initialcontext;
Import javax.rmi.rtableremoteObject;
Import converter;
Import converterhome;
Public Class ConvertClient
{
Public static void main (String [] Argv)
{
Try
{
Context initial = new initialcontext ();
Object objref = initial.lookup ("myconverter");
Converterhome Home =
(Converterhome) PortableRemoteObject.narrow (Objref,
Converterhome.class;
Converter currencyconverter = home.create ();
Double amount = currencyconvertor.dollartormb (100.00);
System.out.println (String.Valueof (Amount));
Amount = currencyconverter.rmbtodollar (100.00);
System.out.println (String.Valueof (Amount));
Currencyconverter.remove ();
}
Catch (Exception E)
{
System.err.Println ("Caught An Unexcepted Exception!");
EX.PrintStackTrace ();
}
}
}
// Analysis
Context initial = new initialContext (); object objref = initial.lookup ("myconverter");
When deploying EJB, the location of the EJB is registered in the name service using JNDI. This process is called JNDI binding. The bound method is similar to the object name-> object value, in the deployment phase of this example, the name of the EJB has been named myconverter.
So the first step to use EJB is to find EJB (MyConverter). Also use the JNDI (Java Naming and Directory Interface = Java Name and Directory Interface) to find the location of the EJB. JNDI is just a Java API, not a specific implementation, which provides a cross-service interface for the underlying service provider interface. It can be inserted into the JNDI layer into the JNDI layer into the JNDI layer by implementing the JNDI service provider.
Naming Services is similar to the phone number book, you need to get a lookup environment (just like which phone book is to be determined).
In J2EE, the Environment Interface Class is defined by the javax.naming.context interface, and the Javax.naming.Naming.initialContext class is implemented by the initial environment javax.naming.initialcontext class for each J2EE application. Provide a specific JNDI name lookup service.
CONTEXT INITIAL = New InitialContext (); // Using created factory mode?
Object objref = initial.lookup ("MyConverter"); // Since you can bind a variety of objects, the return value of Java is Object. ConverterHome Home = (ConverterHome) PortableRemoteObject.narrow (Objref, ConverterHome.Class);
After finding EJB, you have to locate the HOME interface of the EJB, which is an EJB object found for JNDI. Use a HOME interface type of this EJB to reference the home interface converterhome.class.
Converter currencyConverter = home.create (); double amount = currencyConverter.DollarToRMB (100.00); System.out.println (String.valueOf (amount)); amount = currencyConverter.RMBToDollar (100.00); System.out.println (String. Valueof (Amount); currencyconverter.remove ();
Then the Home Interface Home, create the variable of the remote interface Converter type of the EJB CURRENCYCONVERTER. The client calls the operation of the remote interface CURRENCYCONVERTER (Double Amount = CURRENCYCONVERTER.DOLLLARTORMB (100.00);) The remote interface is used to call the EJB's operation CONVERTEREJB.DOLLARTORMB (100.00), return the result, to use EJB application logic .
Finally, don't forget to destroy the EJB object currenconverter.remove ().
Remote interface
/ *
*@(#)Converter.java
* /
Import javax.ejb.ejbobject;
Import java.rmi.remoteexception;
Public Interface Converter EXTENDS EJBOBJECT
{
Public Double DollRTORMB (Double Dollars) Throws RemoteException;
Public Double Rmbtodollar (Double Rmb) throws RemoteException;
}
Local interface
/ *
* @ (#) ConverterHome.java
* /
Import javax.io.serializable;
Import java.rmi.remoteexception;
Import javax.ejb.createException;
Import javax.ejb.ejbhome;
Public Interface Converterhome Extends EJBHOME
{
Converter Create () throws remoteExceptoin, CreateException;
}
EJB
/ *
* @ (#) ConvertEREJB.JAVA
* /
Import java.rmi.remoteexception;
Import javax.ejb.sessionbean;
Import javax.ejb.sessionContext;
Public Class ConvertRejb Implements SessionBean
{
Public Double DollartORMB (Double Dollars) {
Return DOLLAR * 8.3000;
}
Public Double RmbTodollar (double RMB)
{
RETURN RMB / 8.3000;
}
Public convertEREJB () {}
Public void ejbcreate () {}
Public void ejbremove () {}
Public void ejbactivate () {}
Public void ejbpassiVate () {}
Public void setsessionContext (sessontext sc) {}
}