2011-02-15 74 views
1

我想停止运行在端口8080上的服务器。在我的java应用程序中,每当应用程序关闭时,此服务器也需要停止。但我找不到任何解决方案,除了手动停止服务器。有没有办法阻止代码服务器?通过下面 我已经给你的方式,我使用的是Windows 7使用Java代码停止运行服务器?

+0

是否例如一个Apache服务器,还是你自己开发的服务器?如果这样的话,这种方式将会完全不同。 – 2011-02-15 10:16:01

+0

它是一个独立的Java应用程序或基于Web? – 2011-02-15 10:18:10

+0

你可以在这里发布你的代码吗? – 2011-02-15 10:24:38

回答

1

你是如何启动SymmetricDs的?作为一个Windows服务,作为一个WAR还是嵌入在你的应用程序中?

查看用户指南,似乎如果您可以将它嵌入代码中,您应该能够直接启动和停止它。 user guide中的更多详细信息以及以下示例代码。

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(); 
    } 

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

} catch (IOException e) { 
} 
0

,因为我没有看到代码,如何创建服务器和接受连接,下面ooptions.you应尽量落实firstway, 其余的选项不能保证是否会导致正确的进程被终止。

  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. 你需要调用Runtime.getRuntime()TASKKILL/F/IM或者是有JPS哪个更好杀死相关进程注明。

    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

你为什么不检查Cargo?它提供了一个Java API来启动/停止Java容器。您可以在主页中找到支持的容器列表。