RUNNING EJB IN JBOSS-3.2
R.S.RAMASWAMY
( published in DeveloperIQ ..
January-2004 )
-----------------------------------------------------------------------------------------------
JBOSS SERVER is open-source and free J2EE compliant
AppServer.
Last month, they have formally joined the JCP(Java
Community Process).
It is reported that it is a very popular & high quality
server.
Therefore, it will be useful to learn how to run and use
this server. The JB0SS3.2 software has been given in
NOVEMBER-2003 CD
in Miscellaneous section along with Ant and JDoclet software.
So, readers can start using it. ( Please instal it over Win 2000
ONLY).
Instal the binary version .We do not need the source files.
( It is not correct if the xml file names and
extension is capital letter.)
( Be careful to name the files as ejb-jar.xml &
jboss.xml only)
( not
EJB-JAR.XML & JBOSS.XML)
-----------------------------------------------------------------------
The following lesson illustrates the method
of creating
and executing a stateless-session
bean in JBoss3.2 server.
----
We have installed jboss3.2
in c:\drive (as jboss32).
So , cd to
c:\jboss32
Let us create a folder for our lessons as c:\jboss32\lsns.
cd c:\jboss32\lsns
create a folder 'statelessdemo' under lsns folder.
cd to
'c:\jboss32\lsns\statelessdemo' folder.
---------
This is our working directory.
..demo>set path=c:\windows\command;d:\jdk141\bin
( we are using jdk1.4.1 installed in D DRIVE.)
(jdk1.4.1 given in
DeveloperIQ..March 2003 CD).
Next we prepare a batch
file containing the jar files as
listed below.
---------
Before that , we create
a file as 'cpath-info.txt',for our own
reference as follows:
---------------------------------
// cpath-info.txt
================
set classpath=c:\jboss32\lsns\statelessdemo;
c:\jboss32\client\log4j.jar;
c:\jboss32\client\jboss-j2ee.jar;
c:\jboss32\client\jboss-common-client.jar;
c:\jboss32\client\jnp-client.jar;
c:\jboss32\client\jboss-system-client.jar;
c:\jboss32\client\jboss-client.jar;
c:\jboss32\client\jbosssx-client.jar;
c:\jboss32\client\jboss-transaction-client.jar
-------------------------------------------------------------
The corresponding
batchfile should be typed in a continuous line.
------------------------------------------------------------------
It is named as setcpath.bat
---------------------------
After creating
setcpath.bat in the working folder,
( working
folder='c:\jboss32\lsns\statelessdemo').
give the following
command:
...demo>setcpath
( this will set the classpath for the window as specified
earlier).
Checkup as follows:
....demo>echo
%classpath%
( you can verify by comparing with the data in cpath-info.txt)
This is most important because most of the troubles in
java programs are due to incorrect classpath!
--------------------------------------------------------------
Now we are ready to
create
a) greeterRemote.java
b) greeterHome.java
c) greeterBean.java
-------------------------
These files are
exactly the same as what we have already seen for weblogic5.1. There is absolutely no difference.(please
refer to November 2003 issue of
DeveloperIQ...)
..demo>javac
*.java ( compile the source files).
As before, create a subfolder named 'META-INF' (case-sensitive).
..demo>md META-INF
..demo> cd META-INF
---------------------------
Create ejb-jar.xml and
jboss.xml in META-INF folder.
-----
// ejb-jar.xml
( this file is the same
// whether we are using weblogic or jboss).
-------------------------------------------------------------
<?xml version="1.0" ?>
<!DOCTYPE ejb-jar PUBLIC
"-//Sun Microsystems, Inc.//DTD
"http://java.sun.com/dtd/ejb-jar_1_1.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>greetBean</ejb-name>
<home>greeterHome</home>
<remote>greeterRemote</remote>
<ejb-class>greeterBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
============================================================
// jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>greeterBean</ejb-name>
<jndi-name>greeterJndi</jndi-name>
</session>
</enterprise-beans>
</jboss>
=============================================================
Now, cd back to
c:\jboss32\lsns\statelessdemo folder.
create the jar file as
follows:
...demo>jar cf
greeter.jar *.class META-INF\*.xml
--------------------------------
Open another dos window for the server.
cd to c:\jboss32\bin
..bin>set
JAVA_HOME=D:\JDK141
( no need to set path).
..bin>run
----------------------------------
This will start the
server. You will get a lot of messages .Wait till the
server gets started fully .
-------------------
Now switch back to our working directory window
(
c:\jboss32\lsns\statelessdemo)
From there , copy greeter.jar to:
c:\jboss32\server\default\deploy
folder, as follows:
....demo>copy
greeter.jar
c:\jboss32\server\default\deploy
----------------------
That completes the
deployment process!
========================================================
Let us now create the
client file as follows:
(we are now in
c:\jboss32\lsns\statelessdemo folder).
Since the jndi data
will be different for jboss, note it carefully.
// greetClient.java
==================
import javax.ejb.*;
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;
public class greeterClient
{
public static void
main(String args[])
{
try
{
System.out.println("Please wait----!");
Properties props = new
Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL,"localhost");
props.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
Context ctx = new
InitialContext(props);
System.out.println("Context
ok");
greeterHome home =
(greeterHome)ctx.lookup("greeterJndi");
System.out.println("Home ok");
greeterRemote gr = home.create();
System.out.println("Remote ok");
System.out.println("Success...." + gr.greetme(args[0]));
}
catch(Exception e1)
{
System.out.println(" " + e1);
}
}
}
---------------------------------------------------------------
..demo>javac
greeterClient.java ( this compiles the client file).
Now , we are ready to
run the program.
>java
greeterClient "sam"
We get
"Success!...How are you ...sam?"
That completes this short but essential lesson on
running EJB in JBoss3.2 server.
-------------------------------------------------------------
Acknowledgements:
This
method of creating EJB programs without using wizards, is taken from ‘Teach yourself EJB in 21
Days’..Ghaly..(SAMS/PEARSON).
The same method is used in 'Java Server Programming
..Allama Raju of WebLogic' (WROX PRESS)
.