Author is not detailed
1. Introduction to this section
This chapter mainly introduces JBoss (free EJB servers), as well as how to install JBOSS, build your first EJB and client. About what is EJB, and how to develop, etc. These have many books on EJBs, which will not be explained here. The description information is running on the Windows platform, of course you can apply on other supported platforms (eg Linux, etc.).
2, about JBoss
JBoss supports EJB 1.1 and EJB 2.0 specification, which is a container and server that manages EJB. Similar to Sun's J2SDK Enterprise Edition (J2EE), JBoss Core Services is just a EJB server. JBoss does not include a Web container for Serverlers / JSP Page, of course, can be used with Tomcat or Jetty. JBoss needs a relatively small memory and hard disk space. You can run well on 64M memory and a few megabytes. Sun's J2EE requires a minimum memory of 128m, and 31M hard disk space. JBoss start speed is 10 times faster than J2EE. Moreover, it can embed the SQL database server for persistence Bean processing, which will start automatically when startup. (J2EE loads separate cloudscape sql server).
One of the very good features of JBoss can "hot" deployment, "hot" deployment means that in deploying bean just simple copying bean JAR files to deployment path, if Bean has been loaded, JBoss uninstall it, then load a new version BEAN. If you need more JBoss features, you can view websites about the JBoss Forum. If you need to support, you can pay the JBossGroup LLC if you need it.
This chapter step by step to help you start distributed with JBoss: how to create a simple bean, and how to deploy and test on the JBoss server.
3, essential conditions
JBoss is written in pure Java, requiring a Java system and JDK1.3 compatible. In order to be cheap and run, you need Ant 1.4 or above. How to get and install it in the next section. Of course, you have to develop EJB foundations.
4, install ANT
We have seen this name in many books and web pages, what is it used? Ant is a Java-based build tool. There is now a lot of Build tools, such as make, gnumake, nmake, jam, etc., and these tools are very excellent. Ant is a cross-platform Build tool. The reason that Ant can cross the platform because Ant no longer needs you to write shell commands, Ant's profile is XML-based task tree, which allows you to run a variety of tasks, the task run is the implementation of a specific task interface Objects are completed.
There are two versions of Binary and Source to download the latest version 1.5.2.
Binary version: http://ant.apache.org/old-releases/v1.5.2/binaries/
Source version: http://ant.apache.org/old-releases/v1.5.2/source/
About these two versions can be referred to http://ant.apache.org/. If you can't download it, you can't ask me QQ: 182244794, Email: Amaryllis@163.net.
After downloading Ant, you decompressed files, all files will be placed under an Apache-Ant-1.5.2 path. Ant documents are released in a DOCS / Manual directory, you can open web DOCS / Manual / INDEX.html to browse about the contents of the Ant document. I use the Windows system released under the C: /Apache-ant-1.5.2 path. 1) Set an ANT_HOME environment variable to point to your Ant directory.
2) Set the Java_Home environment variable to point to your JDK directory.
3) Add% ANT_HOME% / bin to the PATH environment variable.
4) When you do not run Ant without any parameters, Ant will find a file called build.xml in the current directory. If not found, there will be the following tips:
BuildFile: build.xml does not exist! Build failed
In order to test whether the Ant installation is successful, I built a new build.xml file, put it in the current directory% Ant_Home% / Samples. Build.xml content is as follows:
Public Class asimpleHelloObject
{
Public static void main (String [] args) {
System.out.println ("AsimpleHelloObject.main WAS Called");
}
Running Ant display results as follows:
C: /Apache-ant-1.5.2/samples> antbuildfile: build.xml
INIT:
AsimpleHelloObject:
[echo] wrote asimplehelloobject.java
Compile:
[javac] Compiling 1 Source File to C: /Apache-ant-1.5.2/samples
Run:
[java] asimplehelloobject.main Was called [echo] Ant Appears to be successfully installed
Builde Successful
Total Time: 11 seconds.
The ANT you installed here is successful. If there is a warning
Warning: Java_Home Environment Variable Is Not Set. If Build Fails Because Sun. * Classes Could Not Be Found You Will Need To Set The Java_Home Environment Variable To The Installation Directory of Java.
Indicates that you need to set the Java_Home environment, specify the JDK directory you installed. How to use all kinds of commands in Ant, I will write "ANT Document" introduced later.
5, install JBoss
1) Before installing and running JBOSS, you must check if JDK is installed, you need to set the JDK directory in the PATH environment.
2) Download the JBoss server. JBoss is a compressed file that you can download the latest version. In order to support Web Server, I use the Windows platform, so I downloaded JBoss3.0.6-Tomcat4.1.18. You can download it directly from http://prdownloads.sourceforge.net/jboss/. If you are using a UNIX / LUNIX platform, you can also download the appropriate JBoss server from this URL.
3), install
(1) Install under Lunix / Unix
A, you downloaded the JBoss server L for UNIX / LUNIX version to extract files to a temporary directory with JDK JAR tools.
B. Set JBoss_Dist points to the top directory after the JBoss decompressed.
C, then go to this directory,
CD jboss_dist / bin
sh run.shl
After running, the information is displayed as follows:
Bash-2.04 $ / bin / sh run.sh
JBoss_ClassPath =: run.jar: ../ LIB / Crimson.jar
JBoss.home = /TMP/JBOSS-2.2.2
Using jaas loginconfig: file: /tmp/jboss-2.2.2/conf/default/AUTH.CONF
Using Configuration "Default"
[Info] Java Version: 1.3.1, Sun Microsystems Inc.
[INFO] Java VM: Java Hotspot (TM) Server VM 1.3.1-B24, Sun Microsystems Inc.
[Info] System: Linux 2.2.16-22, i386
[Shutdown] shutdown hook added. [SHUTDOWN]
[Service Control] Registered with Server
[Service Control] Initializing 24 MBeans
...
[Service Control] Started 24 Services
[Default] JBoss 2.2.2 Started In 0m: 7S
(2) Installing under Windows
a, download jboss3.0.6-tomcat4.1.18.zip, then extract it to a directory with Winzip, I decompressed in the c: /jboss3.0.6-tomcat4.1.18 directory
b, basically no configuration after decompression, use the run.bat command under the bin directory.
c, run the run.bat command, please see if there is any abnormality in the background.
D. If you run successfully, please visit http: // localhost: 8083, a blank page will appear. Access http: // localhost: 8080 will appear in Tomcat, if Apache Tomcat / 4.1.12 - HTTP Status 500 - No Context Configured to Process this Request error, this is normal because you have not yet released. EAR or .war application. Of course, jboss-3.2.orc3_tomcat-4.1.18.zip does not have such a problem, because the web test file WebTest.ear visits http: // localhost: 8082 you can see in directory server / default / deploy / JBoss boots service. You can run http: // localhost: 8080 / jmx-console will appear related pages.
(3) Window NT and 2000 supplements
For NT and Win2K systems If you want to install NT or Win2K service, you can add JBOSS to the system service, avoid you to run the run.bat command each time you need to run the run.bat command, just start the JBoss server automatically when the system starts.
A, first you need to download the JavaService.exe file. l Edit a batch file, your named file name, deposit the same directory as JavaService.exe. I named jboss_tomcat_service.bat This file is @echo offecho --------
echo Usage:% 0 jdk_home tomcat_home (classic / hotspot / server) echo NOTE:. You MAY NOT use spaces in the path names If you know howecho to fix this, please tell me.echo JDK 1.4 does not come with hotpot server by default You Mustecho Install this seperately if you wish to use it.echo example:% 0 ../jdk ../jboss30 Hotspotecho --------
IF "% 1" == "uninstall" goto uninstall
IF "% 1" == "-uninstall" goto uninstall
IF "% 1" == "" GOTO USAGE
IF "% 2" == "" GOTO USAGE
IF "% 3" == "" GOTO USAGE
IF "% 1" == "-help" GOTO USAGE
IF "% 1" == "-?" GOTO USAGE
IF "% 1" == "/?" GOTO USAGE
: INSTALL
JavaService.exe -install JBoss306% 1 / JRE / BIN /% 3 / JVM.DLL -DJAVA.CLAS.PATH =% 1 / lib / Tools.jar;% 2 / bin / run.jar -start Org.jboss.Main -stop org.jboss.main -Method systemExit -Or% 2 / bin / out.txt -current% 2 / bin goto Eof
: Uninstall
JavaService.exe -uninstall jboss306
Goto EOF
: USAGE
echo -------- To Install JBoss 3.0.6 DO
Echo usage:% 0 JDK_HOME JBOSS_HOME (Classic / Hotspot / Server)
Echo Note: You May Not Use Spaces in The Path Names. If you know how echo to fix this, please tell me.
Echo JDK 1.4 Does Not Come with Hotpot Server by Default, You Must Echo Install this seperately if you wish to use it.
Echo EXAMPLE:% 0 ../jdk ../jboss30 Hotspot
echo --------
echo -------- To Uninstall JBoss 3.0.6 DO
Echo usage:% 0 uninstall echo --------
Goto EOF
: eof
e, run the following command
JBoss_Tomcat_Service C: /J2SDK1.4.0_02 C: /Jboss-3.0.6_tomcat-4.1.18 Server
Where c: /j2sdk1.4.0_02 is a directory installed by Java c: /jboss-3.0.6_tomcat-4.1.18 is a directory installed by JBoss, Server is a constant parameter
f, you will see the command successful command, you can see the JBoss306 service in Win2K or NT service, you can start it normally. Then look at various web pages (same as Windows installation) If successful, please congratulations, you have installed the JBoss server. (4) problem
If the service is installed, the web page is not correct. You need to try a few times, first run
JBoss_Tomcat_Service Uninstall
Then run
JBoss_Tomcat_Service C: /J2SDK1.4.0_02 C: /JBoss-3.0.6_tomcat-4.1.18 Server.
Chapter 1 supplement
After I read the first chapter, I didn't show it for build.xml. Now I added as follows:
1, download example source program
All examples of the source code, all in the file documentation-example.zip (Windows Platform) or Documentation-Example.Tar.gz (UNIX / LUNIX platform). You can download it directly from www.jboss.org. After downloading, put it in a directory. Download URL: http://www.jboss.org/docs/manual/files/documentation-example.zip1.1 Establishing Bean This section is mainly a simple EJB, you can view the code, this "intend" example, is a Simple and stateful session EJB. Its purpose is to calculate interest rates to borrow money according to the interest rate described. In fact, there is only one line of functionality in the entire package code.
1.2 Review EJBS before we view the code, let's review the EJB. In EJB minimal type, there must be three classes: Remote Interface, Home Interface, and Bean implementation classes. Remote interface is to provide the EJB method to the outside world, let the outside code to call, in this example, class name is org.jboss.interest.IRest. Home Interface is a class that manages the Remote Interface class. Includes operations such as establishment, deletion. In this example, the class name is org.jboss.interest.InterRestHome. The bean implementation class provides the implementation of all methods of Home Interface and Remote Interface. In this example, the class name is org.jboss.interest.InterRestBean.
Of course, a bean may also include other classes, or even other packages. But there must be three classes, other classes are established on these three classes. All classes are packaged into a JAR file, which is used to reflect the hierarchical relationship of the package with a directory structure. All classes in this example are packaged in the org.jboss.interest package, so they need to be in the directory ORG / JBOSS / INTEREST /.
There must be a META-INF directory before containing all the JAR files established. This directory stores deployment descriptors (typically called "EJB-JAR.XML"), and optional other XML files. These files tell the server about applying clear service information. For JBoss, the file name must be called "jboss.xml".
Deploy the JAR file to the JBoss Server. On the client, you need a JNDI.Properties file, this file tells you where the client program is initialized to find the JNDI naming service. From this service, the client will find the Interest Bean and return to Bean's Home Interface. Home interface is used to get a Remote Interface for Bean. It can use a remote interface to access the business approach provided by Bean.
1.3 EJB classes We need three classes: Remote Interface, Home Interface and Bean implementation classes. Remote Interface Remote Interface Class, File Name Interest.java. code show as below:
package org.jboss.docs.interest; import javax.ejb.EJBObject; import java.rmi.RemoteException;. / ** This interface defines the `Remote 'interface for the` Interest' EJB Itssingle method is the only method exposed to the outside world. The classInterestBean implements the method. * / public interface Interest extends EJBObject {/ ** Calulates the compound interest on the sum `principle ', with interest rate per period` rate' over `periods' time periods. This method also prints a message to standard output;. this is picked up by the EJB server and logged In this way we can demonstrate that the method is actually being executed on the server, rather than the client * / public double calculateCompoundInterest (double principle, double rate. Double Periods) Throws RemoteException;} The remote interface has only one business method CalculateCompoundinterest. Home interface file name INTERESTHOME.JAVA. code show as below:
package org.jboss.docs.interest; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; / ** This interface defines the 'home' interface for the 'Interest' EJB. * / public interface InterestHome extends EJBHome {/ ** Creates an instance of the `InterestBean 'class on the server, and returns a remote reference to an Interest interface on the client. * / Interest create () throws RemoteException CreateException;
Finally we give the bean implementation class, file name: InterestBean.java. code show as below:
package org.jboss.docs.interest; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; / ** This class contains the implementation for the 'calculateCompoundInterest' method exposed by this Bean It. includes empty method bodies for the methods prescribe by the SessionBean interface; these do not need to do anything in this simple example * / public class InterestBean implements SessionBean {/ ** Calulates the compound interest on the sum `principle ', with interest. rate per period `rate 'over` periods' time periods This method also prints a message to standard output;. this is picked up by the EJB server and logged In this way we can demonstrate that the method is actually being executed on the server. , rather than the client * / public double calculateCompoundInterest (double principle, double rate, double periods) {System.out.println ( "Someone called` calculateCompoundInterest '! ");. return principle * Math.pow (1 rate, periods ) - Princ iple;.} / ** Empty method body * / public void ejbCreate () {} / ** Every ejbCreate () method ALWAYS needs a corresponding ejbPostCreate () method with exactly the same parameter types * / public void ejbPostCreate () {} / ** Empty method body * / public void ejbRemove () {} / ** Empty method body * / public void ejbActivate () {} / ** Empty method body * / public void ejbPassivate () {} / ** Empty method Body * / public void setsessionContext (sessionContext sc) {}} Note that most of the way is empty. Because these methods are described in the sessionBean interface, they must exist in the InterestBean, but in this simple example, the specific content is not required.
2, deployment descriptor
When you edit the finished file, let's see the deployment descriptor. This file tells EJB Server which class should be awakening bean, home interface, and remote interface. If there is more than one bean in a package, it indicates how EJB is in addition to the other bean. In this simple example, there is only one EJB, so we don't have to care about this problem. Both most commercial EJB Server provide a graphical deployment tool to construct deployment descriptors, JBoss does not have an XML editor, but it can simply manually construct deployment descriptors. The following is an Interest Bean deployment descriptor: XML Version = "1.0" Encoding = "UTF-8"?>
The deployment descriptor file must be named "ejb-jar.xml" and must be placed under the directory Meta-INF. An error that everyone is prone is a directory name, some written into "meta_inf", "meta ~ inf" or "meta-inf" all these do not work properly.
We deploy an app on Server instead of an EJB. In this example, our application has only one EJB. In the deployment descriptor
Despite the deployment descriptor format file ejb-jar.xml is all common to all EJB Server. You can get a correct defined DTD from Sun, but not specify each special EJB Server. Nored Names from EJB-NAME to the deployed JNDI name, such as "[Application Name] / [Bean Name]". Provide a default behavior through JBOSS to work. These work come from EJB-JAR.XML. In the case of advanced configuration, you must define the JBoss description behavior, this behavior deploys the descriptor with JBoss.xml. In the advanced configuration, we will talk about the details of the JBoss.xml descriptor. Here we only configure the JBoss.xml descriptor JNDI name to access Interest EJB Home INRTERFACE. Using the default JNDI name to access EJB Home INRTERFACE, you need the same EJB-NAME with EJB-JAR.XML. For this simple example, the Interest Bean is positioned with JNDI initialization context (as "Interest"). We want to use "Interest / Interest" to make Home Interface, so we must use the JBoss.xml descriptor. In jboss.xml, re-override the default JNDI naming. In order to rewrite the default behavior, we use the EJB-Name element value in ejb-jar.xml as a JNDI naming of the Bean Home interface, you must explain the JNDI naming, to write the JBoss.xml descriptor as follows:
XML Version = "1.0" Encoding = "UTF-8"?>
This file instructions calling the Interest Bean is bound to the JNDI name of Interest / Interest. Our standard EJB-JAR.XML deployment descriptor with jboss descriptions JBoss.xml set the JNDI name of the Interest EJB HOME interface to "Interest / Interest". We now have an EJB class, which is the file that establishes the EJB file package.
3, package and deploy bean
The JAR package is a JAR file that contains EJB class files and deployment descriptors. In the sample, the example may be different from you to set the environment, so you need to modify the contents of Examples / Build / Build.xml. With regard to each Ant, I will introduce to everyone in the ANT document. About how to modify, let me tell you step by step. The following is a Windows platform. As for the UNIX / LUNIX platform, it is not described herein.
1) Before modification, the JBoss_Dist environment variable is first set, specify the directory of the JBoss you install. I installed C: /JBoss-3.0.6_tomcat-4.1.18, so jboss_dist is set to C: /Jboss-3.0.6_tomcat-4.1.18.
2) Do you have a web server such as Tomcat or Jetty. If you don't have installed, it is recommended that you install JBoss-3.0.6_tomcat-4.1.18, JBoss and Tomcat are directly combined, do not need to be installed separately, of course, JBoss-Jetty can be installed (jboss and jetty). Do you like what web server you like. 3) After completing the above two steps, let's discuss the modification of the Examples / Build / Build.xml file. Some of the code in the original file:
Pathelement location = "$ {jboss.dist} /client/jbossx-client.jar" />
Part of the file after modification:
5) After the completion, you can view the intend.jar file: jar -tvf interest.jar To deploy the bean to the Server, you must copy the interest.jar file to the jboss_dist / server / default / deploy directory. The server will find changes that have changed and re-deploy it.
6) When the JBoss Server is running, JAR is deployed by running Ant Intro-Interest-Deploy in an Examlpes / Build directory. During your deployment, you will see some information on the Server console.
7) If you find information that will fail, it is usually the deployment descriptor EJB-JAR.XML file structure is broken or lost or in the wrong directory.
8) If you are deploying on Server, we will write a simple client to test whether it works normally.
4, encoding and compile the client
A single EJB is not used, and we need at least a simple client to use its services. An EJB can be adjusted by other EJBs, ordinary JavaBeans, a JSP page, an Applet program or a separate application. In this example. We write a simple application. This application will establish an object of the Interest class. And perform it. When deploying beans, Server will bind the EJB Home interface to the JNDI name name and output this HOME interface, and can be called via RMI. The client code is as follows:
package org.jboss.docs.interest; import javax.naming.InitialContext; import javax.naming.Context; import javax.rmi.PortableRemoteObject; import org.jboss.docs.interest.Interest; import org.jboss.docs.interest. InterestHome;.. / ** This simple application tests the 'Interest' Enterprise JavaBean which isimplemented in the package 'org.jboss.docs.interest' For this to work, theBean must be deployed on an EJB server * / public class InterestClient { / ** This method does all the work. It creates an instance of the Interest EJB onthe EJB server, and calls its `calculateCompoundInterest () 'method, then printsthe result of the calculation. * / public static void main (String [] args ) {// Enclosing the whole process in a single `try 'block is not an ideal way // to do exception handling, but I do not want to clutter the program up // with catch blockstry {// Get a naming contextInitialContext JNDICONTEXT = New InitialContext (); System.out.println ("Got Context"); // Get a reason to the INTEREST BeanObject Ref = JNDI Context.lookup ( "interest / Interest"); System.out.println ( "Got reference"); // Get a reference from this to the Bean's Home interfaceInterestHome home = (InterestHome) PortableRemoteObject.narrow (ref, InterestHome.class) ; // Create an Interest object from the Home interfaceInterest interest = home.create (); // call the calculateCompoundInterest () method to do the calculationSystem.out.println ( "Interest on 1000 units, at 10% per period, compounded over 2 Periods IS: "); System.Out.println (Interest.calculateCompoundINTEREST (1000, 0.10, 2));} catch (eXception e) {system.out.println (e.tostring ());}}}
The client finds the InterestHome interface under JNDI named "Interest / Interest". If you provide a correct jboss.xml file, the Bean Home interface will be bound to this name. Otherwise, JNDI's name is "Interest". In order to connect JBoss JNDI, a initialization context must be established, which is based on JNDI.Properties based on the client's ClassPath path. This jndi.property file we put in Examples / Resources / JNDI.Properties. Code is as follows: java.naming.factory.initial = org.jnp.interfaces.NamingContextFactoryjava.naming.provider.url = localhost: 1099java.naming.factory.url.pkgs = org.jboss.naming: org.jnp.interfaces
Specifies the INITIALCONTEXTFACTORY (initialization context factory), URL, and packages for factory objects. Here URL is "localhost", or runs the host name (IP) of JBoss, and the default port used is 1099. For different RMI protocols, ensure that the "REF" object can be converted to an "InterestHome" object via the "Narrow" method. The client does not need to be necessary to be in the same package with the EJB class. So it must Import EJB Class class.
In order to run the ANT command. I have modified the examples / org / jboss / docs / intend / build.xml file, some of the original file:
After modification:
5, supplementary description
Each JAR file: 1) JBoss-j2ee.jar: It is a standard javax.ejb. * Includes EJB2.0 interfaces and classes.
2) JBoss-jaas.jar: It is the Java Identification and Authorization Service Security Class.
3) JBOSSSX-Client.jar: It is a JBOSSSX security extension client class.
4) JBoss-Client.jar: It is a JBoss EJB container agent and STUB client class.
5) JNP-Client.jar: It is a JBoss JNDI provider client class.
Now hide code, compile and deploy EJB, run the simple client for testing.
6 Conclusion
Finally, a summary is made.
Server:
Three files are required for each EJB (including, stateless session, or entity beans): bean implementation class, Remote interface class, home interface class. These classes and one or more deployment descriptors (EJB-JAR.XML, JBOSS.XML, etc.), these descriptors must be packaged into the JAR file together in the meta-inflicity. To deploy this JAR file, you only need to copy to JBoss / Server / Default / Deploy /.
Client:
1) Serve from JNDI to find the Home interface;
2) From the HOME interface, establish a new bean or get a bean already existing;
3) Use the obtained Remote interface to access the BEAN on the server to implement the business method in the class.
Before the client looks for the Home interface, the client must know how to locate from JNDI. So we provide a jndi.properties file. This file and some JAR under JBoss / Clientx must contain to ClassPath to allow the client to run.