JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of...

42
JBoss [http://www.jboss.org] Weiqi Gao Principal Software Engineer Object Computing, Inc. http://www.ociweb.com Thursday July 10, 2003 St. Louis Java Users Group http://www.ociweb.com [http://www.ociweb.com/javasig] Copyright © 2003 Weiqi Gao

Transcript of JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of...

Page 1: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JBoss [http://www.jboss.org]

Weiqi GaoPrincipal Software Engineer

Object Computing, Inc. http://www.ociweb.com

Thursday July 10, 2003St. Louis Java Users Group http://www.ociweb.com [http://www.ociweb.com/javasig]

Copyright © 2003 Weiqi Gao

Page 2: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Agenda

Today’s Agenda focuses on the practical, "HOWTO",side.

• What Is JBoss?• Setup JBoss• Ant, XDoclet, Etc.• Data Sources• Start a Project• Generated Code

Copyright © 2003 Weiqi Gao 2 / 41

Page 3: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Agenda (Continued)

• JBoss Class Loading• Configure Logging

Copyright © 2003 Weiqi Gao 3 / 41

Page 4: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

What Is JBoss?

• A popular open source Java application server that supports theJ2EE 1.3 specifications

• Runs under any J2SE 1.3 or later Java virtual machine• Based on an JMX core where other pieces of the system areplugged in

• Supports JNDI, Servlet/JSP (Tomcat or Jetty), EJB, JTS/JTA,JCA, JMS

• Also supports Clustering (JavaGroups), Web Services (Axis),and IIOP integration (JacORB)

Copyright © 2003 Weiqi Gao 4 / 41

Page 5: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

What Is JBoss? (Continued)

• Production release version is 3.2.1. Developer release versionis 4.0.0DR2

• Released under the LGPL

Copyright © 2003 Weiqi Gao 5 / 41

Page 6: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Setup JBoss

The JBoss project is hosted on Source Forge [http://sourceforge.net/projects/jboss].

The Files [http://sourceforge.net/project/showfiles.php?group_id=22866]section contains all JBoss releases since 2.2.

The files page contains sections named JBoss , JBoss-Jetty ,JBoss-Tomcat .

For this talk we will use jboss-3.2.1_tomcat-4.1.24.zip .

Installation is easy:

$ cd /opt$ unzip /download/jboss-3.2.1_tomcat-4.1.24.zip

Copyright © 2003 Weiqi Gao 6 / 41

Page 7: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Setup JBoss (Continued)

This creates the JBoss directory structure [inspect-dirs.txt]. Theserver subdirectory houses the minimal , default , and all config-uration sets.

To run a configuration set, say all , simply do:

$ cd /opt/jboss-3.2.1_tomcat-4.1.24/bin$ ./run.sh -c all

Copyright © 2003 Weiqi Gao 7 / 41

Page 8: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JMX Console

Copyright © 2003 Weiqi Gao 8 / 41

Page 9: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JMX Console (Continued)

Copyright © 2003 Weiqi Gao 9 / 41

Page 10: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Web Console

Copyright © 2003 Weiqi Gao 10 / 41

Page 11: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Web Console (Continued)

Copyright © 2003 Weiqi Gao 11 / 41

Page 12: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Closer Look

bin [inspect-bin.txt] contains run.sh , shutdown.sh , and bootstrap-ping jar files.

client [inspect-client.txt] contains jar files that must be specifiedwhen compiling J2EE applications. Note that servlet.jar is inserver/default/lib . The server does not use this directory atrun time.

docs [inspect-docs.txt] contains DTDs for deployment descrip-tors as well as data source configuration scripts for supportedRDBMSs.

Copyright © 2003 Weiqi Gao 12 / 41

Page 13: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Closer Look (Continued)

lib [inspect-lib.txt] contains jar files needed to by JBoss. Do notput user jar files into this directory.

server [inspect-server.txt] contains the "factory" configurationsets. New configuration sets can be created here, starting witha copy of one of the preconfigured sets.

For each configuration set, conf [inspect-conf.txt] contains config-uration files, deploy [inspect-deploy.txt] contain deployed systemresources and hot-deployable user applications, and lib [inspect-server-lib.txt] contain non-hot-deployable jar files used by the con-figuration set.

Copyright © 2003 Weiqi Gao 13 / 41

Page 14: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Closer Look (Continued)

The data , log , and tmp directories are created if necessary whenthe server is started. The log/server.log [server.log] file containslots of useful information.

Copyright © 2003 Weiqi Gao 14 / 41

Page 15: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Ant, XDoclet, Etc.

Ant [http://ant.apache.org] is an indispensable build tool for Javadevelopment.

XDoclet [http://xdoclet.sourceforge.net] is a must have for EJBdevelopment.

JUnit [http://junit.org] is necessary for test-first design and devel-opment.

Download the latest releases and install by unpacking, and updateyour PATHand CLASSPATH:

Copyright © 2003 Weiqi Gao 15 / 41

Page 16: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Ant, XDoclet, Etc. (Continued)

$ cd /opt$ tar jxvf /download/apache-ant-1.5.3-1-bin.tar.bz2$ unzip /download/junit3.8.1.zip$ tar zxvf /download/xjavadoc-1.0.tar.gz$ mkdir xdoclet-1.2b3; cd xdoclet-1.2b3$ tar zxvf /download/xdoclet-bin-1.2b2.tar.gz

We also use CVS, MySQL, and PostgreSQL, which comes with RedHat Linux 9.0. CVS and PostgreSQL is also available as part ofCygwin [http://cygwin.com] for Windows.

Copyright © 2003 Weiqi Gao 16 / 41

Page 17: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JBoss as a Service

The Java Service Wrapper [http://wrapper.tanukisoftware.org] caninstall JBoss as a Windows service or Unix daemon. Its documen-tation uses JBoss as an example ;).

It consists of:

• a native executable wrapper (Wrapper.Exe )• a native shared library libwrapper.so (Wrapper.Dll )• a Java library wrapper.jar

• a few shell scripts (batch files)• and a configuration file wrapper.conf

Copyright © 2003 Weiqi Gao 17 / 41

Page 18: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JBoss as a Service (Continued)

All of these files are copied to JBoss’s bin directory. Thewrapper.conf file is modified to suit JBoss’s needs.

On Windows, batch files are provided to install and uninstall JBossas a service. On Red Hat Linux, the shell script can be softlinkedto /etc/rc.d/init.d to serve as init-script.

Copyright © 2003 Weiqi Gao 18 / 41

Page 19: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Data Sources

JBoss uses JCA to configure JDBC data sources. To configure adata source, we need:

• The JDBC driver jar file and its java.sql.Driver class name• The JDBC URL• The user name and password

docs/examples/jcs contains data source templates: oracle-ds.xml,

[oracle-ds.xml] mysql-ds.xml, [mysql-ds.xml] postgres-ds.xml ,etc.

Copyright © 2003 Weiqi Gao 19 / 41

Page 20: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Data Sources (Continued)

To configure a data source available to a configuration set, wesimply:

• Copy the JDBC driver to its lib directory• Copy the appropriate *-ds.xml to its deploy directory• Modify the *-ds.xml to reflect the correct JDBC URL, user nameand password

For Oracle, transaction-service.xml also needs modification.

Copyright © 2003 Weiqi Gao 20 / 41

Page 21: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Start a Project

There are many starting points for a project. From a physical pointof view. The first step should be to setup the environment and abuild loop.

The build.xml of the hello project started out like this:

<project default="junit"><target name="junit">

<junit printsummary="on" fork="false"haltonfailure="false"showoutput="true"><classpath refid="cp"/>

Copyright © 2003 Weiqi Gao 21 / 41

Page 22: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Start a Project (Continued)

<formatter type="brief" usefile="false"/><batchtest>

<fileset dir="src"includes="**/Test*.java"/>

</batchtest></junit>

</target></project>

Copyright © 2003 Weiqi Gao 22 / 41

Page 23: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Project Structure

Slight elaboration on the starter build.xml file will give us a fullbuild.xml file that is capable to generate code, compile, package,deploy and run the entire project.

Here’s a pictorial depiction [graphics/dependency_graph.png] ofthe full build.xml.

As the build.xml takes shape, the directory structure of the projectalso become clear:

src , resources The source files, resource files, deploy-ment descriptors, configuration files.

Copyright © 2003 Weiqi Gao 23 / 41

Page 24: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Project Structure (Continued)

gen , classes , staging , dist Directories for generated code, com-piled code, a staging area for intermedi-ate jars (the EJB jar that will go into theEAR), and the final deliverables (the EAR,and the client program.

Copyright © 2003 Weiqi Gao 24 / 41

Page 25: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Session Bean

...

/*** @ejb.bean name="Hello"* type="Stateless"* view-type="remote"* transaction-type="Container"** @ejb.transaction type="Required"*/

public abstract class HelloBeanimplements SessionBean {

Copyright © 2003 Weiqi Gao 25 / 41

Page 26: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Session Bean (Continued)

/*** @ejb.interface-method view-type="remote"*/

public String sayHello(String message) {...

Copyright © 2003 Weiqi Gao 26 / 41

Page 27: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Entity Bean

/*** @ejb.bean name="Item"* type="CMP"* view-type="both"* primkey-field="item"** @ejb.transaction type="Required"** @ejb.finder* signature "java.util.Collection findAll()"** @jboss.persistence

Copyright © 2003 Weiqi Gao 27 / 41

Page 28: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Entity Bean (Continued)

* datasource="java:/OracleDS"* datasource-mapping="Oracle9i"*/

public abstract class ItemBeanimplements EntityBean {

Copyright © 2003 Weiqi Gao 28 / 41

Page 29: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Persistent Fields

/*** @ejb.create-method*/

public Integer ejbCreate(Integer item)throws CreateException {setItem(item);return null;

}/*** @ejb.interface-method view-type="both"* @ejb.persistence* @ejb.pk-field

Copyright © 2003 Weiqi Gao 29 / 41

Page 30: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Persistent Fields (Continued)

*/public abstract Integer getItem();

Copyright © 2003 Weiqi Gao 30 / 41

Page 31: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Message-Driven Bean

/*** @ejb.bean name="Note"* destination-type="javax.jms.Queue"* acknowledge-mode="Auto-acknowledge"* subscription-durability="NonDurable"* transaction-type="Container"** @jboss.destination-jndi-name name="Note"*/

public class NoteBean implementsMessageDrivenBean, MessageListener {private Context context;

Copyright © 2003 Weiqi Gao 31 / 41

Page 32: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

A Message-Driven Bean (Continued)

public void onMessage(Message message) {

Copyright © 2003 Weiqi Gao 32 / 41

Page 33: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Generated Code

This snippet of Ant script is responsible for generating all the EJBinterfaces and deployment descriptors:

<taskdef name="ejbdoclet"classname="xdoclet.modules.ejb.EjbDocletTask">

<classpath refid="cp"/></taskdef><ejbdoclet ejbspec="2.0" destdir="gen"

<fileset dir="src"includes="**/foo/server/*Bean.java"/>

<homeinterface/><remoteinterface/>

Copyright © 2003 Weiqi Gao 33 / 41

Page 34: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Generated Code (Continued)

<localhomeinterface/><localinterface/><session/><entitycmp/><deploymentdescriptor destdir="gen/ejb/META-INF"/><jboss version="3.2" destdir="gen/ejb/META-INF"/>

</ejbdoclet>

Copyright © 2003 Weiqi Gao 34 / 41

Page 35: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Running the App

Copyright © 2003 Weiqi Gao 35 / 41

Page 36: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Running the App (Continued)

Copyright © 2003 Weiqi Gao 36 / 41

Page 37: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Running the App (Continued)

Copyright © 2003 Weiqi Gao 37 / 41

Page 38: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JBoss Class Loading

JBoss’s class loading architecture needs a little getting used to.

• JBoss uses the concept of class loader repository. For anydynamically deployed file, such as EAR, WAR, EJB jar, RAR, andSAR files each is loaded with a new subordinate class loader.However they also register themselves with a loader repository.

• These class loaders will first ask the repository and then loadclasses from themselves.

• They may also decide to become the head of a new loaderrepository. Classes loaded into child loader repositories arenot visible to parent loader repositories.

Copyright © 2003 Weiqi Gao 38 / 41

Page 39: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

JBoss Class Loading (Continued)

• The order in which the class loaders are added to the repositorydo matter.

• The Russian doll model.

Add this jboss-app.xml along side with application.xml :

<jboss-app><loader-repository>

hello:service=LoaderRepository</loader-repository>

</jboss-app>

Copyright © 2003 Weiqi Gao 39 / 41

Page 40: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Configure Logging

JBoss uses Log4j for its internal logging. It controls Log4j throughthe conf/log4j.xml file. This is a normal Log4j XML config file withone addition. A JBoss specific TRACE level.

To request the trace service add a category section to log4j.xml :

<category name="org.jboss.mx.loading"><priority value="TRACE"

class="org.jboss.logging.XLevel"/></category>

You can also configure different File Appenders for different log-ging categories.

Copyright © 2003 Weiqi Gao 40 / 41

Page 41: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Support Resources

The JBoss Group [http://jboss.org] is founded by Marc Fleury, alsothe founder of the JBoss project, to provide training and consultingsupport for JBoss. The forums on JBoss’s website is answered byJBoss developers and contains lots of useful information.

The official mailing lists jboss-development and jboss-user arearchived at Source Forge. GMANE [http://gmane.org] offers a bidi-rectional nntp gateway to many mailing lists, including the JBosslists.

The Core Developers Network [http://coredevelopers.com] is anewly formed company that provide training and consulting ser-

Copyright © 2003 Weiqi Gao 41 / 41

Page 42: JBoss []java.ociweb.com/.../2003-07/jboss-presentation.pdf · JBoss as a Service (Continued) All of these files are copied to JBoss’s bin bin

Support Resources (Continued)

vices for not only JBoss, but also other Open Source Java prod-ucts.

Copyright © 2003 Weiqi Gao 42 / 41