Skip to content

Commit

Permalink
for #41 adding more timeout management
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbiam committed May 1, 2018
1 parent 3598e97 commit 380baf2
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 19 deletions.
57 changes: 39 additions & 18 deletions src/main/java/net/sourceforge/myvd/inserts/jdbc/JdbcInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public class JdbcInsert implements Insert,JdbcPool {
boolean addBaseToFilter;


static HashMap<String,ComboPooledDataSource> poolCache = new HashMap<String,ComboPooledDataSource>();

HashMap<String,String> ldap2db,db2ldap;
private String name;
private boolean hasWhere;
Expand Down Expand Up @@ -192,25 +194,40 @@ public void configure(String name, Properties props, NameSpace nameSpace)
//this.maxIdleCons = Integer.parseInt(props.getProperty("maxIdleCons","5"));
//logger.info("maxIdleCons : " + this.maxIdleCons);


ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(driver);
} catch (PropertyVetoException e1) {
throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Could not load driver",e1);
}
cpds.setJdbcUrl(url);
cpds.setUser(user);
cpds.setPassword(pwd);
cpds.setMaxPoolSize(this.maxCons);
//cpds.setma(this.maxIdleCons);
cpds.setPreferredTestQuery(this.valQuery);
cpds.setTestConnectionOnCheckin(true);
cpds.setIdleConnectionTestPeriod(30);

String poolKey = (url+user).toLowerCase();

this.ds = cpds;
synchronized(poolCache) {
if (poolCache.get(poolKey) != null ) {
logger.info(this.name + " - using existing connection pool");
this.ds = poolCache.get(poolKey);
} else {
logger.info(this.name + " - creating connection pool");
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(driver);
} catch (PropertyVetoException e1) {
throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Could not load driver",e1);
}
cpds.setJdbcUrl(url);
cpds.setUser(user);
cpds.setPassword(pwd);
cpds.setMaxPoolSize(this.maxCons);
//cpds.setma(this.maxIdleCons);
cpds.setPreferredTestQuery(this.valQuery);
cpds.setTestConnectionOnCheckin(true);
cpds.setIdleConnectionTestPeriod(30);
//TODO need to make configurable
cpds.setUnreturnedConnectionTimeout(Integer.parseInt(props.getProperty("unreturnedConnectionTimeout","0")));
cpds.setDebugUnreturnedConnectionStackTraces(true);

cpds.setCheckoutTimeout(30000);


this.ds = cpds;
poolCache.put(poolKey, cpds);
}

}
base = nameSpace.getBase().toString();

rdn = props.getProperty("rdn");
Expand Down Expand Up @@ -985,7 +1002,11 @@ public HashMap<String, String> getLDAP2DBMap() {
}

public void shutdown() {
// TODO Auto-generated method stub
synchronized(poolCache) {
for (String poolkey : poolCache.keySet()) {
poolCache.get(poolkey).close();
}
}

}

Expand Down
99 changes: 98 additions & 1 deletion src/main/java/net/sourceforge/myvd/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package net.sourceforge.myvd.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.util.ArrayList;
Expand Down Expand Up @@ -412,7 +417,99 @@ public void startServer() throws Exception {
ldapServer.start();
((ExtendedRequestHandler) ldapServer.getExtendedRequestHandler()).init(globalChain, router);


final int shutdownPort = Integer.parseInt(props.getProperty("server.shutdown.port","-1"));
if (shutdownPort > 0) {
final String shutdownHost = props.getProperty("server.shutdown.host","127.0.0.1");
final String shutdownCommand = props.getProperty("server.shutdown.command","shutdown");

final Server server = this;

new Thread() {
public void run() {
logger.info("Starting shutdown socket listener");
try {
ServerSocket socket = new ServerSocket(shutdownPort,0,InetAddress.getByName(shutdownHost));
while (true) {
logger.info("shutdown waiting for input");
Socket clientSocket = null;
try {
clientSocket = socket.accept();
} catch (Throwable t) {
logger.warn("Could not accept connection",t);
continue;
}
logger.info("request received");
//PrintWriter out =
// new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
logger.info("reading data");
String command = in.readLine();
logger.info("'" + command + "'");
if (command != null) {
command.trim();
}
logger.info("'" + command + "'");
if (shutdownCommand.equalsIgnoreCase(command)) {
logger.info("Stopping threads");

try {
server.stopServer();
} catch (Exception e1) {
logger.warn("Could not gracefully shutdown server",e1);
}

logger.info("Closing input stream");

try {
in.close();
} catch (Throwable t) {}

/*try {
out.close();
} catch (Throwable t) {}*/

logger.info("Closing client socket");
try {
clientSocket.close();
} catch (Throwable t) {}

logger.info("Closing server socket");
try {
socket.close();
} catch (Throwable t) {}

logger.info("Sleeping for 10 seconds");
try {
Thread.sleep(10000);
logger.info("Exiting");
System.exit(0);
return;
} catch (Exception e) {}

} else {
command = null;
logger.info("invalid command");
try {
in.close();
} catch (Throwable t) {}

/*try {
out.close();
} catch (Throwable t) {}
*/
try {
clientSocket.close();
} catch (Throwable t) {}

}
}
} catch (IOException e) {
logger.error("Could not start shutdown listener",e);
}
}
}.start();
}


}
Expand Down

0 comments on commit 380baf2

Please sign in to comment.