开发者

Background job with a thread/process?

开发者 https://www.devze.com 2023-04-07 04:51 出处:网络
Technology used: EJB 3.1, Java EE 6, GlassFish 3.1. I need to implement a background job that is execute every 2 minutes to check the status of a list of servers. I already implemented a timer and my

Technology used: EJB 3.1, Java EE 6, GlassFish 3.1.

I need to implement a background job that is execute every 2 minutes to check the status of a list of servers. I already implemented a timer and my function updateStatus get called every two minutes.

The problem is I want to use a thread to do the update because in case the timer is triggered again but my function called is开发者_开发百科 not done, i will like to kill the thread and start a new one.

I understand I cannot use thread with EJB 3.1 so how should I do that? I don't really want to introduce JMS either.


You should simply use and EJB Timer for this.

When the job finishes, simply have the job reschedule itself. If you don't want the job to take more that some amount of time, then monitor the system time in the process, and when it goes to long, stop the job and reschedule it.

The other thing you need to manage is the fact that if the job is running when the server goes down, it will restart automatically when the server comes back up. You would be wise to have a startup process that scans the current jobs that exist in the Timer system, and if yours is not there, then you need to submit a new one. After that the job should take care of itself until your deploy (which erases existing Timer jobs).

The only other issue is that if the job is dependent upon some initialization code that runs on server startup, it is quite possible that the job will start BEFORE this happens when the server is firing up. So, may need to have to manage that start up race condition (or simply ensure that the job "Fails fast", and resubmits itself).

0

精彩评论

暂无评论...
验证码 换一张
取 消