Reputation: 1458
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
Upvotes: 1
Views: 4152
Reputation: 12575
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.
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(); } } });
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(); }
Upvotes: 0
Reputation: 1260
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();
}
}
Upvotes: 1
Reputation: 3486
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.
Upvotes: 0
Reputation: 240908
try {
// Execute a command to terminate your server
String command = "command to stop your server";
Process child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}
Upvotes: 0