Listing 1: Timer-Producer Bean
public class TimerProducerBean implements SessionBean {

private SessionContext sessionContext;
private TimerConsumer consumer = null;
private Controller controller = null;

public void ejbCreate() throws CreateException {

// 1. Lookup for TimerConsumer Home 
// 2. Create an instance of TimerConsumer bean
consumer = timerConsumerHome.create();

// 3. Lookup for ControllerHome
// 4. Create an instance of controller bean
controller = controllerHome.create();
}

public void startBean(long timerValue) {
// keep running until shutdown 
while (keepRunning() ) { 
// invoke the onTimer method on consumer bean
consumer.onTimerEvent();
try{
//sleep until the next event.
Thread.sleep(timerValue);

}catch(InterruptedException e){
}
} //end while
}

private boolean keepRunning(){
//query controller bean for run status
return controller.checkRunning();
}
}//end of class

Listing 2: Timer-Consumer Bean
public class TimerConsumerBean implements SessionBean {
private SessionContext sessionContext;

public void onTimerEvent( ) {
// Write the code and add appropriate exception handling.
// This Bean can be deployed as BEAN_MANAGED TX or 
//CONTAINER_MANAGED_TX based on the application requirements.
}
} //end of class

Listing 3: Controller Bean
public class ControllerBean implements SessionBean {
private SessionContext sessionContext;
private static boolean running = true;

public void shutdown( ) {
running = false;
}

public boolean checkRunning(){
return running; 
}
} //end of class

Listing 4: TDB Launcher
public class TDBLauncher {
private int numberOfTDBs =10;

public static void main(String args[]) throws Exception{

//start each invoker thread
for(int k =0; k < numberOfTDBs ; k++){
new TDBInvoker ().start();
}

try{ 
// Sleep for some time to ensure that all threads start successfully
Thread.sleep(30000); 
}catch(Exception e){ }

// Exit the JVM
System.exit(0);
}
} //end of class

Listing 5: TDB Invoker
public class TDBInvoker extends Thread{

public void run(){
TimerProducerHome home = null;
TimerProducer timerProducer = null;

// 1. Lookup for TimerProducerHome 
// 2. Create an instance of TimerProducer bean
timerProducer = home.create();

//3. Invoke the startBean(long timerValue) method on TimerProducer bean
// with a timer frequency of 600,00 milliseconds (10 minutes)
timerProducer.startBean(600000); 
}
}//end of class

Listing 6: TDB Shutdown Agent
public class TDBShutdownAgent {

public static void main(String args[]) throws Exception{
ControllerHome controllerHome = null;
Controller controllerBean = null;

// 1. Lookup for ControllerHome 
// 2. Create an instance of controller bean
controllerBean = controllerHome.create();

//3. Invoke the shutdown method on controller bean.
ControllerBean.shutdown();
}
}//end of class

Listing 7: Message-Invoker Bean
public class MessageInvokerBean implements SessionBean {
private SessionContext sessionContext;
private MessageConsumerBean consumer = null;
private Conrtoller controller = null;

public void ejbCreate() throws CreateException {

// 1. Lookup for MessageConsumer Home 
// 2. Create an instance of MessageConsumer bean
consumer = messageConsumerHome.create();

// 3. Lookup for ControllerHome
// 4. Create an instance of controller bean
controller = controllerHome.create();
}

public void startMDB() {
// keep running until shutdown
while (keepRunning() ) { 
//call processMessage()
consumer.processMessage();
}
}

private boolean keepRunning(){
//query controller for run status 
return controller.checkRunning();
}

}//end of class

Listing 8: Message-Consumer Bean
public class MessageConsumerBean implements SessionBean {

private SessionContext sessionContext;
private QueueConnection queueConnection = null;
private String recvQueueName = null;
private long waitTime = 15000; // 15 seconds.

public void ejbCreate() throws CreateException {
//1. Lookup for QueueConnectionFactory
//2. Lookup for recvQueueName
}


public void processMessage( ) {
//create the session, queue and receiver object
QueueSession session =queueConnection.createQueueSession(false, 
Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(recvQueueName); 
QueueReceiver qReceiver = session.createReceiver(queue);

//wait for a message to arrive
Message msg = qReceiver.receive(waitTime);

if (msg != null) {
//process the message, performing required business logic
}
}

} //end of class

Listing 9: MDB Launcher
public class MDBLauncher {
private int numberOfMDBs =10;

public static void main(String args[]) throws Exception{

//start the MDB invoker threads 
for(int k =0; k < numberOfMDBs ; k++){
new MDBInvoker ().start();
}

//wait for some time to ensure that all the invokers start successfully
try{ 
Thread.sleep(30000); 
} catch(Exception e){}

//Exit the JVM
System.exit(0);
}
} //end of class

Listing 10: MDB Invoker
public class MDBInvoker extends Thread{

public void run(){
MessageInvokerHome home = null;
MessageInvoker messageInvoker = null;

// 1. Lookup for MessageInvokerHome 
// 2. Create an instance of MessageInvoker bean
messageInvoker = home.create();

//3. Invoke the startMDB() method on MessageInvoker bean
messageInvoker.startMDB( );
}
} //end of class

Listing 11: MDB Shutdown Agent
public class MDBShutdownAgent {

public static void main(String args[]) throws Exception{
ControllerHome controllerHome = null;
Controller controllerBean = null;

// 1. Lookup for ControllerHome 
// 2. Create an instance of controller bean
controllerBean = controllerHome.create();

//3. Invoke the shutdown method on controller bean.
ControllerBean.shutdown();
}
}//end of class