Reputation: 1027
I made JBoss (really Redhat EAP 6.2) RESTful webservice (JAX-RS) which basically queries another Java server. It's Java EE web app with Maven. However what I would like to do, is that my JBoss server queries other Java server every 1 minute, and when I query my JBoss server via web server I can pull all the history of queries sent by background worker to other java server. While I can do the persistence and so on, my question is what would be the best way to spawn a background worker in that JBoss?
Upvotes: 1
Views: 1145
Reputation: 1864
If you are using EJB3.1, then you can use @Schedule to set up a scheduled/timer task. If you don't use EJB3.1 but use Spring, then you use Spring's @Scheduled. If you don't use both, then you may want to rely on third party scheduler services like Flux or Quartz, which have more sophisticated scheduling features.
For example using EJB3.1, you can set up something like this -
import java.util.Date;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
@Stateless
public class BackgroundTaskProcessing
{
@Schedule(dayOfWeek = "*", hour = "*", minute = "*", second = "*/5", persistent = false)
public void backgroundTask()
{
System.out.println("I execute for every 5 seconds");
}
}
Incidentally, I asked something similar which you may be interested to keep an eye on.
Upvotes: 1