Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer Task

 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if a TimerTask object is deployed in a clustered environment? Does it schedule a new task on each node?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing in TimerTasks to make them cluster aware, so it schedules a new task for each JVM. You could implement this yourself, by having them check whether another is already running (via some locking mechanism such as a process available over RMI). Or if your clustered environment is an ejb container you could use an EJB Timer service.
[ May 21, 2008: Message edited by: Paul Sturrock ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on where you create the TimerTask, if that component is being started at every node, the timer task also will be run.
TimerTask by itself would not have the idea about the clustered environment, unless you put the intelligence in it.
 
visu Nekk
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nitesh and Paul, Is RMI the only way to add intelligence or is there any other way? I am running my TimerTask in JBoss server. Please guide me in brief on adding intelligence to it.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your timer runs in a clustered environment, you need some network technology that can be accessed from each node in the cluster. RMI was just an off-hand suggestion. You could equally use a database, or a shared JNDI namespace. The logic is pretty simple: before starting your timer check whether there is already already running. Have each timer update something via whichever repository you choose to alert to others that they are running.

However, if you are using JBoss and EJB 3, why not use an EJB Timer Service? It can do all the above for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic