Author: Xiaowen Peng JBoss is an open source EJB server, after its integration with other servers can provide a complete J2EE platform. This article describes how to install and configure JBoss in a Linux environment, and how to implement EJB development and deployment on the JBOSS platform. As the most important component in the J2EE architecture, EJB is the core of implementing server-side distributed computing. The EBJ server is an EJB container that controls the operation of EJB and provides a series of system-level services such as transaction, database access, security control. The EJB server is an important part of the J2EE application server. Sun J2EE SDK, IBM's WebSphere, and the J2EE implementation of BEA's WebLogic, etc. embed EJB servers. Although JBOS is currently not a complete J2EE application server, it is a complete EJB server that is integrated with Tomcat, Jetty and other web servers, it is possible to provide a complete J2EE platform. The biggest advantage of JBoss is that it is the free software open from the source code and fully follows the J2EE specification. Due to the powerful function and excellent performance of JBoss, and the combination of GNU projects such as Linux, there is currently a powerful force for enterprise applications in J2EE server.
Install JBoss
JBoss installation and configuration is relatively simple. First download the JBoss package to http://www.jboss.org. At present, the highest version of JBoss is 3.0. It is recommended to download a relatively stable JBoss 2.4.4 and Tomcat 3.2.3 integrated binary packages, which avoids the configuration problem between JBoss and Tomcat after downloading a single package. Download Software Packages After compression to / usr directory, you will generate /usr/jboss-2.4.4_tomcat-3.2.3. To make it easy for future use, rename this directory as / usr / jb_tom. In / usr / jb_tom directory, you can find / usr / jb_tom / jboss and / usr / jb_tom / tomcat two subdirectories, which are JBoss and Tomcat's root directory. Before you start JBoss, you should install JDK first (recommended to install the JDK 1.3 or more) and set the environment variable ClassPath. The Run_WITHTOMCAT.SH file under the / usr / jb_tom / jboss / bin directory is the startup script of JBoss and Tomcat. Follow JBoss and Tomcat's default configuration, start the script after starting the JBoss and Tomcat's HTTP service at 8080 and 8083 ports, respectively. . If everything is normal, enter http: // localhost: 8080 in the browser, and enter http: // localhost: 8080, there is no error-free blank page.
Create EJB
The following is a simple stateless session bean as an example, telling how to write EJB for the JBoss platform. According to the EJB specification, at least one of the three classes in an EJB should include the following three classes: ◆ Remote interface Remote Interface exposes the entire EJB external interface, in this example, the remote interface is encapsulated in the Greet.greet class. ◆ The local interface local interface describes the behavior when creating, managing, and destroying EJB, in this example, local interface is encapsulated in the Greet.grethome class. ◆ The Bean class bean class implements all methods defined in the remote interface, in this example, the bean class is encapsulated in the Greet.greatbean class. EJB is provided in the form of a JAR package at the time of publication. The EJB server requires that all class files and corresponding deployment files are included in the JAR package, and to organize in accordance with the directory structure developed in EJB. In our example, all class files are located in the GREET directory, and the deployment file is located in the meta-inflicity, the corresponding directory structure is:
Greet
- Greet.java
- GreetHome.java - GreetBean.java
META-INF
- ejb-jar.xml
- jboss.xml
1. Define the interface exposed to the outside interface to define in a remote interface. The EJB in this example only provides an interface to Calculatemagic, 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 a remote interface for 'GREET'
Public Interface Greet Extends EJBOBJECT
{
Public Double Calculatemagic (Double Seed) THROWS RemoteException;
}
2. Define the local interface of the local interface EJB to describe the behavior of creation, managing, and destroy EJB, and the local interface should provide at least the CREATE () method to describe the behavior of the EJB creation. 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 Gree EXTENDS EJBHOME
{
Greet Create () throws remoteException, CreateException;
}
3. Implementing the work of the bean class EJB is implemented in the Bean class, the Bean class must provide the corresponding implementation for all methods defined in the remote interface. The source file corresponding to the Bean class in this example 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 giving the interface definition of the EJB and provides the specific implementation of the Bean class, use the following command to compile these .java files, generate the corresponding .CLASS file:
Javac * .java -classpath /
/usr/jb_tom/jboss/lib/ext/jboss-j2ee.jar :..
Deployment descriptor
According to the EJB specification, if you want to successfully deploy EJB to the EJB server, you must provide the corresponding deployment descriptor for the EJB server. The deployment descriptor describes the EJB to be deployed, including information such as the remote descriptor of the EJB, a local descriptor, and a bean class. Since the EJB server can only complete the EJB deployment only after obtaining these basic information, the writing EJB descriptor must be an indispensable part of development EJB. For different EJB servers, the deployment descriptor required to deploy the same EJB may not be the same. On the JBoss platform, any EJB that will be deployed must provide EJB-JAR.XML and JBOSS.XML two files, both files in the meta-inflicity in the JAR package, used to deploy EJBs A brief description. EJB-JAR.XML EJB-JAR.XML is a standard deployment descriptor defined by the EJB specification, and the deployment descriptor is required when deploying EJB on any EJB server. The ejb-jar.xml code used in this example is as follows:
XML Version = "1.0" encoding = "cp1252"?>
session>
enterprise-beans>
ejb-jar>
JBoss.xml Although ejb-jar.xml is common to all EJB servers, it does not provide all information for EJBs that will be deployed for EJB servers. In order to be more flexible to EJB deployment, most EJB servers require EJB developers to provide another file to describe the EJB that will deploy, in JBoss, this file is jboss.xml, it is also in JAR package The Meta-Inf Directory in the Meta-INF. The JBoss.xml can be described with respect to the JNDI name and corresponding persistence of the EJB, and the jboss.xml used in this example is as follows:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
enterprise-beans>
jboss>
Deploy EJB
The final step of developing EJB is to compress all class files and corresponding deployment descriptors into a JAR package, and then deploy them on the EJB server. In this example, the generation of JAR package can be implemented by the following command:
JAR CF GREETEJB. Jar Greet / *. Class meta-inf / *. XML
This command compresses the .class file in the GREET directory to the Gretejb.jar file under the .xml file under the Meta-INF directory. If you want to know if the generated JAR package correctly contains all files, you can use the command:
JAR CVF Gretejb.jar
To view the files contained in Greejb.jar. If the following information is obtained, the required files have been properly included in the compression 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.mf
1007 Sun May 24 14:35:46 CST 2002 Greet / GreetBean.class
209 Sun May 24 14:35:46 CST 2002 Greet / Greet.class
251 Sun May 24 14:35:46 CST 2002 Greet / GreetHome.Class
493 Sun May 24 08:40:00 CST 2002 Meta-Inf / EJB-jar.xml
303 Sun May 24 08:43:22 CST 2002 meta-inf / jboss.xml
The generated JAR package is quite simple in the deployment of JBoss, just copy the file to the DEPLOY directory of jboss, the command is as follows:
CP Gretejb.jar / usr / jb_tom / jboss / deploy /
JBoss supports hot deployment, all files in the deploy directory are automatically detected by jboss, and perform corresponding EJBs according to the test results
[Info, containerfactory] Deploying Gretejb
[Info, gretejb] initializing
[Info, gretejb] initialized
[Info, GREETEJB] Starting
[Info, gretejb] started
At this point, the deployment of EJB on the JBoss platform is complete, and if you want to know if the EJB can work properly, you need to test a specialized client program to test.
Test EJB
The value of EJB is to provide its customers with the corresponding service, and the range of EJB customers is quite wide, which can be additional EJBs, ordinary JavaBean, JSP pages, applets, or standard Java applications. GreetClient.java is a client already deployed EJB client, whose 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 ());
}
}
}
Compile the EJB client program with the following command:
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 EJB, the command is as follows:
Java -cp /
$ ClassPath: /usr/jb_tom/jboss/client/jboss-client.jar :. /
GreetClient
summary
In this paper, in a stateless session bean as an example, the whole process of developing and deploying EJB on the JBoss platform, the JBoss installation, EJB creation, EJB deployment, and EJB test have been briefly introduced. As an open source EJB server, JBoss has begun to be accepted by more and more companies, and the JBoss-based successful case is not uncommon. For more information on jboss, please visit the JBoss website http://www.jboss.org.