开发者

Stopping Running Server with Java Code?

I want to stop a server running on port 8080. In my java application, whenever application is closed, also this server needs to be stopped. But I could not find any solution except stopping the server manually. Is there any way to stop a server with开发者_如何学运维 codes ? By the way, I am using Windows 7


How are you starting SymmetricDs? As a windows service, as a WAR or embedded in you application?

Looking at the user guide it seems that if you could embed it in your code you ought to be able to start and stop it directly. More details in the user guide along with the following example code.

import org.jumpmind.symmetric.SymmetricWebServer;

public class StartSymmetricEngine {

    /**
     * Start an engine that is configured by two properties files. One is
     * packaged with the application and contains overridden properties that are
     * specific to the application. The other is found in the application's
     * working directory. It can be used to setup environment specific
     * properties.
     */
    public static void main(String[] args) throws Exception {

        SymmetricWebServer node = new SymmetricWebServer(
                                   "classpath://my-application.properties");

        // this will create the database, sync triggers, start jobs running
        node.start(8080);

        // this will stop the node
        node.stop();
    }   

}


try {
    // Execute a command to terminate your server
    String command = "command to stop your server";
    Process child = Runtime.getRuntime().exec(command);

} catch (IOException e) {
}


Why don't you check Cargo? It provides a Java API to start/stop Java containers. You can find the list of supported containers in the home page.


Since i did not see the code , how are creating server and accepting connection ,below i have given you the following ooptions.you should try to implement the firstway , rest of the option wont guarantee whether correct process will be killed.

  1.  public void stopServer()
    {
      threadReference.interrupt();
    }
    
    while(!Thread.interrupted())
    {
    
       //   Accept Server Connection
       try 
       {
        Thread.sleep(1000);
       } 
       catch(InterruptedException ex)  
       { 
         Thread.currentThread().interrupt(); 
       }
    }
    
    Runtime.getRunTime().addShutDownHook(new Thread()
    {
      public void run()
      {
        try 
        {  
          ref.stop();
        }  catch (IOException e)  
        { 
          // close server socket
          // other clean up 
          e.printStackTrace();  
        }  
      }
    });
    
  2. you need to specify this in Runtime.getRuntime() TASKKILL /F /IM or there is a jps which is better to kill relevant process.

    try
    

    {

    Process p = Runtime.getRuntime().exec("TASKKILL /F /IM  communicator*"); 
    BufferedReader in = 
    new BufferedReader(new  InputStreamReader(p.getInputStream()));
    
    String result = null;  
    while ((result= in.readLine()) != null) {  
     if ( "SUCCESS".equals(result.substring(0,7))
     {
      break;
     }
    }  
} catch (IOException e) 
{  
   e.printStackTrace();  
}  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