The first EJB3.0 example
In July 2004, the EJB Expert Committee announced the new EJB3.0 specification herb. On October 7, 2004, JBoss released the first preview version of EJB3.0 JBoss-EJB-3.0_PreView_1. November 5, 2004, JBoss released the second preview version of EJB3.0 JBoss-EJB -3.0_preview_2.
Let's complete the first EJB3.0 program.
Match the environment
Environmental requirements for implementing EJB3.0 use JBoss JBoss-
4.0.1
RC1 (already RC2) and JDK1.5 and higher. JBoss-4.0.1rc1 can be downloaded on www.jboss.org, and JDK1.5 can be obtained in java.sun.com. We use it in a Windows environment, so we download the corresponding Windows version.
First install the JDK. Download JDK-1_5_0_01-Windows-i586-P.exe, install to the corresponding directory, I installed to D: / Program Files / Java / JDK
1.5.0
_01. Then configure the environment. In my computer -> attribute -> Advanced -> Environment Variables -> System Variables, Set Java_Home = D: / Program Files / Java / JDK1.5.0_01, Path will D: / Program files / java / jdk1.5.0 _01 / bin; written in the forefront.
Download jboss-4.0.1rc1.zip, directly decompress to D: / JBoss4. Setting JBoss_Home = D: / JBoss4.
Since the Ant is used, the APACHE ANT related execution is also needed. In the installation directory of Apache Server, WebLogic or JBuilder, the best version is higher. I use JBuilder 2005, the path is D: / borland / jbuilder2005 / thirdparty / Ant / bin; still set in the PATH of the system variable, written after the JDK path. (Note: If the Ant version is lower, it will compile time, my Apache Ant Version
1.6.2
Compiled on July 16 2004).
Also use to use the JBoss-EJB-3.0_preview_2 package, you can get at www.jboss.org. After decompression, including DOCS, LIB two folders and release_notes.txt and install.html two instructions. There is a homepage file index.html in the DOCS folder, including EJB3.0 Specification Herb EJB-3_0-EDR-Spec.pdf, Guide Folder Tutorial, Configuration Description Folder Reference and Hibernate3 Folder. The lib folder is EJB3-Interceptors-Aop.xml and EJB3.Deployer required to deploy the EJB3.0 environment.
Run simple EJB3.0 example, we only need to copy EJB3-Interceptors-Aop.xml and EJB3.Deployer folders to D: / JBoss4 / Server / All / Deploy.
We deploy the example under all, so we use when running JBOSS.
Run -c all
After starting JBOSS, we complete the configuration of the environment.
Programming
There are examples of various aspects of EJB3.0 under the DOCS folder. We use the stateless bean as an example.
The stateless bean scrip is in the Stateless folder. Including SRC and running configuration files of storage source files build.xml, JNDI profile jndi.properties and instruction files stateless.html.
Source files include Calculator.java, CalculatorBean.java, CalculatorLocal.java, CalculatorRemote.java, and Client.java. It is easy to write stateless beans in an EJB3.0 environment. All bean types are Homleses so you only need to create a bean class and implement at least one interface.
In CalculatorBean.java, we declare that a stateless bean only needs to be declared as @ stateless, and the EJB container will deploy this class as stateless bean.
Import javax.ejb.stateless;
@StateLess
Public Class CalculatorBean Implements CalculatorRemote, CalculatorLocal
{
Public Int Add (int X, int y)
{
Return X Y;
}
Public int subtract (int X, int y)
{
Return X - Y;
}
}
This CalculatorBean implements two interfaces. One is Remote, one is LOCAL. Now you can implement a Remote, a Local or two implementations in EJB3.0.
You only need to identify it as @Remote.java definition.
Import javax.ejb.remote;
@Remote
Public Interface CalculatorRemote Extends Calculator
{
}
Also in CalculatorLocal.java, you just need to identify a @local to define the local interface of Calculator Bean.
Import javax.ejb.local;
@Local
Public Interface CalculatorLocal Extends Calculator
{
}
Calculator bean requires two JNDI bindings to correspond to @Remote and @local interfaces. The default, JBoss uses the full name of the interface as the name of JNDI. This makes you easily call CalculatorRemote.class.getname () to find a reference to a JNDI name.
In Client.java, you can find a full name using the Remote interface to find the state of BEAN, or notice that there is no HOME interface.
Import org.jboss.tutorial.Stateless.Bean.calculator;
Import org.jboss.tutorial.Stateless.Bean.calculatorRemote;
Import javax.naming.initialcontext;
Public Class Client
{
Public static void main (string [] args) Throws Exception
{
InitialContext CTX = New InitialContext ();
Calculator Calculator = (Calculator) ctx.lookup (CalculatorRemote.class.getName ());
System.out.Println ("1 1 =" Calculator.Add (1, 1));
System.out.println ("1 - 1 =" Calculator.Subtract (1, 1));
}
}
Now you can perform this stateless bean directly.
operation result
This instance uses the ANT mechanism to run, so you need to configure build.xml. Different build.xml need to set ClassName: java> target> JNDI's configuration is the same JNDI.Properties: Java.naming.factory.initial = org.jnp.interfaces.namingContextFactory Java.naming.factory.url.pkgs = org.jboss.naming: Org.jnp.interfaces Java.naming.Provider.URL = localhost Open the command to run the window, go to the sample directory, such as D: /jboss-ejb-3.0_preview_2/docs/tutorial/stateless, type the command ANT compilation file. Compile success will display: build successful. After successful compilation, you will be more than a build directory, which is a file that compiled Class files and tutorial.ejb3. Then run the Ant Run, execute the program, we can see: Buildfile: build.xml Prepare: Compile: ejbjar: Run: [java] 2005-01-12 15: 07: 25, 296 info org.jboss.remoting.invokerregistry [main ] - Failed to load soap remote transport: ORG / APACHE / AXIS / AXISFAULT [java] 1 1 = 2 [java] 1 - 1 = 0 Build Successful Total Time: 3 second SECONDS Herein information [java] 2005-01-12 15: 07: 25, 296 info org.jboss.remoting.invokerregistry [main ] - Failed to load soap remote transport: ORG / APACHE / AXIS / AXISFAULT Is a bug in jboss, we can't care. They will be improved in the next version. At this point, I completed an example of EJB3.0. We can find that EJB3.0 brings us a surprise. We don't have to configure a large number of cumbersome deployment descriptions and complex interfaces. I believe more EJB3.0 surprises are waiting for us to discover the network, waiting for practice to prove