|
ejb-jar.xml :
One or more EJBs are packaged inside a JAR ( .jar ) file. There should be only one ejb-jar.xml file in an EJB
JAR file. So ejb-jar.xml contains deployment description for one or more than one EJBs. Now as we learned in an
earlier article, there are 3 types of EJBs so ejb-jar.xml
should be able to contain deployment description for all 3 types of EJBs.
Our ejb-jar.xml file for FirstEJB contains deployment description for the only EJB we have developed;
FirstEJB. Since it is a Session bean, it's deployment description is contained inside <session></session>
tags.
<ejb-jar>
<description></description>
<enterprise-beans>
<session></session>
</enterprise-beans>
</ejb-jar>
Now let's discuss different deployment descriptor tags inside the <session></session> tag.
First is the <ejb-name> tag. The value of this tag should be name of EJB i.e. any name you think should
point to your Session EJB. In our case it's value is "First". Then come <home>, <remote> and
<ejb-class> tags which contain complete path to Home, Remote and EJB implementation classes. Then comes
<session-type> tag whose value is either "Stateless" or "Stateful". In our case it is "Stateless" because our
Session bean is stateless. For more info on Stateless and Stateful Session beans, please read "An Introduction to Enterprise JavaBeans" article. Last tag is <transaction-type>,
whose value can be either "Container" or "Bean". We will learn more about transactions in another article, for now
it is sufficient to say that the transactions for our FirstEJB will be managed by the container.
<ejb-jar>
<description></description>
<enterprise-beans>
<session>
<display-name>FirstEJB</display-name>
<ejb-name>First</ejb-name>
<home>com.stardeveloper.ejb.session.FirstHome</home>
<remote>com.stardeveloper.ejb.session.First</remote>
<ejb-class>com.stardeveloper.ejb.session.FirstEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Then there is a <container-transaction> tag which tells that all methods of FirstEJB "support"
transactions. We will learn more about these tags and ejb-jar.xml as we continue to learn more about transactions
and security in EJB environment in other articles. For now let's move forward.
Compiling the EJB Java source files :
Our directory and file structure till now looks something like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
src
com
stardeveloper
ejb
session
First.java
FirstHome.java
FirstEJB.java
META-INF
ejb-jar.xml
You can compile all the Java source file by using a command like following on the command prompt :
C:\Projects\EJB\FirstEJB\src\com\stardeveloper\ejb\session>
javac -verbose -classpath %CLASSPATH%;C:\JBoss\client\jboss-j2ee.jar
-d C:\Projects\EJB\FirstEJB *.java
Note: The point to remember is to make sure jboss-j2ee.jar ( which contains
J2EE package classes ) in the CLASSPATH, or you will get errors when trying to compile these classes.
If you have installed JBoss Server in a separate directory then substitute the path to jboss-j2ee.jar with the
one present on your system. The point is to put jboss-j2ee.jar in the CLASSPATH for the javac, so that all
EJB source files compile successfully.
On the next page we will learn how to package these .class files and .xml file into an easy to deploy
JAR file.
Packaging EJB source files into a JAR file :
Till now our directory and file structure should look something like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
com
stardeveloper
ejb
session
First.class
FirstHome.class
FirstEJB.class
META-INF
ejb-jar.xml
src
com
stardeveloper
ejb
session
First.java
FirstHome.java
FirstEJB.java
Now to package the class files and XML descriptor file together, run the following command at the
command prompt :
C:\Projects\EJB\FirstEJB>jar cvfM FirstEJB.jar com META-INF
Running this command should produce an EJB JAR file with the name of FirstEJB.jar in the FirstEJB folder.
But there is one thing still left to be done.
Adding JBoss specific configuration file :
Till now our FirstEJB.jar file contains generic EJB files and deployment description. To run it on JBoss we will
have to add one other file into the META-INF folder of this JAR file, called jboss.xml. This file contains the
JNDI mapping of FirstEJB.
So create a new jboss.xml file in the FirstEJB/META-INF folder where ejb-jar.xml file is present. Copy and paste
the following text in it :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN"
"http://www.jboss.org/j2ee/dtd/jboss.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>First</ejb-name>
<jndi-name>ejb/First</jndi-name>
</session>
</enterprise-beans>
</jboss>
To add this new jboss.xml file into our existing FirstEJB.jar file, run the following command at the
DOS prompt :
C:\Projects\EJB\FirstEJB>jar uvfM FirstEJB.jar META-INF
Now our FirstEJB.jar is ready to be deployed to the JBoss Server.
iv. Deploying FirstEJB.jar on JBoss Server :
Deploying EJBs on JBoss is as easy as copying the FirstEJB.jar file and pasting it into the C:\JBoss\deploy
folder. If JBoss is running, you should see text messages appearing on the console that this EJB is being deployed
and finally deployed and started. As simple as that.
v. Creating the client JSP page :
Create new WEB-INF folder in C:\Projects\TomcatJBoss folder. Now create a new web.xml file and copy/paste following
text in it :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
</web-app>
As you can see, this web.xml is almost empty and is not doing anything useful. We still created it
because Tomcat will throw an error if you try to access this /jboss context that we had created earlier without
creating a /WEB-INF/web.xml file. ejb-jar.xml :
One or more EJBs are packaged inside a JAR ( .jar ) file. There should be only one ejb-jar.xml file in an EJB
JAR file. So ejb-jar.xml contains deployment description for one or more than one EJBs. Now as we learned in an
earlier article, there are 3 types of EJBs so ejb-jar.xml
should be able to contain deployment description for all 3 types of EJBs.
Our ejb-jar.xml file for FirstEJB contains deployment description for the only EJB we have developed;
FirstEJB. Since it is a Session bean, it's deployment description is contained inside <session></session>
tags.
<ejb-jar>
<description></description>
<enterprise-beans>
<session></session>
</enterprise-beans>
</ejb-jar>
Now let's discuss different deployment descriptor tags inside the <session></session> tag.
First is the <ejb-name> tag. The value of this tag should be name of EJB i.e. any name you think should
point to your Session EJB. In our case it's value is "First". Then come <home>, <remote> and
<ejb-class> tags which contain complete path to Home, Remote and EJB implementation classes. Then comes
<session-type> tag whose value is either "Stateless" or "Stateful". In our case it is "Stateless" because our
Session bean is stateless. For more info on Stateless and Stateful Session beans, please read "An Introduction to Enterprise JavaBeans" article. Last tag is <transaction-type>,
whose value can be either "Container" or "Bean". We will learn more about transactions in another article, for now
it is sufficient to say that the transactions for our FirstEJB will be managed by the container.
<ejb-jar>
<description></description>
<enterprise-beans>
<session>
<display-name>FirstEJB</display-name>
<ejb-name>First</ejb-name>
<home>com.stardeveloper.ejb.session.FirstHome</home>
<remote>com.stardeveloper.ejb.session.First</remote>
<ejb-class>com.stardeveloper.ejb.session.FirstEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Then there is a <container-transaction> tag which tells that all methods of FirstEJB "support"
transactions. We will learn more about these tags and ejb-jar.xml as we continue to learn more about transactions
and security in EJB environment in other articles. For now let's move forward.
Compiling the EJB Java source files :
Our directory and file structure till now looks something like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
src
com
stardeveloper
ejb
session
First.java
FirstHome.java
FirstEJB.java
META-INF
ejb-jar.xml
You can compile all the Java source file by using a command like following on the command prompt :
C:\Projects\EJB\FirstEJB\src\com\stardeveloper\ejb\session>
javac -verbose -classpath %CLASSPATH%;C:\JBoss\client\jboss-j2ee.jar
-d C:\Projects\EJB\FirstEJB *.java
Note: The point to remember is to make sure jboss-j2ee.jar ( which contains
J2EE package classes ) in the CLASSPATH, or you will get errors when trying to compile these classes.
If you have installed JBoss Server in a separate directory then substitute the path to jboss-j2ee.jar with the
one present on your system. The point is to put jboss-j2ee.jar in the CLASSPATH for the javac, so that all
EJB source files compile successfully.
On the next page we will learn how to package these .class files and .xml file into an easy to deploy
JAR file.
Packaging EJB source files into a JAR file :
Till now our directory and file structure should look something like following :
C:\Projects
TomcatJBoss
EJB
FirstEJB
com
stardeveloper
ejb
session
First.class
FirstHome.class
FirstEJB.class
META-INF
ejb-jar.xml
src
com
stardeveloper
ejb
session
First.java
FirstHome.java
FirstEJB.java
Now to package the class files and XML descriptor file together, run the following command at the
command prompt :
C:\Projects\EJB\FirstEJB>jar cvfM FirstEJB.jar com META-INF
Running this command should produce an EJB JAR file with the name of FirstEJB.jar in the FirstEJB folder.
But there is one thing still left to be done.
Adding JBoss specific configuration file :
Till now our FirstEJB.jar file contains generic EJB files and deployment description. To run it on JBoss we will
have to add one other file into the META-INF folder of this JAR file, called jboss.xml. This file contains the
JNDI mapping of FirstEJB.
So create a new jboss.xml file in the FirstEJB/META-INF folder where ejb-jar.xml file is present. Copy and paste
the following text in it :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN"
"http://www.jboss.org/j2ee/dtd/jboss.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>First</ejb-name>
<jndi-name>ejb/First</jndi-name>
</session>
</enterprise-beans>
</jboss>
To add this new jboss.xml file into our existing FirstEJB.jar file, run the following command at the
DOS prompt :
C:\Projects\EJB\FirstEJB>jar uvfM FirstEJB.jar META-INF
Now our FirstEJB.jar is ready to be deployed to the JBoss Server.
iv. Deploying FirstEJB.jar on JBoss Server :
Deploying EJBs on JBoss is as easy as copying the FirstEJB.jar file and pasting it into the C:\JBoss\deploy
folder. If JBoss is running, you should see text messages appearing on the console that this EJB is being deployed
and finally deployed and started. As simple as that.
v. Creating the client JSP page :
Create new WEB-INF folder in C:\Projects\TomcatJBoss folder. Now create a new web.xml file and copy/paste following
text in it :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
</web-app>
As you can see, this web.xml is almost empty and is not doing anything useful. We still created it
because Tomcat will throw an error if you try to access this /jboss context that we had created earlier without
creating a /WEB-INF/web.xml file.
|