×

Loading...

A note on EJB and WebLogic

本文发表在 rolia.net 枫下论坛I am posting one of my teching notes on EJB and Weblogic. Because I don't have time to
edit it, there may be some misatkes therein.
But I hope this note can help Java novices know how complicated the server-side developement can be. Don't blame on me
if you don't understand this note. --Jabber
--------------------------

Java is both a lanaguge and platform. Writing code is relatively easy.
Configuring the system is more challenging. If we cannot make EJB code
work in WebLogic, I don't think we know anything about EJB.



The Tom Valesky book contains a lot of useful example code. However,
1/3 of its contents are out-of-date.

EJB is a complicated system. It is related to a lot of other things,
protocols, databases, .... The best way to learn EJB is to follow the
examples in Weblogic. Without a good bachground in general programming
and in Java, it is very difficult to understand EJB, unless you are really
persistent in learning.


To download Weblogic, go to wwww.beasys.com. It will take you more than 2 hours.

After installation, you need to invastiagte WebLogic's structure. Let me supppose
we installed weblogic at C:\weblogic

1) Under C:\weblogic, you will find
---weblogic.properties, this file configures the whole server, as you start
Weblogic, the system will read this file.
---weblogic.policy, this file controls the sucurity of your Weblogic server.
If you claim you know Java 2, you should have an understanding of policy file. If
you don't know, try to open it and guess the meaning of its contents.
---setEnv.cmd, this is a Windows Scripting file and is used for setting up
environment variables. You need open it and set JAVA_HOME to C:\jdk1.2.2. Please
don't use JDK1.2.1 because it does not support XML files well. I suggest not
using JDK1.3 because WebLogic is not tested with JDK1.3
---startWeblogic.cmd, this file is used to start up Weblogic server
---startConsole.cmd, this file is used to start up Weblogic Console. Weblogic
Console is graphic tool. It is like MCC. It is very beautiful. It is wriiten in Java Swing. After
you server is up, you can use Weblogic Console to monitor your server.
---startSNMPAgent.cmd, this file is used to start up a SNMP agent. you may not
be interested in it.
--- some .sh files. These files are Unix versions of the above .cmd files.
Since we work at Windows platform, you may delete all the .sh files because they
are uselesss.


****for Windows 95/98 users
Weblogic is not designed for Win 95/98. If you have to run it on Windows 95/98,
you have to take some actions. First, you need to expand your machine's virtual
memory by entering command.cmd/e:4096/p. (If you don't expand virtual memory,
your machine will complain "out of environmental space" ). Second, you need to
rename all .cmd file to .bat file because Windows 98/95 does not recognize
.cmd extension.

2). Under C:\Weblogic\lib, you will find some .jar files. Among them, the most
important one is weblogicaux.jar. This .jar file contains the library for the
so-called J2EE technology, including servlets, jsp, ejb, jms, jts, xml, ....
Please check your setEnv.cmd to see if the CLASSPATH contains %WL_HOME%\lib\weblogicaux.jar.
(WL_HOME is windows constant defined in setEnv.cmd, and you can find its definition).
If your CLASSPATH does not contain this term, your EJB code won't compile.

3). Under C:\Weblogic\classes, you will find a lot of utility Java classes shipped
by BEA System (The WebLogic vendor). Here are some examples:
---weblogic.ejbc (the ejb compiler)
---weblogic.rmic (the rmi compiler)
---weblogic.jspc (the jsp compiler)
---weblogic.Admin (for server administration)
---weblogic.deploy (the ejb deploy tool)
Just name a few. For historical reasons, some of these classes were named completely
using lowercase letters. In addition, these java classes are not compressed using
jar utility. TO set up a good worinikng environment, you need make sure that
your CLASSPATH contains %WL_HOME%\classes. Open setEnv.cmd file and check it.

4) Under C:\weblogic\eval, a database called cloudscape is installed for running
all Weblogic examples. You may browse it but don't modify it.

