Tutorial for building j2ee applications using jboss and eclipse -1

xiaoxiao2021-03-06  78

Chapter 1

Configuration of Eclipse To Use Jboss and Lomboz

Install eclipse.

First Of All We Have To Set Up Eclipse Integrated Development Environment (IDE) with JBoss As Our Application Server.

So go to this page: http://www.eclipse.org/downloads/index.php and download the binary for the Eclipse editor You can download the appropriate binary for your platform This tutorial was developed using Linux (Red Hat 8.0.. ) as the operation system.

EXAMPLE: Eclipse-sdk-2.1-linux-gtk.zip, Eclipse-sdk-2.1-win32.zip

NOTE: Eclipse Does Not Include A Java Runtime Environment (JRE). You will NEED AT Least A 1.3 Level Java Runtime or Java Development Kit (JDK) Installed On Your Machine in Order To Run Eclipse.

Now unzip this file info your home directory.

[Vishal @ localhost eclipse] $ unzip eclipse-sdk-2.1-linux-gtk.zip

Archive: Eclipse-sdk-2.1-linux-gtk.zip

Archive: Eclipse-sdk-2.1-linux-gtk.zip

Inflating: eclipse / plugins / org.eclipse.core.boot_2.1.0 / boot.jar

Inflating: eclipse / plugins / org.eclipse.core.boot_2.1.0 / splash.bmp

Inflating: eclipse / plugins / org.eclipse.core.boot_2.1.0 / boot.xml

Inflating: Eclipse / Plugins / Org.eclipse.core.boot_2.1.0 / plugin.properties

-------------------------------------------------- ---------------------------

-------------------------------------------------- ----------------------------

-------------------------------------------------- -----------------------

INFLITING: Eclipse / Install.ini

INFLITIPSE / Startup.jar

Inflating: eclipse / readme / readme_eclipse.html

ONCE you have unzipped this file you will have a directory caled 'eclipse' in your home directory.

Go INTO That Directory and Run The Script File Called 'Eclipse'.

[Vishal @ localhost eclipse] $ CD Eclipse

[Vishal @ localhost Eclipse] $ LSCPL-V10.html Features Install.ini Plugins Startup.jar

Eclipse icon.xpm notice.html readme Workspace

[Vishal @ localhost eclipse] $ ./eclipse

This Will Run The Eclipse IDE ON YOUR WORKSTATION. Let's get hmiliar with this environment.

Install jboss.

Now Eclipse Is Installed and is Running On Your Workstation, Let's Install Jboss.

You can download JBOSS ready to run. Because JBOSS comes with a simple embedded Database (Hypersonic) and a web server (JBossWEB or Tomcat) you can use it out of the box without any initial configuration. Downloads can be accessed from either the JBOSS Project Page on SourceForge where you will always find up to date downloads (http://sourceforge.net/projects/jboss), or from the JBOSS Home Page where you will find useful information about JBoss, the official, free forums, project information etc ., And Also the download page http://www.jboss.org/downloads.jsp. For this Tutorial We are for use jboss-3.2.1 with the Built-in 'Jetty' JBossWeb Http Sever.

Example: JBoss-3.2.1.zip (Includes JBossWeb Http Server and JSP / Servlet Engine, EJB, CMP2.0, JCA, IIOP, Clustering, JTS, JMX.)

NOTE: JBoss Does Not Include A Java Runtime Environment (JRE).

First login as user 'root' and the unzip this file in a suitable place where you have enough space. I Have Unzipped it under an / opt / jboss / directory.

IT WILL CREATE A DIRECTORY NAMED JBOSS-3.2.1 Under / OPT / JBOSS /.

Before You Run Jboss, in Order to Check your installation of jboss, makess your have you have follow. Environment Variables Set.

JBoss_Home: /opt/jboss/jboss-3.2.1

Java_home: /usr/java/j2sdk1.4.1_02

Classpath: /usr/java/j2sdk1.4.1_02/lib/toLs.jar

Now Go Into Directory JBoss-3.2.1 and you will have all the directories. [Root @ localhost jboss-3.2.1] # ls

BIN Client Docs Lib Server

Go Into The Bin Directory and You Will Find A Script named run.sh.

Run this script from the command line. Hopefully it will start, and you will see message.

15: 12: 59,876 INFO [Server] JBoss (MX Microkernel) [3.2.1 (build: cvstag = jboss_3_2_1 date = 200305041533)] Started in 59s: 676ms.

NOTE: This Means That JBoss Installed and Running SuccessFully. Now, It's Time To Configure It from With Eclipse IDE.

In Order to Test Jboss Is Running Correctly, Go To your Web Browser and Access this url 'http: // localhost: 8080 / jmx-console / index.jsp'.

Note: This page is referred as the 'jmx management console'.

If You Can Access this Screen from your browser, jboss is running successfully.

Note That on this page under the sub-heading of 'jboss' there is a line' service = hypersonic '. Go to That link; That Will Bring Up an' mbean view 'page. On this page page there is an mbean operation called' startDatabaseManager '; go there and press the'. Invoke 'button This will bring up the console for the embedded Hypersonic database, from where you can access schemas and do other database management operations.

Creating the database schema.

................................

In Order to That, We Well Use The Following Script Files.

a) MyStoreSchema.script - to create the database schema.

