Deploy an EJB with JBOSS and build a client application to use it.
BY Budi Kurniawan
Many people often encounter problems when building Enterprise JavaBeans (EJB), and to deploy the first Enterprise Bean and deploy it on the selected J2EE server. First, compared with other J2EE technology, the learning curve of EJB is steep. Second, when writing an Enterprise Bean, you have to handle a lot of files. Many authors about the book of EJBs discuss this technology in the first few chapters of their book, but few people teach you how to make your first EJB. As a technical guiding article, this paper mainly tells how to develop a simple EJB and deploy it with JBoss, rather than exploring EJB specification and technology. The process of deploying other J2EE servers is the same, just some tiny changes.
EJB is one of the main technologies in J2EE. You may have read a lot of articles about this exciting technology, and now you may be prepared to take the next step: develop a real EJB and run it in an EJB container. Through this article, you will learn how to develop an EJB, deploy it, and build a client application to use it.
This EJB used in this article is very simple: called StringProcessor, its unique function is to convert a string to uppercase. That is, enter "EJB", StringProcessor converts it to "EJB". This paper focuses on the entire development process, so I intend to make the code simply. You can prove the concept we have to talk about through this applet.
figure 1.
Directory structure of your small project
To develop and deploy an EJB application, you must experience four steps:
1. Write a class for your bean.
2. Write a deployment descriptor.
3. Create a deployment file.
4. Deploy bean.
In addition, I will tell how to build a client application, so you can see how your bean is running. I will tell it step by step. Let us take an example in this article, first build a directory called Workplace as your work area. You can put your bean classes in the com.javapro.ejb package, so you need to build a few directory for the source file and Java class (see Figure 1). After doing these, the directory will contain 10 files (see Figure 2). Now transfer to the steps required to build your EJB.
figure 2.
project files
Write a class
As you know, an Enterprise Bean is constructed by three classes (see
Listing 1,
2 and
3). You should save these files in the Workplace / COM / JavaPro / EJB directory. When you compile the source file, you should do these steps. (Suppose you have included javac.exe in your "path" environment variable. Note that the path here is for Windows. If you are in Linux / Unix, you should modify it. This will not be a big big problem.)
1. If you are in Windows, open a command prompt.
2. Change the directory to your Workplace directory.
3. The first source file to be compiled is StringProcessor.java because the other two files require StringProcessor classes. Compilation requires JBoss-j2ee.jar in ClassPath. This file containing the EJB API can be found in the client directory of the JBOSS installation directory. In this example, I use JBoss 2.4.4 and install it in C: /JBoss-2.4.4 (in JBoss 3.0, the JBoss-J2EE.jar file is also located in the client directory of the installation directory) : Javac -classpath c: /jboss-2.4.4/client/Jboss
J2EE.JAR COM / JAVAPRO / EJB / STRINGPROCESSOR.JAVA
4. Compile StringProcessorHome.java files with the following command (note that it requires the StringProcessor.class file under Workplace, so you need to provide the current directory, indicating the directory through the period after jboss-j2ee.jar):
Javac-Classpath C: /Jboss-2.4.4/client/
JBoss-j2ee.jar ;.
COM / JavaPro / EJB / STRINGPROCESSORHOME.JAVA
5. Compile StringProcessorBean.java files. Command is similar to the command in step 4:
Javac-Classpath C: /Jboss-2.4.4/client/
JBoss-j2ee.jar ;.
COM / JavaPro / EJB / STRINGPROCESSORBEAN.JAVA
Writing a deployment descriptor A deployment descriptor using an EJB application is EJB-JAR.XML (see List 4). That is, it is an XML file. You must save this file into the Workplace / Meta-Inf directory.
Creating a deployment file To deploy your EJB, you need to pack the Java class and deployment descriptors into a JAR or EAR file. Enter the following command from the Workplace directory (assuming jar.exe in your PATH environment variable):
JAR CFV StringProcessor.jar
COM / JavaPro / EJB / *. Class meta-inf / ejb-jar.xml
If everything is normal, the following is information from JAR applications:
Added Manifest
Adding: COM / JavaPro / EJB / STRINGPROCESSOR.CLASS (in =
270) (OUT = 194) (Deflated 28%)
Add:
COM / JavaPro / EJB / STRINGPROCESSORBEAN.CLASS (in =
897) (OUT = 446) (Deflated 50%)
Add:
COM / JavaPro / EJB / STRINGPROCESSORHOME.CLASS (in =
301) (OUT = 205) (Deflated 31%)
Adding: meta-inf / ejb-jar.xml (IN = 577) (OUT =
255) (DEFLATED 55%)
Deploying your bean is a simple in JBoss2.4. You only need to copy the .jar file to the deployment directory in the JBoss installation directory. You don't even need to restart JBoss. If you copy this file to the deployment directory when you run in JBoss, it will issue this information (unfortunately, you can't get free to deploy your EJB directive with JBoss 3.0 on JBoss website):
[Info, autodeployer] auto deploy of file: / c: / jboss-
2.4.4 / Deploy / StringProcessor.jar [INFO, J2EEDEPLOYER] Deploy J2EE Application:
File: / c: /jboss-2.4.4/deploy/stringprocessor.jar
[Info, J2EEDEPLOYER] CREATE APPLICATION
StringProcessor.jar
[Info, J2EEDEPLOYER] Install EJB MODULE
StringProcessor.jar
[Info, containerfactory] Deploying: file: / c: / jboss-
2.4.4 / TMP / Deploy / Default / StringProcessor.jar
[Info, containerfactory] Deploying StringProcessor
[Info, stringprocessor] initializing
[Info, stringprocessor] initialized
[Info, stringprocessor] Starting
[Info, stringprocessor] started
[Info, ContainerFactory] Deployed Application:
FILE: / C: / JBOSS-
2.4.4 / TMP / Deploy / Default / StringProcessor.jar
[Info, J2EEDEPLOYER] J2EE Application:
File: / c: /jboss-2.4.4/deploy/stringprocessor.jar IS
Deployed.
Writing client applications To test if your EJB is run, let's create a Client class (see List 5). To compile the client application, enter the following command from the Workplace directory:
Javac-ClassPath C: /Jboss-2.4.4/client/Jboss
J2EE.jar ;. Client.java
The Client class requires a parameter, which is a string that will be converted to uppercase. Use the following command to run the client program from Workplace (assuming java.exe is already included in your PATH environment variable):
Java-ClassPath C: /Jboss-2.4.4/client/Jboss
Client.jar; c: /jboss-2.4.4/client/jnp-client.jar ;.
Client "Golden Retriever"
The output of the above command is:
Got Context
Got Reference
Uppercase of 'Golden Retriever' IS
Golden Retriever
You have learned how to develop your first EJB and deploy it with JBoss. If you use a different J2EE server, the process is similar. Your work area directory structure is the same, and you write the same source file and use a similar order to compile your EJB, compile and run your client application. Different are the location of the EJB API required in your classpath (JBoss-J2EE.jar in JBoss) and deployment procedures. With regard to these issues, you should refer to the server file instructions.