开发者

"Main loop" in a Java application on JBoss server

I am creating a J开发者_如何学CBoss 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 @Startup and @Singleton beans.

In short, you can write something like this:

@Startup @Singleton 
public class MainLoopBean {

   @PostConstruct   
   public void mainLoop() {   
   }

}

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.


If the connection overhead is really affecting your performance, you can change the setting for connection pooling in JBoss. This would make the application simpler, more robust and scalable. Writing to one file does not scale up to multiple parallel connections. It also requires more IO than writing to the DB directly.


Why are you considering loops at all? Why not set up a JMS queue and a listener on it, that way whenever something happens, you're able to respond. No need for loops, no special hooks, nothing.

Alternatively, if you're actually interested in doing something more complicated, look into the Java Connector Architecture, which provides you these kinds of hooks as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