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

    Default Problem in recurssive call

    Hi

    I have a java program that runs 24x7. The code dos web mining and if there is an exception the function is called again in the catch block. There are occasions when there are like 200-300 continuous exceptions. After this although the process runs but the code does not do anything. Could this be because a stack is created which goes out of memory coz this recursion would occur when ever there is an exception in the code.

    Thanks

    Sachin

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you do any logging? If so, does that tell you anything?

    Myself, I think I'd re-design this app and not use recursion for this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    It would be nice if you showed us some of the relevant code; it helps to reduce the guessing factor.

    kind regards,

    Jos

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

    Default

    Thanks for replying.

    This is the code:

    My main looks like this:

    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);
    //recurssivecallforlostconnection(); // RECURSSIVALY CALLS THE PROGRAM WHEN CONTINOUS CONNECTION WITH URL SEND EXCEPTION //

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

    catch(Exception e1)
    {

    }
    }
    }
    }


    and the function that is called when an exception occurs would be:


    public static void recurssivecallforlostconnection()
    {
    try
    {
    while(true)
    {
    Thread.sleep(120000); // PROGRAM SLEEPS FOR 6 SECONDS //
    startwarningsystem();
    }
    }

    catch (Exception e)
    {

    writeexceptionstoafile("Exception in recurssivecallforlostconnection " + e);
    sendemailafterexception("Exception in recurssivecallforlostconnection " + e);
    recurssivecallforlostconnection(); // RECURSSIVALY CALLS THE PROGRAM WHEN CONTINOUS CONNECTION WITH URL SEND EXCEPTION //
    }
    }

    Well I can not redesign the code but I can run a script that kills the process and starts it again every week. But still as fara s the java programmign is concerned I want to know the reason of it not working.

    thanks again

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    what is this method? startwarningsystem();
    Also Thread.sleep(120000); // PROGRAM SLEEPS FOR 6 SECONDS // will actually sleep the program for 120 sec, or 2 min.

    Oh, and if there is an exception in your code somewhere, you are going to call the function for infinity, which will cause another exception, calling the function again, exception again...

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

    Default

    Hi

    Thanks for replying.

    startwarningsystem() method does some work. but with web mining i do get exceptions and yes it would keep calling the function recursively until the exception stops. the program sleeps for 2 mins. I think I have to increase the sleep time to decrease the number of recursive calls. But is there any other method that can be used

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    I want to know the reason of it not working
    Its Poorly designed

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

    Default

    norm

    I would have appreciated if you could have come with a solution. thanks n e ways

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    The solution is to redesign the code so it doesn't use recursion.

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

    Default

    Mr. norm

    here is how it works. The code is suppose to run 24x7 coz its a flood warning system. Now if there is an exception the code will not run. Then what do i do???

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    That sounds like a poor design. Many programs use try catch blocks to handle exceptions and then continue without recursion.

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

    Default

    I think the best solution would be to put the code in the main function under an infinite loop.

Similar Threads

  1. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 11:03 AM
  2. call chaining
    By vlad in forum New To Java
    Replies: 2
    Last Post: 05-06-2010, 09:56 PM
  3. GUI problem when call another class
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 08:29 AM
  4. recursion and call stack problem
    By OptimusPrime in forum New To Java
    Replies: 4
    Last Post: 12-26-2009, 09:49 PM
  5. not able to call the ejb
    By deepak9976 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-07-2009, 10:12 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
  •