I am creating a JBoss server to deploy a java application which will be a REST-like servlet taking data from requests and placing them into a SQL database.
My main question: Is it possible to set up a class on the JBoss server which is not run based on requests but is more like a main loop. I.e. just a loop which will “sleep” then check some information and either do something or sleep again.
Basically what I am trying to do is write a bunch of data to a file, once that file fills up to a certain point, write it all at once to the database to reduce connection overhead.
My best guess is that I could write any kind of class with a loop and have it run in the way I want(as long as my “sleep” technique was correct to allow for the servlet on the same JBoss time to run).
What I don’t know though is how to get that main loop to run constantly; Just call it in the constructor?? The only way I know how to have things run on the server currently is to have a mapping set up in the web.xml and actively make a web page request information from the server…
Is there a better(read easier) service than JBoss and java for something like that
Thanks in advance, I have searched pretty hard for an explanation of something like this but it seems I am missing the right keyword…
Have a look at
@Startupand@Singletonbeans.In short, you can write something like this:
Ideally you should couple this with the timer service. When some amount of work is done and you want to pause, just schedule the method to be invoked later and return.