b) MyStoreschema.Data - to Populate The Tables with data.

c) MyStoreschemadrop.Script - to Drop / Delete All Tables Used in this Tutorial.downloads:

MyStoreSchema.Script

MyStoreschema.Data

MyStoreschemAdrop.script

First Of All Copy There 3 FILES INTO The $ jboss_home / server / default / data / hypersonic / Directory. In this Example it is /opt/jboss/jboss-3.2.1/server/default/data/hypersonic/

Now Access The HSQL Database Manager Using The JMX-Management Console Shown Above.

Note: Please Drop / Delete Any Tables ALREADY Present in The Database with Same Name BeforeHese WHEN You Will Run These Scripts.

On The Top Level Menu of HSQL Database Manager File> Open script ..> Select MyStoreSchema.script.

After Clicking Open> Press Execute Sql Statement and The Following Screen Will Appear as Shown In The Figure Figure Below.

Go to View> Refresh Tree. If You Can See Five New Tables Created THEMA IS Ready, And It's Time to Populate Them with data.

Go to file> open script ..> select mystoreschema.data> open ..> Execute SQL Statement.

Now Your Data IS loaded in the database, in order...............

Make Sure You 'commit' (options-> commit) to save your schema in the database.

NOTE: SHUTDOWN JBOSS Server As We Will Run It Later from INSIDE Eclipse.

INSTALL LOMBOZ.

In order to run JBOSS from within Eclipse you need a plug-in, and for this tutorial we are using Lomboz which is available from http://sourceforge.net/projects/lomboz or from http://www.objectlearn.com/ INDEX.JSP.

Note: we are using lomboz.21_02.zip for this tutorial.

Now unzip this file in a temporary directory; you will have a directory named plugins Go to the directory plugins / com.objectlearn.jdt.j2ee / servers This directory contains configuration files for various servers For this tutorial we supply a configuration file... for jboss3.2.1 available under downloads.Note: If there are multiple configuration files the plugin will presently default to the first configuration file within the (eclipse / plugins / com.objectlearn.jdt.j2ee / servers /) directory in alphabetical order, so Rename All The Configuration Files To A '.bak'Extension. This is just to avoid slip-ups whilst we're learning.

Now, Copy The Supplied Jboss321all.server File Into The Servers Directory.

CD Plugins /

[Vishal @ localhost plugins] $ ls

com.ObjectLearn.jdt.j2ee

[Vishal @ localhost com.objectlearn.jdt.j2ee] $ CD Servers

