Skip to content

Commit

Permalink
Merge pull request #102 from mendix/features/idle-connection-monitor
Browse files Browse the repository at this point in the history
Added a idle connection monitor that regularly closes idle connections
  • Loading branch information
fazzdev authored Nov 7, 2018
2 parents fbda8d0 + 7f3b833 commit 7a008f3
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
Binary file modified RestServices.mpr
Binary file not shown.
51 changes: 51 additions & 0 deletions javasource/restservices/actions/startIdleConnectionMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file was generated by Mendix Modeler.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package restservices.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

import restservices.consume.RestConsumer;

/**
* Regularly checks for connections connections that have been idle for at least the given amount of time, and closes them.
*/
public class startIdleConnectionMonitor extends CustomJavaAction<java.lang.Boolean>
{
private java.lang.Long interval;
private java.lang.Long maxIdleTime;

public startIdleConnectionMonitor(IContext context, java.lang.Long interval, java.lang.Long maxIdleTime)
{
super(context);
this.interval = interval;
this.maxIdleTime = maxIdleTime;
}

@Override
public java.lang.Boolean executeAction() throws Exception
{
// BEGIN USER CODE
return RestConsumer.startIdleConnectionMonitor(interval, maxIdleTime);
// END USER CODE
}

/**
* Returns a string representation of this action
*/
@Override
public java.lang.String toString()
{
return "startIdleConnectionMonitor";
}

// BEGIN EXTRA CODE
// END EXTRA CODE
}
39 changes: 39 additions & 0 deletions javasource/restservices/consume/IdleConnectionMonitorThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package restservices.consume;

import org.apache.commons.httpclient.HttpConnectionManager;

public class IdleConnectionMonitorThread extends Thread {

private final HttpConnectionManager connMgr;
private long intervalMillis;
private long maxIdleTimeMillis;
private volatile boolean shutdown;

public IdleConnectionMonitorThread(HttpConnectionManager connMgr, long intervalMillis, long maxIdleTimeMillis) {
super();
this.connMgr = connMgr;
this.intervalMillis = intervalMillis;
this.maxIdleTimeMillis = maxIdleTimeMillis;
}

@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(intervalMillis);
connMgr.closeIdleConnections(maxIdleTimeMillis);
}
}
} catch (InterruptedException ex) {
// terminate
}
}

public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}
14 changes: 13 additions & 1 deletion javasource/restservices/consume/RestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@
public class RestConsumer {
private static ThreadLocal<HttpResponseData> lastConsumeError = new ThreadLocal<HttpResponseData>();

private static IdleConnectionMonitorThread idleConnectionMonitorThread = null;
private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
static HttpClient client = new HttpClient(connectionManager);

static {
connectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 10);
}
Expand Down Expand Up @@ -740,4 +741,15 @@ public static RequestResult getLastConsumeError(IContext context) {
lastConsumeError.set(null);
return res.asRequestResult(context);
}

public static boolean startIdleConnectionMonitor(long interval, long maxIdleTime)
{
if (idleConnectionMonitorThread != null)
return false;

idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, interval, maxIdleTime);
idleConnectionMonitorThread.start();

return true;
}
}
1 change: 1 addition & 0 deletions javasource/system/UserActionsRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public void registerActions(IActionRegistrator registrator)
registrator.registerUserAction(restservices.actions.setResponseHeader.class);
registrator.registerUserAction(restservices.actions.setResponseStatus.class);
registrator.registerUserAction(restservices.actions.setRestBasePath.class);
registrator.registerUserAction(restservices.actions.startIdleConnectionMonitor.class);
registrator.registerUserAction(restservices.actions.StartMicroflowServiceJava.class);
registrator.registerUserAction(restservices.actions.StartPublishesServicesJava.class);
registrator.registerUserAction(restservices.actions.throwRestServiceException.class);
Expand Down
Binary file added userlib/commons-codec-1.11.jar
Binary file not shown.
Empty file.

0 comments on commit 7a008f3

Please sign in to comment.