Chapter 30. Exercises for Chapter 13

Exercise 13.1: EJB Timer Service

In this exercise, you will learn how to use work with the EJB Timer Service. The examples in this chapter match the modifications made to the Ship EJB to enable Timers. This exercise builds off the code within Exercise 12.1, so initialization and deployment should be around the same.

Clean the Database

You need to clean and refresh the database. To do this, shutdown JBoss if you have it running and run ant clean.db. Then restart JBoss.

Build and Deploy Example Programs

Perform the following steps:

  1. Open a command prompt or shell terminal and change to the ex13_1 directory created by the extraction process.

  2. Set the JAVA_HOME and JBOSS_HOME environment variables to point to where your JDK and JBoss 4.0 are installed. Examples:

    Windows:C:workbookex13_1> set JAVA_HOME=C:jdk1.4.2 C:workbookex13_1> set JBOSS_HOME=C:jboss-4.0
    Unix:$ export JAVA_HOME=/usr/local/jdk1.4.2 $ export JBOSS_HOME=/usr/local/jboss-4.0
  3. Add ant to your execution path. Ant is the build utility.

    Windows:C:workbookex13_1> set PATH=..antin;%PATH%
    Unix:$ export PATH=../ant/bin:$PATH
  4. The exercise uses a JMS Topic. Deploy the topic using the following Ant target.

    $ ant make-topic
  5. Build the EJBs used in this example.

    $ ant

    You will see titan.jar copied to the JBoss deploy directory and redeployed by the application server.

  6. Initialize the database.You will see a bunch of entity beans being created.

$ ant createdb

Examine the Service Code

The scheduleMaintenance, clearSchedule, and ejbTimeout methods from the EJB book have been added to Ship EJB to show the EJB Timer Service in action.

ShipBean.java

public void scheduleMaintenance(String descr, Date dateOf) {
   TimerService timerService = ejbContext.getTimerService( );
   timerService.createTimer(dateOf, description);
}

public void clearSchedule( ) {
   TimerService timerService = ejbContext.getTimerService( );
   java.util.Iterator timers = timerService.getTimers( ).iterator( );
   while (timers.hasNext( )) {
      System.out.println("Cancelling maintenance on ship: " + getName( ));
      javax.ejb.Timer timer = (javax.ejb.Timer) timers.next( );
      timer.cancel( );
   }
}

public void ejbTimeout(javax.ejb.Timer timer)   {
   String description = (String) timer.getInfo( );

   try {
      InitialContext jndiContext = new InitialContext( );
      TopicConnectionFactory factory =(TopicConnectionFactory)
              jndiContext.lookup("java:comp/env/jms/TopicFactory");

      Topic topic = (Topic)
         jndiContext.lookup("java:comp/env/jms/MaintenanceTopic");

      TopicConnection connect = factory.createTopicConnection( );
      TopicSession session = connect.createTopicSession(true, 0);
      TopicPublisher publisher = session.createPublisher(topic);

      TextMessage textMsg = session.createTextMessage( );
      textMsg.setText(getName( ) + " " + description);
      publisher.publish(textMsg);
      session.close( );
      connect.close( );
   } catch (Exception e){
      throw new EJBException(e);
   }
}

A stateless session bean has been added to the ship package so that the Ship EJB methods can be called remotely. The scheduleMaintenance and clearSchedule methods look up a Ship EJB and call those methods on that particular entity bean.

ShipMaintenanceBean.java

public void scheduleMaintenance(int shipId, int secs, String desc) {
   try {
      ShipHomeLocal shiphome =(ShipHomeLocal)
          jndiContext.lookup("java:comp/env/ejb/ShipHomeLocal");

      ShipLocal ship = 
         shiphome.findByPrimaryKey(new Integer(shipId));
      Date dateOfTest = new Date(System.currentTimeMillis( ) 
                                 + (secondsToSchedule * 1000));
      ship.scheduleMaintenance(description, dateOfTest);
   } catch (NamingException e){
      throw new EJBException(e);
   } catch (javax.ejb.FinderException e){
      throw new EJBException(e);
   }
}

public void clearSchedule(int shipId) {
   try {
      ShipHomeLocal shiphome =(ShipHomeLocal)
          jndiContext.lookup("java:comp/env/ejb/ShipHomeLocal");

      ShipLocal ship = 
         shiphome.findByPrimaryKey(new Integer(shipId));
      Date dateOfTest = new Date(System.currentTimeMillis( ) 
                                 + (secondsToSchedule * 1000));
      ship.clearSchedule( );
   } catch (NamingException e){
      throw new EJBException(e);
   } catch (javax.ejb.FinderException e){
      throw new EJBException(e);
   }
}

Examine the Client Code

There are four client programs used to run the examples. The programs are in the com.titan.clients package. InitDB.java calls code in TravelAgentEJB to create all the entity beans needed for this example. JmsClient_1.java listens on the MaintenanceTopic for published messages from the ejbTimeout method in ShipEJB. MaintenanceScheduler.java initiates a call to the ShipMaintenance EJB to schedule maintenance. CancelMaintenance.java initiates a call to the ShipMaintenance EJB to clear the maintenance schedule. We don’t walk through the code for these programs because they are a quite straightforward example of invoking on a stateless session bean.

Run the Example

The first thing you must do to run the example is start up the JMS client that listens for maintenance messages. Launch another console window and initialize its environment as described above. Start the JMS client by executing the following Ant target:

C:workbookex13_1>ant run.watcher

You should see the following displayed on the console:

C:workbookex13_1>ant run.watcher
Buildfile: build.xml

prepare:

compile:

ejbjar:

run.watcher:
     [java] Listening for messages on topic/titan-MaintenanceTopic...

Next, you can schedule maintenance to a ship by running the ScheduleMaintenance script. There is one provided for both Windows and Unix. To run this script, you need to provide a ship ID (101 or 102), the time in seconds for when you want the maintenance scheduled, and finally a description of the maintenance that will be scheduled:

C:workbookex13_1>ScheduleMaintenance 101 5 propellar

After five seconds, you should see the JMS client console window show up with the scheduled maintenance:

C:jbossworkbookex13_1>ant run.watcher
Buildfile: build.xml

prepare:

compile:

ejbjar:

run.watcher:
     [java] Listening for messages on topic/titan-MaintenanceTopic...

     [java]  MAINTENANCE SCHEDULED:
     [java] Nordic Prince propeller

You can cancel any maintenance by running the CancelMaintenance script before the ejbTimeout executes. There is one provided for both Windows and Unix. To run this script, you need to provide a ship ID (101 or 102):

C:workbookex13_1>CancelMaintenance 101
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.93.12