Part 5 : Implementing the "SampleStartupBean" startup bean
In this step, we’ll finish the implementation for the startup bean.
First, open the SampleStartupBeanBean.java from the SampleStartupBeanEJB project. Add the following code snippet, to implement the start() function.
Listing 3. SampleStartupBeanBean start()
public boolean start() {
//User Calendar type interval
//(com.ibm.websphere.scheduler.UserCalendar Interface)
//Interval for repeating the task : 1 minute
String interval = "1minutes";
Scheduler scheduler;
try {
// Getting a handle to the scheduler
scheduler = (Scheduler) new InitialContext().
lookup("jndi/SampleScheduler");
MessageTaskInfo taskInfo = (MessageTaskInfo) scheduler.
createTaskInfo(MessageTaskInfo.class);
// Start time for the Messaging task : 30 seconds after
// the TimerMediationModule starts
java.util.Date startDate = new
java.util.Date(System.currentTimeMillis()+30000);
// Messaging Task Configuration
taskInfo.
setConnectionFactoryJndiName("jms/SampleTriggerQCF");
taskInfo.
setDestinationJndiName("jms/SampleTriggerQ");
taskInfo.
setStartTime(startDate);
taskInfo.
setRepeatInterval(interval);
taskInfo.
setNumberOfRepeats(-1);
taskInfo.
setName("SampleMessagingTask");
//Setting a sample text for the task message
taskInfo.setMessageData("Sample Trigger Message");
TaskStatus ts;
//Registering the task with the scheduler.
ts = scheduler.create(taskInfo);
taskID = ts.getTaskId();
System.out.println("++ Task Created with TaskID :"+taskID);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TaskInfoInvalid e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
|
This code first gets the handle to the SampleScheduler that we created using the admin console. Then, it creates a messaging task with the queue connection factory (SampleTriggerQCF) and the queue (SampleTriggerQ). This task is registered with the scheduler and the interval for repeating this task is specified as "1 minute". To read more about the UserCalendar type, refer the resources at the end of the tutorial.
The "taskID" variable is defined as a global static variable in the SampleStartupBeanBean class, so that we can persist it and use it to kill the task, when the TimerMediationModule stops.
Add this line of code as a global statement in the SampleStartupBeanBean class.
Listing 4. SampleStartupBeanBean global variable "taskID"
private static String taskID = "";
|
Now, we will implement the stop() function. Add the following code snippet in the bean class.
Listing 5. SampleStartupBeanBean stop()
public void stop(){
Scheduler scheduler;
try {
scheduler = (Scheduler) new InitialContext().
lookup("jndi/SampleScheduler");
scheduler.cancel(taskID,true);
System.out.println("++ Task Cancelled with TaskID :"+taskID);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
|
This logic will cancel the task when the TimerMediationModule (application) is stopped or uninstalled.
We are now ready to quickly test if our startup bean is functioning as expected. Check the TimerMediationModuleApp’s deployment description to see if the SampleStartupBeanEJB module is part of it. Now start the test server and deploy the TimerMediationModule. You should see a similar message in the console.
Listing 6. SystemOut console output
[8/24/08 23:35:12:701 IST] 0000007d SystemOut O ++ Task Created with TaskID :52
|
This shows us that a task with ID "52" has been created and is registered with the
scheduler. Now open the admin console and go to "Service Integration > Buses >
SampleSIBus > Destinations > SampleTriggerQ > QueuePoints >
SampleTriggerQ@widNode.server1-SampleSIBus". Open the Runtime tab. You should see the "Current Message Depth" field incrementing each minute. This shows that the scheduler is running the messaging task every minute and the task itself is putting the trigger messages in the queue.
Figure 22. Examining the message depth on SampleTriggerQ
Our SampleStartupBean and the messaging task are now ready and functional! In the
next section, we will complete the final step of creating and configuring the JMS Export for the TimerMediationModule, which would pick up the trigger messages.
|