Java 在后台杀死Derby DB网络服务器
我正在寻找一种在网络模式下运行ApacheDerby服务器的好方法。我正在使用NetworkServerControl启动服务器,它工作得很好 我按如下方式启动服务器:Java 在后台杀死Derby DB网络服务器,java,derby,javadb,Java,Derby,Javadb,我正在寻找一种在网络模式下运行ApacheDerby服务器的好方法。我正在使用NetworkServerControl启动服务器,它工作得很好 我按如下方式启动服务器: /** * starts the network server * @return true if sucessfull */ public boolean start() { try { // just to be sure that we don't start two servers
/**
* starts the network server
* @return true if sucessfull
*/
public boolean start()
{
try
{
// just to be sure that we don't start two servers
this.stop();
server = new NetworkServerControl();
server.start( null );
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
/**
* stops the server
*
* @return true if there were no problems stopping the server
*/
public boolean stop()
{
try
{
if ( server == null )
{
server = new NetworkServerControl();
}
server.shutdown();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
/**
* Try to test for a connection
*
* @return true if the server is alive
*/
public boolean testForConnection()
{
try
{
server.ping();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
就这样停下来:
/**
* starts the network server
* @return true if sucessfull
*/
public boolean start()
{
try
{
// just to be sure that we don't start two servers
this.stop();
server = new NetworkServerControl();
server.start( null );
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
/**
* stops the server
*
* @return true if there were no problems stopping the server
*/
public boolean stop()
{
try
{
if ( server == null )
{
server = new NetworkServerControl();
}
server.shutdown();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
/**
* Try to test for a connection
*
* @return true if the server is alive
*/
public boolean testForConnection()
{
try
{
server.ping();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
在main()上,我有这样一个命令,以便在服务器运行时进程不会死亡
(...)
clsDB.start();
while( clsDB.testForConnection() )
{
Thread.sleep( 60000 );
}
testForConnection()如下所示:
/**
* starts the network server
* @return true if sucessfull
*/
public boolean start()
{
try
{
// just to be sure that we don't start two servers
this.stop();
server = new NetworkServerControl();
server.start( null );
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
/**
* stops the server
*
* @return true if there were no problems stopping the server
*/
public boolean stop()
{
try
{
if ( server == null )
{
server = new NetworkServerControl();
}
server.shutdown();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
/**
* Try to test for a connection
*
* @return true if the server is alive
*/
public boolean testForConnection()
{
try
{
server.ping();
return true;
}
catch ( Exception ex )
{
this.logLogger.debug( ex );
return false;
}
}
我的问题是,当我的JAR的一个新instance被调用时,旧的仍然会运行(除非我真的很幸运,并且测试是在新服务器启动之前进行的)
我知道我可以测试服务器是否已经运行,然后我就不会再启动了,但是如果服务器已经存在,我想让start像重启一样工作。我不久前用derby做了一个项目(包括在网络模式下运行),我似乎记得有一些SQL可以在服务器上执行,从而关闭derby 所以,假设是这样的话(对不起,没有时间去谷歌了),你可以在启动时寻找你的网络实例。如果存在,请在其上运行此SQL。一旦您确定它最终被关闭(我想轮询它是否存在),您就可以再次启动它 编辑:哦,这不是SQL,它是以“shutdown=true”作为参数连接到数据库的。见:
我提出的另一个解决方案不与您的JAR交互,您可能不喜欢 另一种方法是让启动derby实例的JAR打开一个TCP端口,它在启动derby时监视该端口。然后,您可以从新的JAR实例(显然是在打开您自己的TCP端口之前)向它发送您自己的关闭命令 derby JAR实例的启动将是:
- 查看商定的TCP端口是否已打开
- 如果是,发送关机命令。
- 然后,向其发送命令的JAR将使用
方法关闭derbystop()
- 一旦关闭derby,它将发送一个
返回并关闭连接success
- 然后,向其发送命令的JAR将使用
- 我们在商定的TCP端口上侦听
- 我们调用
并启动Derbystart()
与我的其他解决方案相比,这需要做更多的工作,但从理论上讲,这是一个更好的解决方案,因为作为重启的一部分,您的JAR可能还需要做其他事情(清理/重启其他资源,记录它正在关闭的事实,诸如此类)。我们最终使用了一个在关闭服务器时创建的文件,然后,在服务器进程中,我们定期检查文件是否存在,如果存在,我们将关闭服务器。是的,我也考虑过这一点,但当时我们认为这会导致杀伤力过大和安全问题。谢谢