Results 1 to 7 of 7
  1. #1
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default Run Java code 24x7

    Hi

    I had asked this question earlier but could not get a definite answer. So I am putting this question again in a different way. I have to run a java code 24x7 because its a warning system and it has to trigger a warning when the values being checked go beyond a threshold value. Now one of the warning is a flood warning which does a web mining for extracting certain value. Some times coz there is some problem with the web site an exception is thrown. If I don't run that code in an infinite loop then the code will terminate after exception. While I am running the code in an infinite loop after throwing exception for like 15-20 mins it stops working. Any solution will be greatly appreciated.

    Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I'm not sure what you mean by stops working. Generally, if you had an inifnite loop in a thread runnable class, you could catch the exception and continue. Perhaps if by stops working you are experiencing out of memory exceptions or permanent failure to re-query this web resource, it is possible you are not closing the URL connection resources or that they are not being garbage collected fast enough, if you are making a really high number of request per second.

  3. #3
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Thanks

    This is my code:



    public static void main(String[] args)
    {
    System.out.println("Server started");

    while(true)
    {
    try
    {
    /****************** INFINITE LOOP WILL MAKE THE PROGRAM RUN IN BACKGROUND CONTINOUSLY *****************/

    //while(true)
    {
    startwarningsystem();
    Thread.sleep(120000);
    //System.out.println("currtime in loop ");
    }

    /************************************************** ************************************************** ******/
    }

    catch (Exception e)
    {
    System.out.println("currtime in loop " + e);

    try
    {
    Thread.sleep(120000);


    writeexceptionstoafile("Exception in main " + e);
    sendemailafterexception("Exception in main " + e);
    }

    catch(Exception e1)
    {

    }
    }
    }
    }


    I am closing all the connections made like database. startwarningsystem() function makes a url connection coz i am mining data from a website every 2 mins. afetr the execution of the function it sleeps for 2 mins. By stop I mean this function is not working any more.

    thanks

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Not yet. I get exception only when there is some problem with the website. But in general if you have to run a code 24x7 you would put that in an infinite loop right. Or is there any other better way of doing it considering the case when there could be an exception.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Infinite loop is the way to work around. If you've an endless loop then on an exception what you've to do is reverse the logic back to the top of the while loop, for further execution.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    First off you don't need two loops, surely?
    Or two calls to Thread.sleep().

    Secondly you're eating exceptions in the second try/catch...so you could be getting exceptions thrown during the processing of an exception and never know.

    Create a single loop, with the first try/catch inside it (and use CODE tags if you're posting it here). Call the startwarningsystem(). The catch block will just have the error handling (no sleep() call), and will deal with any exceptions thrown during this error handling (ie at the very least logging them). Add a finally block which will contain the sleep() call.

    That probably won't fix your problem, but it will tidy up your code a bit by removing unecessary lines of code.

Similar Threads

  1. need help with this java code
    By nobody58 in forum Advanced Java
    Replies: 7
    Last Post: 03-17-2010, 01:08 PM
  2. java code
    By noor in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 02:00 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. java code
    By sobhalr in forum New To Java
    Replies: 0
    Last Post: 12-28-2007, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •