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?
Asked
Active
Viewed 1,112 times
1 Answers
1
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.

Community
- 1
- 1

IndoKnight
- 1,846
- 1
- 21
- 29
-
But I dont want scheduler, I want background worker to be started when jboss starts and keep it running all the time. The point is how to 1. how start the background worker during the start of jboss e.g. in which class 2. how to start the jboss background worker e.g. what class – Andrew Feb 16 '14 at 16:51
-
Well it's the same thing but I dont know 2 things: where to start background worker at the start, and what to start. I am not sure if Scheduler would be just right to start at the start and run only 1 instance. Correct me if I am wrong. – Andrew Feb 17 '14 at 17:05
-
May be you can use a stateless session bean on Jboss startup?http://www.mastertheboss.com/ejb-3/how-to-create-an-ejb-startup-service. – IndoKnight Feb 17 '14 at 22:58