JBOSS - the first EJB

xiaoxiao2021-03-06  62

JBoss is an open source free EJB server that implements most of the functions specified by other J2EE. Now Sun has implemented JBOSS as J2EE 1.4 standards. This article leads you from JBoss3.2.6 installation. Also until a complete "Hello, World" EJB is developed.

JBoss installation and start

If you are using JBoss for the first time, you will be very frustrated because it is an open source and can be downloaded for free, but its documentation or technical training is charged, and it is high for the Chinese. The day of training is about 10,000 US dollars, and the documentation must do tens to hundreds of more beautiful)! If you try to find some articles about JBoss's simple entry on JBoss, you can just have a few of the few articles, and there are very few rookie-level articles. On the contrary, it is a lot of artistic articles such as the core design, which is, so that you will make you see more. Therefore, the purpose of this article is to let your JBoss run as soon as possible, and immediately develop simple EJBs on it.

Before installing JBOSS, you must first determine that you have installed JDK1.3 or more, because JBoss is not bundled with other application servers like WebLogic, so JBoss is not to support JDK support to run. Then download a JBoss release on http://www.jboss.org website (I downloaded JBoss-3.2.6.zip), which in this article is used in this article, its stable release jboss3.2.6 (integrated Tomcat4.1) The Tomcat is Tomcat is the famous open source JSP / Servlet server of the Apache Foundation. If you want more to learn Tomcat, visit http://jakarta.apache.org to get more detailed information.

When you download JBoss-3.2.2.2.2.2.2.2.6.zip, the next step is to decompress it. If you are on Windows, you can use Winzip or WinRAR; if it is under Linux, use the unzip command to take myself as an example , Suppose I decompressed it to the following directory

C: / jboss-3.2.6

Compared to WebLogic, WebSphere, etc. J2EE servers, JBoss is simple to make unexpectedly, if you are a Windows user, just enter C: / JBOSS-3.2.6 / bin, enter the run.bat command, JBoss Running; if it is Linux user, just enter C: / JBOSS-3.2.6 / bin, enter run.sh, then JBoss is also running. how about it? Is it very simple?

When you enter run.bat or run.sh, you will find that the screen will continue to scroll some prompt information, after about 1 minute (depending on your machine configuration, I am P4 1.7G, 128M), prompt information It will stop scrolling, as shown below:

(Note: If you are under Windows, then let this DOS window keep this state, don't stop it!)

In this way, JBoss is already in operation. Like other J2EE servers, JBoss also provides a web way console, using the method to enter http://127.0.0.1:8080/web-console in the IE browser, so you will see the following figure One console:

how about it? This interface is not bad better than WebLogic? With this console, you can dynamically manage and monitor JBoss services. Write the first EJB: "Hello, World"

Let's officially start EJB programming. Before writing our first EJB, you should have a roughly understanding of EJB. If there is no, it is recommended that you first go to the Internet to find some articles, otherwise you will not understand the content you want to tell.

Remote interface

Remote interface refers to the call interface that can be seen for the client.

/// * This is a remote interface. The client calls this interface to make real EJB work * / public interface helloworld extends javax.ejb.ejbobject {public string hello () throws java.rmi.RemoteException; }

HOME interface

We can see the home interface as a factory that manufactures EJB. The HOME interface tells the EJB container: "Hey, my customers want me to generate an ejb, now I will give you this task!"

//HelloWorldHome.javapackage sample; / * Home Interface EJB container tells how to generate or destroy instance of the EJB * / public interface HelloWorldHome extends javax.ejb.EJBHome {HelloWorld create () throws java.rmi.RemoteException, javax.ejb.CreateException; }

EJB implementation

Here is true EJB implementation

//HelloWorldBean.javapackage sample; import javax.ejb.SessionContext; remote interface HelloWorld * / pubic class HelloWorldBean implements javax.ejb.SessionBean {private SessionContext ctx / * class specific implementation; 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 ()");} / * Hello method is the actual business logic, which can display "Hello, World" string * / public string hello () {system.out .println ("Hello ()"); Return "Hello, World";}}

Ok, this session EJB is written, the next step we have to do is preparing 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 put In the meta-inflicity of the current project, I put it in the F: / Projects / JBSS-Tutorial directory in this example, and of course you should put it in your own project directory as needed, in order to Clearly let you know the location of the EJB-JAR.XML file, here I list the entire directory structure of this example:

In this way, we have completed a simple session EJB writing, but in fact, JBoss also provides an additional profile: jboss.xml, using it to make more customization of JBoss servers, but because this example is too simple. So we can omit it.

Although we have completed the writing of this session EJB, there is still a final step to do: package. First we entered the root directory of the current project:

CD f: / project / jboss-tutorial

Then execute the JAR command to package all classes and ejb-jar.xml:

JAR CF HelloWorld.jar Sample Meta-INF

At this time, you will find that there is a file called helloWorld.jar in the current directory, which is our final finished product.

Deploy our EJB

Deploying EJB is a very easy task in JBoss, you can copy helloWorld.jar to the C: / jboss-3.2.6 / server / default / deploy directory.

At this time, you can switch to the DOS window running in JBoss, you will find the following prompt information on the screen:

15: 09: 21,184 info [maindeployer] Starting Deployment USA: file: / f: /jboss-3.2.3/server/default/deploy/helloworld.jar15: 09: 21,324 info [ejbmodule] Creating15: 09: 21,354 Info [ejbModule] Deploying HelloWorld15: 09: 21,464 INFO [ejbModule] Created15: 09: 21,484 INFO [ejbModule] Starting15: 09: 21,555 INFO [ejbModule] Started15: 09: 21,555 INFO [MainDeployer] Successfully completeddeployment of package: file: / F: /Jboss-3.2.6/server/default/deploy/helloworld.jar client code

If there is no client code, then EJB is almost unused. We will write client code to call this helloworld.

If you run the client code and the JBoss server on the same machine, then you can run without any modifications, but your client runs on another machine, then you have to change the corresponding line in the source code. a bit:

/ * The following is a line * / env.put (Context.Provider_url, "Localhost: 1099") in the client source code.

Suppose the EJB is deployed on a machine that is 192.168.0.1 in an IP address, then the above source code should be changed as follows:

/ * The following is the modified row after 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 Hashtable (); env.put (Context.initial_Context_Factory, "Org.jnp.interfaces.namingContextFactory"); env.put (Context.Provider_URL, "Localhost: 1099"); env.put ("java.naming.factory.URL .pkg.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, let me compile and run this client. If you are compiled, JVM report If you don't get some classes, you may be that you didn't put J2EE.jar in the path variable of ClassPath. Although the execution result of the client is just a simple "Hello, World" on the screen, it is from another world - Jboss sound!

转载请注明原文地址:https://www.9cbs.com/read-88664.html

New Post(0)