1

I have a small Java Websocket Servlet running on Tomcat 8.0.5. Usually, it answers every received message immediately. But in some use case, it would be nice to have a scheduled event, that runs exactly once after some delay and sends new messages to clients. I've already done some research, but I am not sure whether those articles fit my requirements:

How to run a background task in a servlet based web application?

How to schedule a task in Tomcat

My approach to solve this would be some kind of a Service, that takes an integer for the delay and some other required parameters. Based on that it creates a new Thread that will send some new messages after doing Thread.sleep(integer).

Is that an appropriate solution or is there some 'best practice' to solve this?

Community
  • 1
  • 1
Stefan Braun
  • 390
  • 4
  • 12
  • what about quartz scheduler? – Scary Wombat May 15 '14 at 07:35
  • What you should do is apply a limited thread pool; just spawning threads at will is not a good idea in a threaded server environment as you can seriously disrupt the server's ability to manage resources. The ExecutorService noted by Oleg can help to do this, but Quartz also internally uses a thread pool so it too is a decent solution with lots of flexibility. – Gimby May 15 '14 at 07:59
  • @IwishIcouldthinkofagood quartz really looks like a nice way to do this - but isn't it some kind of overkill to use a scheduling framework for this? I do not know how much it impacts to performance - Would you use it in my use case? – Stefan Braun May 15 '14 at 08:15
  • @Gimby I just started to read Olegs answer - unfortunately I pressed F5 and it looks like he deleted it ... – Stefan Braun May 15 '14 at 08:17
  • Weird, it was a decent enough answer. The gist of the matter is: don't just spawn threads, apply a limited thread pool. Its up to you to decide what is overkill and what is not. – Gimby May 15 '14 at 08:23

0 Answers0