[Vishal @ localhost Servers $ ls

JBoss244.server.bak jboss303tomcat4112.server.bak jboss300all.server.bak jboss321all.server

Tomcat403.server.bak weblogic70.server

JBoss300.Server.bak tomcat410.server x.log

Here's a snippet from the file 'jboss321all.server' Used for configuration. For this tutorial we will use this file, Which you can download Under Downloads.

Downloads: jboss321all.server

You can change the various settings in this file according to your environment (E.G. The port number).

Once you have saved this file under the com.objectlearn.jdt.j2ee / servers directory, move the com.objectlearn.jdt.j2ee directory under $ HOME / eclipse / plugins / directory, where $ HOME is your home directory.

[Vishal @ localhost temp / plugins] mv com.ObjectLearn.jdt.j2ee / home / Vishal / Eclipse / Plugins /.

Now, We Will Configure Lomboz Using this jboss321all.server file.

NOTE: Now You Can Now Delete The Temporary Directory.lomboz Configuration.

Now Run Your Eclipse IDE To Configure Lomboz ($ HOME / Eclipse / Eclipse).

Go to Window On Top Level Menu> Open Perspective> Java As Shown In Fig Below.

As we want to customize java persftive for this tutorial:

Go to WinDow Again At Top Level Menu> Customize Perspective> Other.

Select Lomboz Actions Sub Node.

Click Ok. Now you will have an icon under your top level menu like this.

Click ON this icon Will Have a Lomboz J2EE View Window in Your Workbench.

Now, Lomboz J2EE View is Available.

Go to WinDow> Customize Perspective.

Select File> New Sub Node. Select All Nodes Starting with Lomboz.

Select Window> show view. Select Console, Lomboz J2EE View and Others As Shown Below In Figure.

Now Click OK. You Will Have All Those Options Available from File> New> And Lomboz Menu Will Appear, or by Right Clicking In The Package Explorer A Pop Up Menu Will Appear.

Configure JBoss to Run from Withnin Eclipse.

First Of All We Have To Configure The Java Development Settings, Because Lomboz Requires Different Directories for Source and Binaries.

Go to Window> Preferences> Java> New Project.

ENTER SRC AND BIN for the names of these folders, Which is the defaults. Please do not modify these names as The Lomboz Tasks.

Also, Make Sure Jre Library is set to 'jre_lib' variable.

Go to WinDow on top level menu> preferences.

Go to the Lomboz Node Under The Preferences WINDOW. Make Sure JDK Tools.jar is set to $ java_home / lib / Tools.jar

Go to the server definitions sub node under Lomboz node. Select JBOSS 3.2.ALL under Server types and the rest of the options will be filled after loading the server configuration file which we made for this tutorial.Now, under Server Definitions, go to ' ClassPath 'and make Sure All The Paths Are Correct. If The PathS A Wrong Setting For A Path, It Will Show Red Crosses with Jar Icons In Server Libraries / Client Libraries Sub Section.

You can add or remove libraries from here. Make you 'Apply' The Changes After Altering The Various Options.

And now verify your 'classpath variables'.

Go to Window> Preferences> Java> ClassPath Variables

If you get all these classpath variables right accounting to your environment.

Test Your Configuration.

TO Test Your Configuration, Create a J2EE Project As A Test.

Go to File At Top Level Menu> New> Lomboz J2ee Project.

ENTER 'TEST' AS Project Name and Press Next.

Check your settings here; if you have 'Test / Src' on build path and 'test / bin' under default output folders the it's OK.

Now Click On Libraries Tab on The Project Cree Wizard.

BECAUSE OF A Bug in Eclipse 2.1 this library is configured Wrongly.

So Select this library and remove it.

NOW, Add A New Library> Add library ..> Select JRE System Library> Next.

Select Default Library and Press Finish.

New library is configured.

NOTE: You Have to Repeat this Step WHENEVER You Are Creating A New J2EE Project, as there is a bug in Eclipse 2.1.

NOW Press next.

Go to Web Module> Add ..> Enter module name 'OnlineBank'> OK.

Go to EJB Modules Tab> add ..> Enter module name 'mybank'> OK.

Go to Targeted Server> Add ..> 'JBoss 3.2.1 All' .note: 'JBoss 3.2.1 All' Is Not The File Name, But The Name Assigned To Server In The 'JBoss321all.server' File Used for Configuration Showed ABOVE. Snippet from File Below.

Name = "jboss 3.2.1 all"

EjBModules = "True"

Press Finish.

NOW you will have all the Directories and Libraries Under Test Project In The Package Explorer.

Under Project Test 'Src' Is The Directory Where All Packages for EJB Components and Servlets Will Be Developed, That is your java source files.

The 'MyBank' Directory Is Your EJB Module and Will Have 7 Files At Start Shown in Fig Below.

....................... ..

Note: if you are interested in ant (from the jakarta project) THEN NOTE Both modules have an ant 'build.xml' file which is buy to build the application.

Now Go To Top Level Menu Option Window> Show View> Lomboz J2EE View.

Lomboz J2EE View Will Have Test Project, Which Has Two Modules 'MyBank' And 'OnlineBank' Using Jboss 3.2.1 As Their Default Server.

Now it's time to create an ejb,

Go to File> New> Lomboz EJB CREATION WIZARD.

Note: You CAN Access this Wizard by Right CLICKING in Project Test in Package Explorer. Go to new> Lomboz EJB CREATION WIZARD AS Explained in Beginning.

Add package name 'au.com.tusc'; you can cho. Add 'MySession' in name. Select Stateless Session EJB Option.

This will create a file 'MySessionBean.java' under the 'au.com.tusc' package as shown in figure below.Note: It will generate the bean name, jndi-name and type of bean in the file Also the name of. the file is appended with word 'bean' as you gave the name of the bean as 'MySession' only. So be careful with naming conventions, as you only need to specify the bean name in the wizard. Do not append the word bean To the name as the wizard will do what for you.

Now We Will Add a Business Method via the wizard.

Go to File 'MySessionBean.java' under 'Package Explorer' and Expand That, Right Click ON 'MysessionBean' Node As Shown In The Figure Below, Select New> Lomboz EJB Method Wizard.

In The Method Signature Put 'Public String Learnj2ee (String Messg)', SELECT METHOD TYPE AS 'Business Method' and

Interface Type As 'Remote Interface'.

This Will Generate The Required Signature for your business method in your bean class.

Now Add this line in this method "me too!";

Code Snippet from Bean File

/ **

* @ ejb.interface-method

* tview-type = "remote"

*

** /

Public string learnj2ee (string messg) {

Return "ME TOO!";

}

Save the File. Now We Will Generate The Rest of the Files (Home and Remote Interfaces Along With helper classes, using xdoclet) Required to Deploy this bean.

First add this bean to the module.

Go to Package Explorer> Test> MysessionBean.java> MysessionBean, Right Click ON That; A Menu Will Pop Up. Go To Lomboz J2EE ON THIS POP-UP MENU, AND SELECT 'Add EJB TO MODULE'.

Go to Package Explorer> Test> MyBank (Which is a directory), Right Click on That; a menu will pop up. Go to Lomboz J2EE, Which is at the bottom of this pop up menu. SELECT Generate EJB CLASSES. THIS WILL Generate the required interfaces, helper classes and files related to deployment; that is, deployment descriptors.It will create a new directory named ejbsrc, which will have a package named au.com.tusc, and under that, remote and home interfaces, along with Necessary helper classes, Will be generated.

Note: You will not edit any files generated by Xdoclet under 'ejbsrc' directory at any time while doing this tutorial Because every time you use option Generate EJB Classes, it generates the necessary interfaces and helper classes as mentioned above These files are generated.. by Xdoclet, after parsing your bean class, which is created by Lomboz bean creation wizard. Xdoclet looks at the various declared tags and methods in the bean class and then generates files accordingly. As a result, you just add business methods and their implementations in The bean class, and the rest is left to xdoclet. Hence at Any Time In The Development, You Won't NEED TO (AND Shouldn't!) Edit Any Generated Files. It Will Become Clearer As We Progress in this Tutorial.

