简体   繁体   中英

how to make a jar file always running

i have a jar file: myServerSide.jar,
this jar takes request from client apps, processes them, each one ina thread and renders a response
i've put my jar on linux, but i want it to be ALWAYS running

if i do java -jar myServerSide.jar & for no reason it stops after a while

i also tried deamon -- java -jar myServerSide.jar & it also stops

do you know the reason why?

what should i do,so that it stays always running, and never exit.(is it necessary to make it a service)
thanks for your help (i'm hosting my jar on linode (a VPS) if it is related)

this is the code for my server

        try
    {

        FTLogger.getInstance().logMessage(Level.FINE, "S: Connecting...");

        ServerSocket serverSocket = new ServerSocket(SERVERPORT);
        while (true)
        {
            Socket client = serverSocket.accept();
            Thread serverThread = new Thread(new ServerThread(client));
            serverThread.start();
        }

    }
    catch (Exception e)
    {
        FTLogger.getInstance().logMessage(Level.SEVERE, "S: Error getting connection", e);
    }

in my logs, i don't see any error, and when working the jar works as it should. (if you're sure that it's smthg from my code, should i open another question, and discard this?)

假设您无权访问屏幕,则可以尝试nohup java -jar myServerSide.jar> log.out&

if i do java -jar myServerSide.jar & for no reason it stops after a while

The reason it stops could be (probably is) in your code.

Debugging it should tell you why it stops.

If an java.lang.Error occurs it wouldn't be catched by

 catch (Exception e) {
 ...
 }

only

 catch( Throwable t ) {
 ...
 }

would do it.

I think that you should ensure this programatically by something like infinite loop waiting for requests from client and delegating them to separate threads for processing:

// this is very high-level and obviously a exit point from this loop should be provided
while (true) {
    Request r = waitForRequest();
    processRequestInNewThread(r);
}

Or is there something more you need that I'm missing? Maybe a sample code from your implementation of request handling will help.

You should give us some code. The first thing that pops into my mind is that you need to make sure your method that accepts the connections from clients need to run in an infinite loop. For example:

while (true) {
    acceptAndParseRequest();
}

If you launch a java application, and you embed your code into a loop:

while(true){
...
}

It will never stop, the only reason why it should stop it's because an exception is launched (do you consume resources inside the while) ? In case it really stops, try to understand what is the problem in this way:

while(true){
  try{
     ... your code ....
     }catch(Throwable t){
      system.out.println("This is my problem:" + t.printStackTrace);
     }
}

Sure it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM