2

I am creating a Java service which will run within a web servlet container (probably Tomcat). One portion of the server will run on its own and will not be initiated by HTTP. I know that when an HTTP call causes an exception, the web container can call it again.

I want to be sure that the part of the server which runs continuously will continue to run, even if it fails. I will handle whichever failures I can manually, but if it all fails I want something to restart it all. Are there any tools that can accomplish this easily? I am already using Spring and Tomcat, so if those can provide it, that is ideal. If not, then how about a good design pattern?

Edit: To clarify, I have a web service which will run in Tomcat. I want to run a separate thread within that service and set it up such that when the thread ends or an un-handled exception occurs, Tomcat (or something else) detects the failure and restarts the web service. I know that typically web containers have threads start from some external call and thus handle failures from those threads. What I want is something which handles a background worker thread.

7
  • "part of the server which runs continuously" is this like a separate thread that runs inside your server? Commented Sep 20, 2011 at 20:34
  • I think it will be a separate thread, but I'm open to other approaches. It could run as its own process. Commented Sep 20, 2011 at 20:36
  • Its very unclear what the life cycle of this "other service" is. Is it a singleton waiting for method calls or is a running process? Commented Sep 20, 2011 at 20:54
  • @Adam - it is a background worker thread Commented Sep 20, 2011 at 20:55
  • Why do you need a separate background thread? Commented Sep 20, 2011 at 21:23

4 Answers 4

3

Not quite clear on the design you have in mind, but it seems to me you need some sort of health check.
You can implement such a mechanism in many ways e.g. open a socket from this process that runs all time and periodically send a message.
If there is no reply then the process failed.
You could restart tomcat or implement a mechanism to restart that process.
Can not tell you more details since you do not specify much on what you are trying to do.

UPDATE: I think that you should use JMX. It is offered by Spring and Tomcat that you already use.
Just make the process you want to monitor a managed resource and another module can check if it is alive.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I believe that is what I am looking for. I'm very interested in finding any tools to make that easier to perform. Ideally, these tools would allow Tomcat (or whichever web container) to detect that my web service failed and restart it automatically.
1

If you are running inside a Servlet then as per J2EE spec, you cannot restart the container but, you can use ScheduledExecutorService to continuously monitor that your service is running and if not, then re-start it.

EDIT. More details below

You can call isTerminated() to check if the service still running and add more tasks to it, if the queue is empty.

Comments

0

I may be misunderstanding your problem here, but you might be over-thinking it.

There's nothing stopping you from running multiple Tomcat instances on a single machine. You could then have Server A connect to Server B to pull down information (via a web service of your choosing). This would alleviate the need for an outage on server A to cause an outage on server B (which is what I'm assuming you're trying to avoid).

This is a common way to isolate production environments simply by binding to a separate port. If Tomcat doesn't fit the bill for the service you can always run the application as a service on [insert operating system of choice] and connect to it via a proprietary protocol. Your operating system can handle restarts in that case. Typically I think the multiple Tomcat containers is the easiest approach as it is simple to install and relatively easy to set up.

Good luck, it seems like a fun system administration problem. You also might be interested in checking out Quartz job scheduling as that might fit the bill for an intermittent service.

edit: a little more detail might provide some more detailed answers.

Comments

0

See this post. It's a simple tomcat-watchdog shell script.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.