First ejb3.0 example

Source: Internet
Author: User
Tags jboss

On April 9, July 2004, The EJB Expert Committee published the new ejb3.0 specification. In October 7, 2004, JBoss released the first preview version of jboss-EJB-3.0_Preview_1 supporting ejb3.0. In November 5, 2004, JBoss released a second preview version of jboss-EJB-3.0_Preview_2 supporting ejb3.0. Let's complete the first ejb3.0 program. EnvironmentThe environment implementing ejb3.0 requires the use of JBoss's jboss-4.0.1RC1 (already with RC2) and jdk1.5 and later. Jboss-4.0.1RC1 can be downloaded at http://www.jboss.org/, jdk1.5can be in java.sun.comto. We use it in a Windows environment, so we download the corresponding Windows version. Install JDK first. Download jdk-rj5_0_01-windows-i586-p.exe and install it in the corresponding directory. install it in D:/program files/Java/jdk1.5.0 _ 01. Then configure the environment. In my computer-> properties-> advanced-> environment variables-> system variables, set java_home = D:/program files/Java/jdk1.5.0 _ 01. In Path, set D: /program files/Java/jdk1.5.0 _ 01/bin. Download jboss-4.0.1rc1.zip and decompress it to D:/jboss4. Set jboss_home = D:/jboss4 in the same way. Because ant is used, Apache ant related execution programs need to be obtained. It can be found in the installation directory of Apache server, WebLogic or JBuilder. The best version is higher. In jbuilder2005, the path is D:/Borland/jbuilder2005/thirdparty/ANT/bin. It is still set in the path of the system variable and written after the JDK path. (Note: If the ant version is earlier, an error will be reported during compilation. My name is Apache ant version 1.6.2 compiled on July 16 2004 ). You can also use the jboss-EJB-3.0_Preview_2 package at http://www.jboss.org. Decompress the package docs, libtwo folder and release_notes.txtand install.html two description files. The docsfolder contains the main page file index.html, which contains ejb3.0's script ejb-3_0-edr-spec.pdf, the Guide folder tutorial (including the sample description file and source file), and the configuration instructions for the folder reference and hibernate3. The LIB folder is the ejb3-interceptors-aop.xml and ejb3.deployer required to deploy the ejb3.0 environment. Run the simple ejb3.0 example and we just need to copy the ejb3-interceptors-aop.xml and ejb3.deployer folders to D:/jboss4/ Server/All/deploy . We deploy the example in all, so we use Run-C all After JBoss is started, Environment configuration is completed. Program writing Docs The folder provides many ejb3.0 examples. Let's take stateless bean as an example. The stateless bean sample is in the stateless folder. The srcand runtime configuration file build.xmland jndiconfiguration file jndi.propertiesand stateless.html are included in the package. The source file includes calculator. java., Calculatorbean. Java, Calculatorlocal. Java , Calculatorremote. JavaAnd Client. Java . It is easy to write stateless beans in ejb3.0 environment. All bean types are homeless, so you only need to create a bean class and implement at least one interface. In calculatorbean. Java, we declare a stateless bean as @ stateless, and the EJB container will deploy this class as a 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 and the other is local. Now in ejb3.0, You can implement only one remote, one local, or both.   In calculatorremote. Java, you only need to identify the remote interface of calculator bean as @ remote. Import javax. EJB. Remote;   @ Remote Public interface calculatorremote extends Calculator { } Similarly, in calculatorlocal. Java, you only 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 Two JNDI bindings are required to correspond to the @ remote and @ local interfaces respectively. By default, JBoss uses the full name of the interface as the JNDI name. This allows you to easily call calculatorremote. Class. getname () to find a reference corresponding to the JNDI name. In client. Java, you can find that the full name of the remote interface is used to find the stateless bean. You can also note that the home interface does not exist. 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 directly execute this stateless bean. Running result This instance runs using the ant mechanism, so you need to configure build. xml. You must set classname in different run parameters in build. xml: <Target name = "run" depends = "ejbjar"> <Java classname = "org. JBoss. Tutorial. Stateless. Client. Client" fork = "yes" dir = "."> <Classpath refID = "classpath"/> </Java> </Target> JNDI The configuration is the same as that of 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 run window and go to the example directory, as shown in figure D:/jboss-EJB-3.0_Preview_2/docs/tutorial/stateless , Type the ant command to compile the file. After successful compilation, the following figure is displayed: build successful. After compilation, a build directory is added to the directory, which is the compiled class file and tutorial. ejb3 file. Run ant run and run the program. We can see that: Buildfile: Build. xml Prepare: Compile: Ejbjar: Run: [Java] 15:07:25, 296 info org. JBoss. remoting. invokerregistry [main ]-Failed to load soap remoting transport: ORG/Apache/axis/axisfault [Java] 1 + 1 = 2 [Java] 1-1 = 0 Build successful Total time: 3 seconds   Information [Java] 15:07:25, 296 info org. JBoss. remoting. invokerregistry [main ]-Failed to load soap remoting transport: ORG/Apache/axis/axisfault It is a bug in JBoss, so we can ignore it. They will be improved in the next version.   So far, I have completed an ejb3.0 example. We can find that ejb3.0 brings us a surprise. We don't have to configure a lot of tedious deployment descriptions and complex interfaces. I believe that more ejb3.0 surprises are waiting for us to explore.

Note: I am not liable for any of the above content from the Internet.
Article transferred from: http://blog.csdn.net/EmoSpring/archive/2006/06/07/778598.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.