3

I want to start a service which starts along with Server and never ends until server is killed. So first I have gone with ServletContextListner class where I implemented my logical part to run the method using

while(true){ 
    try{ // do the jobs } 
    catch(Exception e){} 
}

But then i felt it's not good to implement this job at Listener class. Then I moved to one ServiceManager class and doing the same job, but gives me an edge in injecting properties using Spring which is not possible in Listener class.

But fundamental question on how better/from where can I invoke this class and call the startService method which runs infinitly.

public void contextInitialized(ServletContextEvent event) {
        logger.info(" *** START MyListener ****");
        context = event.getServletContext();
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        MyServiceManager serviceManager = (MyServiceManager) ctx.getBean("myServiceManager");
        serviceManager.startService();
        logger.info(" *** END MyListener ****");
    }

or any idea how to invoke/implement such service to run in server forever without any abstractions [under any case, this shouldn't be killed unless server is stopped]

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • What is the problem? Using a ServletListener's contextInitialized to start and contextDestroyed to stop the service should do the trick. What do you mean by "without any abstractions"? – Pyranja Dec 13 '12 at 08:45
  • What about 1. Create a Servlet and put your service code in init method 2. Configure that in web.xml with as a start up servlet – Binu Dec 14 '12 at 09:24
  • I think the problem is, server is not finishing startup until the control goes out of contextInitialized method. because, everything is running fine and the startService methods runs forever as expected. But Tomcat throws error after server startup time is over. – RaceBase Dec 17 '12 at 05:22

2 Answers2

1

This is a duplicate question see: Background process in Servlet

So you can do what is explained in that answer and if you need to then load your Spring configured beans using applicationContext.getBean("yourBean");

Community
  • 1
  • 1
SkyWalker
  • 13,729
  • 18
  • 91
  • 187
  • But in my case, I just need to make sure that this thread never killed and runs forever or never goes into any state (dead) unless server stops. – RaceBase Dec 14 '12 at 06:58
0

Are you open to alternate technologies, namely Node.js ?

I am not sure what your server does, however I had to build a "always-on" Push notification server for iOS and I used Node.js to do that.

If yes, you can build your system like this:

Building the system

1) You setup a web service using your Java to return JSON data to your Node.js server when your Java server receives a HTTP web request (POST request)

2) Your Node.js makes the POST request to your Java server, gets the results and parses it and sends it to your other systems

Always-On Server with fault tolerance

With Node.js installed, you can use Node.js's NPM (Node.js Package Manager) to install the Forever tool.

Then using Forever tool, you can go:

forever start server.js

to start the infinite process with automatic fault tolerance, you can also list the daemon jobs currently managed by forever tool:

forever list

Note: you do not need to use infinite while loops with Node.js, when you start a Node.js http server, it will keep running until you tell it to stop.

When your server starts up, restarts, loses power or whatever, Forever will automagically spawn a new process, giving your automatic fault tolerance.

Hope that helps.

Zhang
  • 11,549
  • 7
  • 57
  • 87