5) Under C:\weblogic\examples, you will find many examples, inclduing ejb, jsp,
servelts, jms, ....
Our interests are in the ejb examples. There are about 10 ejb examples. If you
can make them work, you already can work on EJB. If you go ferther to
C:\weblogic\examples\ejb\basic, you will find 4 packages, each of which represent
a type of beans.
If you really are determined to learn EJB, get into these packages and
open the corresponding index.html file using a browser. Follow the instruction
in the index.html file to see if you can deploy the bean. In each folder, you
can find Java source files and xml configuration files.

6) There are some other directories under C:\weblogic, but I have no interests to
talk about them here.


Now we can start up our server. You have two ways to do it.

1)open a MS-DOS. go to C:\weblogic. Execute setEnv.cmd. Then execute startWeblogic.cmd.
This MS-DOS will be stuck there. It means your server is still running. Once you
see the words "Weblogic Server Started", you can open your browser and hit
http://localhost:7001/index.html. In this page, you will see many links. If your
computer is connected to internet, you will be able to grab all the documentation.
For instance, if you hit "Weblogic EJB", you will be taught all things about EJB
in WebLogic, including, compilation, deployment, .....

2) If you like Windows facilities too much, you can startup Weblogic in another way.
From the "Start" button, go to |Start|Programms|Weblogic 5.10|Weblogic Server
This is easy but I don't suggest doing so.

Weblogic has a very good documentation. It has almost 5000 pages totally. If you
are determined, you can all things by checking the documents. We are IT professionals.
Don't trust books. Don't rely on teachers.


A true Java developer must have a good insight of PATH and CLASSPATH. Otherwise,
he/she cannot do anything useful in a development envorenment. Whenever you try
to compile some Java source files, you should ask the following questions:

1) Does my CLASSPATH include all the necessary library classes?
If you fail in compiling and sees some error message like
"Exception in thread "main" java.lang.NoClassDefFoundError: com/uti/....",
it means the compiler does not find a class in the package
com.uti..... The library might be there, but the compiler cannot reach
the library according to your CLASSPATH. You need check your CLASSPATH and
reset it. To do this, you had better modify setEnv.cmd file and run it again.


2) Where should I put my compiled Java classes?
Many java novices just enter some commands like "javac Hello.java". This is
far away from professional Java programming. For Ejb development, we have to
separate the compiled class file from the source files. For this purpose,
you must specify the destination of your compiled Java class file as follows:
javac -d C:\workdir Hello.java
If I have a folder and it contains all the source file for an EJBean, I can
compele all the soure files as follows:
javac -d C:\workdir *.java

A comment is in order. -d means destination. C:\workdir is just an example and
you can change it to some other directory you like. But do not use C:\ as the
destination. Why? ---The reason is your compiled java classes will mix with
the System files. Say again, don't use C:\ as the destination.

Another comment. If your java source files have a package structure
" package com.xyx.abc.ecommerce;", the machine will create a directory
tree com\xyz\abc\ecommerce under C:\workdir, and your compiled Java class files will
reside at C:\workdir\com\xyz\abc\ecommerce. This is a basic fact but not
all entry-level Java programmers are aware of it!

Now we suppose we have succeeded in compiling a EJB component called hello.
More Specifically, let me assume we have compiled the Hello example from
Tom Valesky book. Then, the complied classes reside at
C:\workdir\book\chap03\hello\.

It is the right time for use to prepare xml files to describe our hello.
For this purpose, we need create another directory C:\workdir\META-INF.
Please note that C:\workdir\META-INF and C:\workdir\book are at the
same level in the file hirarchy. All the xml files will reside at
C:\workdir\META-INF\.


