Skip to content

Commit

Permalink
Merge pull request #78 from mendix/feature/global-request-settings
Browse files Browse the repository at this point in the history
Added action to set max concurrency and timeouts
  • Loading branch information
aleksandrpak committed Apr 21, 2016
2 parents 0ab6425 + 8c5a0ed commit e045322
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 120 deletions.
Binary file added DIST/RestServices_mx5_1.5.1.mpk
Binary file not shown.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ Generic method to make a HTTP request. `post`, `put`, `get`, `delete` are wrappe
#### resetChangeTracking
Resets any state information this app has about a remote collection that is being tracked.

#### setGlobalRequestSettings
Sets settings for all requests that are executed by RestServices module.
* `maxConcurrentRequest` - number of maximum concurrent requests executed. This will set concurrency level per host. Total concurrency level is double this setting.
* `timeout` - timeout specified in milliseconds. This will set timeout for both establishing connection and waiting between data chunks arrived from server.

#### unfollowChanges
Stops tracking a remote collection, which was being followed as result of a `followChanges` call.

Expand Down
Binary file modified RestServices.mpr
Binary file not shown.
2 changes: 1 addition & 1 deletion javasource/restservices/RestServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class RestServices {
/**
* Version of the RestServices module
*/
public static final String VERSION = "1.4.2";
public static final String VERSION = "1.5.1";

/**
* Amount of objects that are processed by the module at the same time.
Expand Down
108 changes: 54 additions & 54 deletions javasource/restservices/actions/getWithParams.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
// This file was generated by Mendix Business 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 restservices.consume.RestConsumer;
import restservices.proxies.HttpMethod;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
*
*/
public class getWithParams extends CustomJavaAction<IMendixObject>
{
private String resourceUrl;
private IMendixObject requestData;
private IMendixObject targetObject;

public getWithParams(IContext context, String resourceUrl, IMendixObject requestData, IMendixObject targetObject)
{
super(context);
this.resourceUrl = resourceUrl;
this.requestData = requestData;
this.targetObject = targetObject;
}

@Override
public IMendixObject executeAction() throws Exception
{
// BEGIN USER CODE
return RestConsumer.request(getContext(), HttpMethod.GET, resourceUrl, requestData, targetObject, false).getMendixObject();
// END USER CODE
}

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

// BEGIN EXTRA CODE
// END EXTRA CODE
}
// This file was generated by Mendix Business 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 restservices.consume.RestConsumer;
import restservices.proxies.HttpMethod;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
*
*/
public class getWithParams extends CustomJavaAction<IMendixObject>
{
private String resourceUrl;
private IMendixObject requestData;
private IMendixObject targetObject;

public getWithParams(IContext context, String resourceUrl, IMendixObject requestData, IMendixObject targetObject)
{
super(context);
this.resourceUrl = resourceUrl;
this.requestData = requestData;
this.targetObject = targetObject;
}

@Override
public IMendixObject executeAction() throws Exception
{
// BEGIN USER CODE
return RestConsumer.request(getContext(), HttpMethod.GET, resourceUrl, requestData, targetObject, false).getMendixObject();
// END USER CODE
}

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

// BEGIN EXTRA CODE
// END EXTRA CODE
}
106 changes: 53 additions & 53 deletions javasource/restservices/actions/postWithResult.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
// This file was generated by Mendix Business 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 restservices.consume.RestConsumer;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
*
*/
public class postWithResult extends CustomJavaAction<IMendixObject>
{
private String collectionUrl;
private IMendixObject dataObject;
private IMendixObject responseData;

public postWithResult(IContext context, String collectionUrl, IMendixObject dataObject, IMendixObject responseData)
{
super(context);
this.collectionUrl = collectionUrl;
this.dataObject = dataObject;
this.responseData = responseData;
}

@Override
public IMendixObject executeAction() throws Exception
{
// BEGIN USER CODE
return RestConsumer.postObject(getContext(), collectionUrl, dataObject, responseData).getMendixObject();
// END USER CODE
}

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

// BEGIN EXTRA CODE
// END EXTRA CODE
}
// This file was generated by Mendix Business 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 restservices.consume.RestConsumer;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
*
*/
public class postWithResult extends CustomJavaAction<IMendixObject>
{
private String collectionUrl;
private IMendixObject dataObject;
private IMendixObject responseData;

public postWithResult(IContext context, String collectionUrl, IMendixObject dataObject, IMendixObject responseData)
{
super(context);
this.collectionUrl = collectionUrl;
this.dataObject = dataObject;
this.responseData = responseData;
}

@Override
public IMendixObject executeAction() throws Exception
{
// BEGIN USER CODE
return RestConsumer.postObject(getContext(), collectionUrl, dataObject, responseData).getMendixObject();
// END USER CODE
}

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

// BEGIN EXTRA CODE
// END EXTRA CODE
}
51 changes: 51 additions & 0 deletions javasource/restservices/actions/setGlobalRequestSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file was generated by Mendix Business 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 restservices.consume.RestConsumer;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
*
*/
public class setGlobalRequestSettings extends CustomJavaAction<Boolean>
{
private Long maxConcurrentRequests;
private Long timeout;

public setGlobalRequestSettings(IContext context, Long maxConcurrentRequests, Long timeout)
{
super(context);
this.maxConcurrentRequests = maxConcurrentRequests;
this.timeout = timeout;
}

@Override
public Boolean executeAction() throws Exception
{
// BEGIN USER CODE
RestConsumer.setGlobalRequestSettings(maxConcurrentRequests, timeout);
return true;
// END USER CODE
}

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

// BEGIN EXTRA CODE
// END EXTRA CODE
}
32 changes: 20 additions & 12 deletions javasource/restservices/consume/RestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
Expand Down Expand Up @@ -75,8 +67,12 @@ public class RestConsumer {
private static ThreadLocal<HttpResponseData> lastConsumeError = new ThreadLocal<HttpResponseData>();

private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
static HttpClient client = new HttpClient(connectionManager);

static HttpClient client = new HttpClient(connectionManager);

static {
connectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 10);
}

public static class HttpResponseData{
private int status;
private String body = null;
Expand Down Expand Up @@ -180,7 +176,19 @@ private static Map<String, String> prepareNextHeadersMap() {
}
return headers;
}


public static synchronized void setGlobalRequestSettings(Long maxConcurrentRequests, Long timeout) {
if (timeout != null) {
client.getParams().setConnectionManagerTimeout(timeout);
client.getParams().setSoTimeout(timeout.intValue());
}

if (maxConcurrentRequests != null) {
connectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxConcurrentRequests.intValue());
connectionManager.getParams().setMaxTotalConnections(maxConcurrentRequests.intValue() * 2);
}
}

