Develop and deploy ejbs in JBoss

Source: Internet
Author: User
Tags define local
Author: Xiao wenpeng
JBoss is an open-source EJB server, which can provide a complete J2EE platform after being integrated with other servers. This article describes how to install and configure JBoss in Linux and how to develop and deploy ejbs on the JBoss platform.

As the most important component in the J2EE architecture, EJB is the core of distributed computing on the server. The ebj Server is an EJB container that controls the operation of ejbs and provides a series of system-level services such as transaction processing, database access, and security control.

The EJB Server is an important part of the J2EE application server. Sun's J2EE SDK, IBM's WebSphere, and BEA's WebLogic and other J2EE implementations are embedded with EJB servers. Although JBoss is not yet a complete J2EE application server, it is a complete EJB server. After integration with tomcat, Jetty, and other Web servers, JBoss can provide a complete J2EE platform.

The biggest advantage of JBoss is that it is a free software open source code and fully complies with J2EE specifications. JBoss has become a powerful force for enterprise-level applications on J2EE servers because of its powerful functions and excellent performance, as well as its combination with GNU projects such as Linux.


Install JBoss

JBoss installation and configuration are relatively simple. First download the JBoss package to the http://www.jboss.org. Currently, JBoss has a maximum version of 3.0. We recommend that you download binary software packages that are relatively stable, such as jboss2.4.4 and tomcat3.2.3. This avoids configuration issues between JBoss and tomcat after a single software package is downloaded.

After the downloaded package is decompressed to the/usr directory, the directory/usr/JBoss-2.4.4_Tomcat-3.2.3 is generated. To facilitate future use, change the directory to/usr/jb_tom. Under the/usr/jb_tom directory, you can find the/usr/jb_tom/JBoss and/usr/jb_tom/tomcat subdirectories, which are the root directories of JBoss and tomcat respectively.

Before JBoss is officially started, install JDK (JDK 1.3 or later is recommended) and set the environment variable ClassPath. The run_withtomcat.sh file in the/usr/jb_tom/jboss/bin directory is the startup script of JBoss and Tomcat. According to the default configuration of JBoss and Tomcat, after the script is run, JBoss and Tomcat HTTP services are started on ports 8080 and 8083 respectively. If everything is normal, enter http: // localhost: 8080 in the browser to display the Tomcat homepage, and enter http: // localhost: 8080 to display a blank page without errors.

Create an EJB

The following uses a simple stateless Session Bean as an example to describe how to write ejbs for the JBoss platform. According to the EJB specification, an EJB should contain at least three class implementations:

◆ Remote interface

The remote interface exposes the external interface of the entire EJB. In this example, the remote interface is encapsulated in the greet. Greet class.

◆ Local Interface

The Local interface describes the behavior of creating, managing, and destroying ejbs. In this example, the local interface is encapsulated in the greet. GreetHome class.

◆ Bean class

The Bean class implements all the methods defined in the remote interface. In this example, the Bean class is encapsulated in the greet. GreatBean class.

EJB is provided as a JAR package at the time of release. The EJB server requires that the JAR package contain all the class files and corresponding deployment files, and organize them according to the directory structure during EJB development. In our example, all class files are under the greet directory, the deployment file is under the META-INF directory, the corresponding directory structure is:


     
      greet  +-- Greet.java  +-- GreetHome.java  +-- GreetBean.javaMETA-INF  +-- ejb-jar.xml  +-- jboss.xml
     

1. Define remote interfaces

The interfaces exposed by EJB to the outside world are defined in the remote interface. In this example, EJB only provides one interface to the outside world: calculateMagic, and the corresponding source file is Greet. java. The Code is as follows:


     
      
Package greet; import javax. EJB. ejbobject; import Java. RMI. remoteException;/*** this interface defines the remote interface public interface greet extends ejbobject {public double calculatemagic (double seed) throws RemoteException for 'greet ;}
     

2. Define local interfaces

The Local interface of EJB describes the behaviors of creating, managing, and destroying ejbs. The local interface should at least provide the create () method to describe the behaviors of ejbs during creation. In this example, the source file corresponding to the local interface is GreetHome. java. The Code is as follows:


     
      package greet;import java.io.Serializable;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBHome;public interface GreetHome extends EJBHome { Greet create() throws RemoteException, CreateException;}
     

3. Implement bean classes

The real work of EJB is implemented in the Bean class, and the Bean class must provide corresponding implementation for all methods defined in the remote interface. In this example, the source file of the Bean class is GreetBean. java:


     
      package greet;import java.rmi.RemoteException; import javax.ejb.SessionBean;import javax.ejb.SessionContext;public class GreetBean implements SessionBean { public double calculateMagic(double seed) {  System.out.println ("Someone called `calculateMagic!'");return seed * Math.random(); } public GreetBean() {}public void ejbCreate() { System.out.println("Create Greet EJB.");}public void ejbRemove() { System.out.println("Remove Greet EJB.");}public void ejbActivate() { System.out.println("Activate Greet EJB");} public void ejbPassivate() {  System.out.println("Passivate Greet EJB"); }/*** Set context for `Greet' EJB*/ public void setSessionContext(SessionContext sc) {  System.out.println("Set context for Greet EJB"); }}
     

After the EJB Interface Definition and Bean class implementation are provided, use the following command to compile these. java files and generate the corresponding. class file:


     
      javac *.java -classpath //usr/jb_tom/jboss/lib/ext/jboss-j2ee.jar:.
     

 

