GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes Calling EJB Bean with WebSphere application server Scheduler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Calling EJB Bean with WebSphere application server Scheduler" Watch "Calling EJB Bean with WebSphere application server Scheduler" New topic
Author

Calling EJB Bean with WebSphere application server Scheduler

nikola petanjak
Greenhorn

Joined: Apr 01, 2011
Posts: 19
Hello everyone,

considering the circumstances i was forced to find another solution for automatic calling EJB method. Unfortunately this was, for my opinion, less elegant and more limited solution.
I'll write short tutorial for how set WebSphere scheduler for all of you who got need to use it.

So here it is, using EJB 3.0 and WebSphere 7

1) Set scheduler in WebSphere console (you'll need database connection set as well, because scheduler automaticly creates few tables for managing tasks)

Go to resources -> schedulers -> new

Set JNDI name by which you'll call Scheduler from your application (in this example, it will be sched/testScheduler), and ofcourse choose database conneciton.

Poll interval - i'm not sure what is purpose of it because i set interval for tasks in application, but anyway i set it to 30min.

Table prefix - i guess choose whatever you prefer

Now after you created new Scheduler, select it in table (resources -> schedulers) and click 'Create tables'.

2) EJB part - i created stateless session bean and implemented com.ibm.websphere.scheduler.TaskHandler with all it's methods. Then i removed @Override annotations from implemented methods, and removed "implements com.ibm.websphere.scheduler.TaskHandler" part of code.

Before:



After:



Ofcourse, now i had to tell in ejb-jar.xml that my bean will implement com.ibm.websphere.scheduler.TaskHandler (as remote) and com.ibm.websphere.scheduler.TaskHandlerHome (as remote home):



Finally, here is code for setting and canceling tasks for particular scheduler (i call it in contextListener when application starts/stops)



So as you can see, taskInfo.setTaskHandler(home) is telling what bean will execute every time task is called.

Method to cancel all tasks with same name:





J Miller
Ranch Hand

Joined: Oct 21, 2010
Posts: 67
I'm trying to walk through this for an application I'm working on right now. Neither myself nor my team mates have a ton of experience with an EJB project, so excuse my ignorance if this is a basic question.

How do I set up a context listener for application startup?

I've set up my bean and my task, but have no idea how to set up a listener, and can't find anything on the web. Thanks.
nikola petanjak
Greenhorn

Joined: Apr 01, 2011
Posts: 19
Hi

Me neither have a lot of experience but i'll do my best to explain.

Context listener is in Web part of application, so in my web.xml file i register it (controller is a name of package, ContextListener is name of a class):



Also, your class must implement javax.servlet.ServletContextListener interface. ServletContextListener has two methods, one when application start and other when applicaiton stops:

nikola petanjak
Greenhorn

Joined: Apr 01, 2011
Posts: 19
I forgot to mention - i think you should stop task that you are about to create when application start because if applicition don't stop "normaly" task will "live on" so when you start application you will have 2 tasks actualy.
Aviram Segal
Greenhorn

Joined: Feb 03, 2013
Posts: 2
I've created a tutorial that explains how to do this:
https://github.com/aviramsegal/articles/blob/master/WebSphereScheduler/WebSphereScheduler.md
 
GeeCON Prague 2014
 
subject: Calling EJB Bean with WebSphere application server Scheduler