public static void addHeaderToNextRequest(String header, String value) {
prepareNextHeadersMap().put(header, value);
}
Expand Down
1 change: 1 addition & 0 deletions javasource/system/UserActionsRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public void handleEvent(Event event)
component.actionRegistry().registerUserAction(restservices.actions.resetChangeTracking.class);
component.actionRegistry().registerUserAction(restservices.actions.serializeObjectToJson.class);
component.actionRegistry().registerUserAction(restservices.actions.ServiceConsistencyCheck.class);
component.actionRegistry().registerUserAction(restservices.actions.setGlobalRequestSettings.class);
component.actionRegistry().registerUserAction(restservices.actions.setResponseCookie.class);
component.actionRegistry().registerUserAction(restservices.actions.setResponseHeader.class);
component.actionRegistry().registerUserAction(restservices.actions.setResponseStatus.class);
Expand Down
29 changes: 29 additions & 0 deletions javasource/tests/TimeoutTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tests;

import com.mendix.core.Core;
import org.junit.Assert;
import org.junit.Test;
import restservices.RestServices;
import restservices.consume.RestConsumeException;
import restservices.consume.RestConsumer;
import restservices.proxies.HttpMethod;
import restservices.publish.MicroflowService;

public class TimeoutTests extends TestBase {

@Test
public void testIdleTimeout() throws Exception {
RestConsumer.setGlobalRequestSettings(null, 1000L);
String url = RestServices.getAbsoluteUrl("ServiceWithLongOperation");

new MicroflowService("Tests.ServiceWithLongOperation", "*", HttpMethod.GET, "Service with 1 minute operation");

try {
RestConsumer.request(Core.createSystemContext(), HttpMethod.GET, url, null, null, false);
} catch (RestConsumeException rce) {
Assert.assertTrue(rce.getResponseData().getBody().startsWith("java.net.SocketTimeoutException"));
} finally {
RestConsumer.setGlobalRequestSettings(null, 0L);
}
}
}

0 comments on commit e045322

Please sign in to comment.