The easiest way to prepare xml files is copying the example xml files from
the Weblogic EJB example and doing some modifications. For instance,
one can go to C:\weblogic\examples\ejb\basic\StatelessSession and
copy all the xml files to C:\workdir\META-INF. There are two xml files
for a bean that does not communicate with the database: one is ejb-jar.xml,
another is weblogic-ejb-jar.xml. The ejb-jar.xml describes how this
bean is composed, while weblogic-ejb-jar describes how this bean will
bound with JNDI and how this bean is pooled and cached. In the two xml
files, there is an XML tag called <ejb-name> and it should be
consistent in two xml files. My impression is that some novices cannot
understand how to modify <home> <remote> <bean-class> tags. Actually,
you nust need put the FULL class names of home, remote interfaces and
bean implementaion class.


There is another minor thing. If one deploy the bean in the graphic tool
in the furture, he/she may use some icons to represent the bean. This
is configured in the ejb-jar.xml file. We need provide these icon files.
The simplest way is to copy C:\weblogic\...\statelessSession\build\images
to C:\workdir\images. Now under C:\workdir, we have three dubdirectories:
book\, images\, META-INF\.


Provide you have got ready two Xml files, it is time that you compress
all the compiled class files and xml files into a jar file. Here is
the command:
jar cv0f hello.jar META-INF book images
Some comments are in order. "c" means creating hello.jar by
packaging three folder: META-INF\, book\, images\.

Now it is the time to run Weblogic's ejb compiler. Our hello.jar just specifies
some public interfaces. To make it root in the Weblogic ejb container, we need
run ejb compiler on the hello.jar. This compiler will generate a set of
classes and these classes will help the hello bean fit into the container.
Otherwise, our hello bean is nothing but some public interfaces. This can be
envisioned from the very beginning: Hello and HelloHome are two interfaces.
Who implement these interfaces?

Here is the command to run the Weblogic's ejb container:

java -cp %CLASSPATH% -Dweblogic.home=%WL_HOME% weblogic.ejbc -compiler javac hello.jar hello_for_deploy.jar

Before you run this ejb compiler, you must run setEnv.cmd in the MS-DOS to
define two windows environment variables: CLASSPATH and WL_HOME.
The above command says Weblogic EJB compiler (weblogic.ejb) is a java application,
so we need run java. This compiler processes hello.jar and create a new jar file
called hello_for_deploy.jar. There are 3 options here. "-cp %CLASSPATH%"
means specifying the CLASSPATH as defined in setEnv.cmd; "-Dweblogic.home=%WL_HOME%"
means defining a System property called weblogic.home as %WL_HOME% as defined
in setEnv.cmd; "-compiler javac" means we use Sun's java compiler.

There is an insiduous bug here. If we do not specify the CLASSPATH,
JVM will not be able to find some WebLogic's internal classes and the
compilation will fail. This might be a bug owed by Sun JDK.

Now it is the time for a bumper harvest. You can open your
weblogic.properties file, search the word "deploy" and find
the place to deploy an ejb component. You can deploy a bean
as illustrated as follows:

weblogic.ejb.deploy=\
C:\workdir\hello_for_deploy.jar
# C:/weblogic/myserver/ejb_basic_beanManaged.jar, \
# C:/weblogic/myserver/ejb_basic_containerManaged.jar, \
# C:/weblogic/myserver/ejb_basic_statefulSession.jar, \
# C:/weblogic/myserver/ejb_basic_statelessSession.jar, \
# C:/weblogic/myserver/ejb_extensions_finderEnumeration.jar, \
# C:/weblogic/myserver/ejb_extensions_readMostly.jar, \
# C:/weblogic/myserver/ejb_subclass.jar, \
# C:/weblogic/myserver/jolt_ejb_bankapp.jar


Now restart your WebLogic server. Look at the server log to see
if your hello bean has been successfully deployed. You can also
check the matter in Weblogic Console. Weblogic Console eats a lot
of memory. If your RAM is too small, you won't have luxury to
enjoy this beautiful console. To check your EJB components,
you jus look into Distributes Componets in the Weblogic Console.

How to make a client to communicate with the Bean is another story.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Sign in and Reply Report