JBoss is an open source free EJB server that implements most of the features that other Java EE provides, Now that the Sun company has put JBoss as a j2ee1.4 standard to implement the server, this article will lead you to start from the installation of Jboss3.2.6, until the development of a complete "Hello,world" EJB.
setup and start-up of JBoss
If you are using JBoss for the first time, you must be frustrated because it is open source and can be downloaded free of charge, but its documentation or technical training is charged, and for the Chinese people is not high (a few days of training about 10000 U.S. dollars, documents are dozens of to hundreds of U.S.). If you try to find some articles on the web that are easy to get started with JBoss, there are only a few, and few rookie-level articles. On the contrary, the most highly theoretical articles, such as its core design, make you look more and more confused. So the purpose of this article is to get your jboss to run as fast as possible and to develop simple ejbs on top of it.
Before you install JBoss, make sure that you have installed jdk1.3 or above, and that JBoss does not need JDK support to run because it is not bundled with JDK like WebLogic and other application servers. Then download an JBoss release (which I downloaded from jboss-3.2.6.zip) on the http://www.jboss.org Web site, the stable distribution I used in this article JBOSS3.2.6 (Integrated tomcat4.1), it needs to be added that Tomcat is the Apache Foundation's well-known open source Jsp/servlet server, if you want to learn more about Tomcat, please visit http://jakarta.apache.org For more detailed information.
When you download the Jboss-3.2.6.zip, the next step is to unzip it, if you are on Windows, you can use WinZip or WinRAR, if you are on Linux, use the unzip command, take me as an example, and suppose I unzip it to the following directory
c:/jboss-3.2.6
Compared to Weblogic,websphere and other Java-EE servers, JBoss startup is simply unexpected, if you are a Windows user, just enter the c:/jboss-3.2.6/bin below, Input Run.bat command, JBoss ran up, if it is a Linux user, only need to enter the c:/jboss-3.2.6/bin below, input run.sh, then JBoss also run. What do you think. It's quite simple.
When you enter Run.bat or run.sh, you will find that the screen will continue to scroll some of the prompts, after about 1 minutes (depending on your machine configuration, mine is P4 1.7g,128m), the message will stop scrolling, as shown in the following figure:
(Note: If you are under Windows, make sure that the DOS window stays in this state, and you must not abort it.) )
In this way, JBoss is already in the running state. Like other Java servers, JBoss also provides a web-mode console by typing http://127.0.0.1:8080/web-console/in IE, so you'll see a console as shown in the following illustration:
What do you think. This interface is no worse than the WebLogic. Through this console, you can dynamically manage and monitor JBoss's services.
writing the first EJB: "Hello,world"
Now we're officially starting EJB programming. Before writing our first EJB, you should have a general understanding of EJB, if not, suggest you go to the Internet to find some articles on this, otherwise you will not understand the following to be covered.
Remote interface
A remote interface is a call interface that can be seen for the client
Helloworld.java
Package sample;
/* This is a remote interface where the client invokes this interface to make the real EJB work.
Public interface HelloWorld extends Javax.ejb.EJBObject
{
Public String Hello () throws java.rmi.RemoteException;
}
Home interface
We can think of the home interface as a factory that makes EJBs, and the Home interface tells the EJB container: "Hi, my client wants me to build an EJB, and now I'm giving you this task." ”
Helloworldhome.java
Package sample;
The/*home interface tells the EJB container how to generate or destroy an instance of an EJB.
Public interface Helloworldhome extends Javax.ejb.EJBHome
{
HelloWorld Create () throws java.rmi.remoteexception,javax.ejb.createexception;
}
Implementation of EJB
This is the real EJB implementation.
Helloworldbean.java
Package sample;
Import Javax.ejb.SessionContext;
/* This class is specifically implemented by the remote interface helloworld*/
Pubic class Helloworldbean implements Javax.ejb.SessionBean
{
Private Sessioncontext CTX;
public void Setsessioncontext (Sessioncontext ctx)
{
This.ctx = CTX;
}
pubic void Ejbremove ()
{
System.out.println ("Ejbremove ()");
}
public void Ejbactivate ()
{
System.out.println ("Ejbactivate ()");
}
public void Ejbpassivate ()
{
System.out.println ("Ejbpassivate ()");
}
The/*hello method is the actual business logic that can display the "Hello,world" string on the client/
Public String Hello ()
{
System.out.println ("Hello ()");
return "Hello,world";
}
}
Well, the entire code for this session EJB is written, and the next step is to write its deployment file:
Ejb-jar.xml
JBoss Hello World Application
Hello World EJB
Hello
Sample. Hellohome
Sample. Hello
Sample. Hellobean
Stateless
Bean
This ejb-jar.xml file should be placed in the Meta-inf directory of the current project, and in this case I'll put it in the
F:/projects/jbss-tutorial directory, of course you should put it in your own project directory as needed, in order to better let you know the location of the Ejb-jar.xml file, the following I list the entire directory structure of the example:
This completes a simple session EJB authoring, but in fact JBoss also provides an additional profile: Jboss.xml, which allows more customization of the JBoss server, but because this example is too simple, we can omit it from writing.
While we've finished writing this session EJB, there's one final step to make: packaging. First we enter the root directory of the current project:
CD f:/project/jboss-tutorial
Then execute the JAR command to package all the classes and Ejb-jar.xml:
Jar CF Helloworld.jar Sample Meta-inf
At this point you will find that in the current directory more than a file named Helloworld.jar, this is our final product.
deployment of our EJB
Deploying EJBS is a very easy task in JBoss, and you can simply copy the Helloworld.jar to the c:/Jboss-3.2.6/server/default/deploy directory.
At this point you can switch to the DOS window where JBoss runs, and you'll notice that the following message appears on the screen:
15:09:21,184 INFO [Maindeployer] Starting deployment of
Package:file:/f:/jboss
-3.2.3/server/default/deploy/helloworld.jar
15:09:21,324 INFO [Ejbmodule] Creating
15:09:21,354 INFO [Ejbmodule] Deploying HelloWorld
15:09:21,464 INFO [Ejbmodule] Created
15:09:21,484 INFO [Ejbmodule] Starting
15:09:21,555 INFO [Ejbmodule] Started
15:09:21,555 INFO [Maindeployer] successfully completed
Deployment of Package:file:/f:/jboss-3.2.6/server/default/deploy/helloworld.jar
Client code
EJB is almost useless to us without the client code. Here we will write the client code to invoke this HelloWorld.
If you run client code and JBoss server on the same machine, the following code can be run without any modification, but if your client is running on another machine, you have to change the corresponding line in the source code:
/* Below is the client source code needs to modify the line * *
Env.put (Context.provider_url, "localhost:1099");
Assuming that the EJB is deployed on a machine with an IP address of 192.168.0.1, the above source should be changed to read as follows:
/* The following is a modified line in the client source code * *
Env.put (Context.provider_url, "192.168.0.1:1099");
/*helloworldclient.java*/
Package sample;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import java.util.Hashtable;
public class Helloworldclient
{
public static void Main (String [] args)
{
Hashtable env = new Hashta BLE ();
Env.put (context.initial_context_factory, "org.jnp.interfaces.NamingContextFactory");
Env.put (Context.provider_url, "localhost:1099");
Env.put ("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
Try
{
Context ctx = new InitialContext (env);
Object obj = ctx.lookup ("HelloWorld");
Helloworldhome home = (helloworldhome) javax.rmi.PortableRemoteObject.narrow (
obj, helloworldhome.class);
HelloWorld HelloWorld = Home.create ();
System.out.println (Helloworld.hello ());
Helloworld.remove ();
}
catch (Exception e)
{
E.printstacktrace ();
System.out.println ("Exception:" + e.getmessage ());
}
}
}
OK, so I can compile and run the client, and if you can't find some classes in the JVM report at compile time, you may not have j2ee.jar this package in the CLASSPATH path variable. The result of the client's execution, though, is simply to print a line of "Hello,world" on the screen, but it comes from another world.
--jboss's voice.