Deployment descriptor

According to the EJB specification, to successfully deploy the EJB to the EJB server, the corresponding deployment descriptor must be provided for the EJB server. The deployment descriptor describes the EJB to be deployed, including the remote descriptor, local descriptor, and Bean class of the EJB. The EJB server can correctly deploy the EJB only after obtaining the basic information. Therefore, compiling the EJB descriptor is an essential part in EJB development.

For different EJB servers, the deployment descriptors required for deploying the same EJB may be different. On the JBoss platform, any EJB to be deployed must provide ejb-jar.xml and jboss. xml files, both in the META-INF directory of the JAR package, are used to briefly describe the EJB to be deployed.

Ejb-jar.xml

The ejb-jar.xml is the standard deployment descriptor defined by the EJB specification, which is required for deploying ejbs on any EJB server. The ejb-jar.xml code used in this example is as follows:


     
      <?xml version="1.0" encoding="Cp1252"?><ejb-jar><description>jBoss test application </description><display-name>Test</display-name><enterprise-beans><session><ejb-name>GreetEJB</ejb-name>

JBoss. xml

While the ejb-jar.xml is common to all EJB servers, it does not provide all information for the EJB server to be deployed. To allow more flexible control over EJB deployment, most EJB servers require the EJB developer to provide another file to describe the EJB to be deployed, in JBoss, the file is jboss. xml, which is also located in the META-INF directory in the JAR package. Jboss. xml describes the JNDI name and durability of the EJB. The jboss. xml used in this example is as follows:


     
      <?xml version="1.0" encoding="ISO-8859-1"?><jboss><enterprise-beans><session><ejb-name>GreetEJB</ejb-name><jndi-name>GreetingEJB</jndi-name></session><secure>false</secure></enterprise-beans><resource-managers/></jboss>
     

Deploy EJB

The last step for developing EJB is to compress all the class files and corresponding deployment descriptors into JAR packages and deploy them on the EJB server. In this example, the JAR package can be generated using the following command:


     
      jar cf greetejb.jar greet/*.class META-INF/*.xml
     

This command compresses the. class file under the greet directory and the. xml file under the META-INF Directory into the greetejb. jar file. If you want to know whether the generated JAR package correctly contains all the files, run the following command:

     
      jar cvf greetejb.jar
     

To view the files contained in greetejb. jar. If the following similar information is obtained, the required files are correctly included in the compressed package. The information is as follows:


     
      0 Sun May 24 15:32:10 CST 2002 META-INF/68 Sun May 24 15:32:10 CST 2002 META-INF/MANIFEST.MF1007 Sun May 24 14:35:46 CST 2002 greet/GreetBean.class209 Sun May 24 14:35:46 CST 2002 greet/Greet.class251 Sun May 24 14:35:46 CST 2002 greet/GreetHome.class493 Sun May 24 08:40:00 CST 2002 META-INF/ejb-jar.xml303 Sun May 24 08:43:22 CST 2002 META-INF/jboss.xml
     

The deployment of the generated JAR package on JBoss is quite simple. You only need to copy the file to the deploy directory of JBoss. The command is as follows:


     
      cp greetejb.jar /usr/jb_tom/jboss/deploy/
     

JBoss supports hot deployment. Changes to all files in the deploy directory are automatically detected by JBoss and corresponding ejbs are performed according to the detection results.

     
      [INFO,ContainerFactory] Deploying GreetEJB[INFO,GreetEJB] Initializing[INFO,GreetEJB] Initialized[INFO,GreetEJB] Starting[INFO,GreetEJB] Started
     

So far, the deployment of ejbs on the JBoss platform is complete. If you want to know whether the ejbs can work normally, you need to compile a dedicated client program for testing.

Test EJB

The value of EJB lies in its ability to provide corresponding services to its customers, which is widely used, it can be another EJB, common JavaBean, JSP page, Applet, or standard Java application. GreetClient. java is the client program of the deployed EJB. The complete source code is as follows:


     
      import javax.naming.*;import java.util.Hashtable;import javax.rmi.PortableRemoteObject; import greet.*;class GreetClient{ public static void main(String[] args) {  System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.Naming ContextFactory");  System.setProperty("java.naming.provider.url", "localhost:1099");try {   // Get a naming context   InitialContext jndiContext = new InitialContext();  System.out.println("Got context"); Object ref  = jndiContext.lookup("GreetingEJB");  System.out.println("Got reference"); GreetHome home = (GreetHome)    PortableRemoteObject.narrow (ref, GreetHome.class);   Greet greet = home.create();   System.out.print("The magic number from server is ");System.out.println(greet.calculateMagic(123.456));  } catch(Exception e) {   System.out.println(e.toString());  } }}
     

Use the following command to compile the EJB client program:


     
      javac GreetClient.java /-classpath /usr/jb_tom/jboss/lib/ext/jboss-j2ee.jar:.
     

If everything is normal, you can run the client program to test the EJB. The command is as follows:


     
      java -cp / $CLASSPATH:/usr/jb_tom/jboss/client/jboss-client.jar:. /GreetClient
     

Summary

This article uses a stateless Session Bean as an example to describe the entire process of developing and deploying ejbs on the JBoss platform, this article briefly introduces JBoss installation, EJB creation, EJB deployment, and EJB testing. As an open-source EJB server, JBoss has been accepted by more and more enterprises. Successful JBoss-based cases are not uncommon. For more information about JBoss, visit the JBoss website http://www.jboss.org.

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.