The following examples are located under $JONAS_ROOT/examples/src:
The eb example ($JONAS_ROOT/examples/src/eb/) uses Entity beans. (See Section 2.2.2 An Example With Database Access.)
The two beans share the same interface (Account): one uses bean-managed persistence (explicit persistence), the other uses container-managed persistence (implicit persistence).
This is a good example for understanding what must be done, or not done, for persistence, based on the chosen mode of persistence. It provides both a CMP 1.1 and a CMP 2.0 implementation.
The lb example ($JONAS_ROOT/examples/src/lb/) uses Entity beans with local interfaces.
A Session Bean, Manager, locally manages an Entity Bean, Manac, which represents an account.
This is a good example for understanding what must be done for a local client collocated with an Entity Bean providing local interfaces.
The jms directory ($JONAS_ROOT/examples/src/jms/) contains a stateful Session Bean with methods performing JMS operations and a pure JMS client message receptor.
A complete description of this example is in Section 26.6 A JMS EJB Example.
The mailsb directory ($JONAS_ROOT/examples/src/mailsb/) contains a SessionMailer and MimePartDSMailer Stateful Session beans with methods providing a way for building and sending mail.
The mdb/samplemdb directory ($JONAS_ROOT/examples/src/mdb/samplemdb/) contains a Message Driven Bean that listens to a topic and an MdbClient, which is a pure JMS client, that sends 10 messages on the corresponding topic.
This is a very good example for understanding how to write and use message-driven beans.
The mdb/sampleappli directory ($JONAS_ROOT/examples/src/mdb/sampleappli/) contains the following:
Two Message-Driven beans, one listening to a topic (StockHandlerBean) the other listening to a queue (OrderBean)
An Entity Bean with container-managed persistence (StockBean) and a stateless Session Bean for creating the table used in the database.
SampleAppliClient sends several messages on the topic. Upon receipt of a message, the StockHandlerBean updates the database via the StockBean and sends a message to the queue inside a global transaction. All of the EJBs are involved in transactions that may commit or rollback.
Alarm (http://jonas.objectweb.org/current/examples/alarm/) is an application that watches alarm messages generated asynchronously through JMS. It utilizes the different techniques used in JOnAS:
An Entity Bean for AlarmRecord, persistent in a database
A Session Bean to allow clients to visualize Alarms received
A Message Driven Bean to catch Alarms
JMS to use a topic where Alarms are sent and received
Tomcat (for JSP pages and servlets)
Security.
Earsample (http://jonas.objectweb.org/current/examples/earsample/) contains a complete J2EE application. This sample is a simple stateful Session Bean that has synchronization and security.
This bean is accessed from a servlet in which the user is authenticated and JOnAS controls access to the methods of the bean. The servlet performs a variety of lookups (resource-ref, resource-env-ref, env-entry, ejb-ref, and ejb-local-ref) in the java:comp/env environment to illustrate how the uniform naming works in the servlets.
The CMP2 directory (http://jonas.objectweb.org/current/examples/cmp2/) contains an example illustrating most of the concepts of CMP 2.0.
jaasclient (http://jonas.objectweb.org/current/examples/jaasclient/) contains an example of a JAAS login module that illustrates the different methods for authentication.
There are different callback handlers that demonstrate how to enter identification at a command line prompt, either with a dialog or without a prompt (where the client uses its own login/password).
Each directory contains a README that explains how to build and run each example.
The eb example contains two Entity beans that manage Account objects.
The two beans share the same interface (Account); one with bean-managed persistence (BMP, explicit persistence), the other with container-managed persistence (CMP, implicit persistence). The default CMP implementation is CMP 1.1. A CMP 2.0 implementation is also provided and its use is described in the README.
Before running this example, perform the steps in:
In order to be able to access your relational database, JOnAS will create and use a DataSource object that must be configured according to the database that will be used.
These DataSource objects are configured via properties files. $JONAS_ROOT/conf contains templates for configuring DataSource objects for databases such as Oracle and PostgreSQL:
Depending on your database, you can customize one of these files with values appropriate for your installation. After doing so, you must update the property jonas.service.dbm.datasources in the jonas.properties file.
For example, for the Oracle1.properties file.
jonas.service.dbm.datasources Oracle1 |
Section 3.5.7 Configuring the Database Service provides more details about DataSource objects and their configuration.
The $JONAS_ROOT/examples/src/eb directory contains an SQL script for Oracle: Account.sql ($JONAS_ROOT/examples/src/eb/Account.sql). If your Oracle server is running and you are using CMP 1.1, you can create the table used by the example. If you are using CMP 2.0, do not create the table.
The JDBC driver classes must be accessible from the classpath. To enable that, update the config_env file (http://jonas.objectweb.org/current/bin/unix/config_env).
In this file, set one of the following variables: IDB_CLASSES, ORACLE_CLASSES, or POSTGRE_CLASSES with the appropriate value for your database installation.
The simplest way to compile this example is to go to the $JONAS_ROOT/examples/src/eb directory ($JONAS_ROOT/examples/src/eb/) and use the compile.sh shell script ($JONAS_ROOT/examples/src/eb/compile.sh).
If the Ant 1.5 build tool is installed on your machine, you can build the JOnAS examples by using the build.xml files located in the $JONAS_ROOT/examples or $JONAS_ROOT/examples/src directories. To do this, use the build.sh shell script.
Here, again, two processes are involved:
The JOnAS server in which beans will be loaded
The Java client that creates instances of beans and calls business methods on it.
To run this example:
Run the JOnAS server to make beans available to clients:
service jonas start jonas admin -a eb.jar |
The following messages are displayed on the standard output:
The JOnAS Server 'jonas' version-number is ready and running on rmi message-header : AccountExpl available message-header : AccountImpl available |
Run the Java clients in another terminal emulator window:
jclient eb.ClientAccount AccountImplHome jclient eb.ClientAccount AccountExplHome |
The example eb has run successfully if the following output displays:
Getting a UserTransaction object from JNDI Connecting to the AccountHome Getting the list of existing accounts in database 101 Antoine de St Exupery 200.0 102 alexandre dumas fils 400.0 103 conan doyle 500.0 104 alfred de musset 100.0 105 phileas lebegue 350.0 106 alphonse de lamartine 650.0 Creating a new Account in database Finding an Account by its number in database Starting a first transaction, that will be committed Starting a second transaction, that will be rolled back Getting the new list of accounts in database 101 Antoine de St Exupery 200.0 102 alexandre dumas fils 300.0 103 conan doyle 500.0 104 alfred de musset 100.0 105 phileas lebegue 350.0 106 alphonse de lamartine 650.0 109 John Smith 100.0 Removing Account previously created in database ClientAccount terminated |
Before ending this session, be sure to stop the JOnAS server:
service jonas stop |