Also under MyBank> META-INF directory now there are 15 files, which includes ejb-jar.xml and jboss.xml and ejb-generate.xml. Initially there were only 6 files as noted earlier. These extra files are needed for deployment of .

Now Let Us Deploy this Bean without going into any further details of deployment.

Go to Lomboz J2EE View in your Workspace, Expand test> Expand mybank.

You will have au.com.tusc.mysessionBean Added to your MyBank EJB Module.right Click ON JBoss 3.2.1 All Icon As Shown In Fig Below.

A Menu Will Pop Up; Select Option Debug Server. It Will Start The Server with Debug Mode And After Successful Start It Will Show This Message In The Console Under Your Workspace As Shown In The Figure Below.

Go to Lombo J2ee View> Expand Test> SELECT MYBANK AND Right Click on That; A Menu Will Pop Up; SELECT OPTION deploy.

Once The Bean Is Deployed, a Message Will Come in Your 'Console' Confirming That, AS Shown In The Figure Below.

Now We Will Create a Client To Access This Bean Deployed On JBoss (Application Sever).

Go to Package Explorer> Test> SRC, Right Click on That, a menu Will Pop Up> SELECT EJB Test Client.

Enter package name 'Test' and client name as 'client'.

Select EJBHOME AS 'MySessionHome' and EJB Interface AS 'MySession'> Finish As Shown In The Figure Below.

Now The Client Is Generated, It's Time To Call Business Methods on The Deployed Bean.

We need some code to invoke the method on the bean ..

Go to Client.java and Write these Lines Under the 'TestBean ()' Method.

String Request = "I'm Tired of 'Hello, World' Examples ..";

System.out.println ("Request from Cliant:" Request);

System.out.println ("Message from Server:" MyBean.Learnj2ee (Request);

Code Snippet from Client.java.

Public void testbean () {

Try {

Au.com.tusc.mysession mybean = gethome (). CREATE ();

// --------------------------------------

// this is the place you make your caver

//System.out.println (MyBean.callyourthod ());

String Request = "I'm Tired of 'Hello, World' Examples .."; System.out.Println ("Request from Client: Request);

System.out.println ("Message from Server:" MyBean.Learnj2ee (Request);

} catch (remoteException E) {

E.PrintStackTrace ();

} catch (createException e) {

E.PrintStackTrace ();

} catch (namingexception e) {

E.PrintStackTrace ();

}

}

NOW FIRST SELECT Client Node Under Package Explorer. Go to Top Level Menu and SELECT this icon as shown below.

On this icon go to> Run as> Java Application.

Your Client Is Going to Make A Request, The Results of Which Will Be Displayed In The Console Under Your Workspace, as shown bell.

Note: Ignore these exceptions regarding org.apache.log4j.Property as it requires 'log4j.properties' on the clients path We will cover this later on as this does not affect the functioning of the bean..

You Have SuccessFully Created A Bean and Invoked An Operation ON It. NOW, Let's Have A Brief Overview of J2EE Concepts in The next chapter, before we start.

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

New Post(0)