Skip to content

Commit

Permalink
Merge pull request #79 from mendix/feature/global-request-settings-6
Browse files Browse the repository at this point in the history
Added action to set max concurrency and timeouts to latest version
  • Loading branch information
aleksandrpak committed Apr 21, 2016
2 parents 8ff8c22 + 1dee4dd commit d2f3f8e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 13 deletions.
Binary file added DIST/RestServices_mx6_2.1.2.mpk
Binary file not shown.
7 changes: 6 additions & 1 deletion 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 Expand Up @@ -577,4 +582,4 @@ It is also possible do share data using RestServices as the module generates RES
# HTTP Verbs in Rest

See the table at [http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services](http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services) to get an idea how the `get`, `put`, `post` and `delete` verbs should be used in combination with rest. The RestServices module respects these best practices as well.
See the table at [http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services](http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services
See the table at [http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services](http://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services
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 = "2.1.0";
public static final String VERSION = "2.1.2";

/**
* Amount of objects that are processed by the module at the same time.
Expand Down
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 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;

/**
*
*/
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
}
29 changes: 18 additions & 11 deletions javasource/restservices/consume/RestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +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.NTCredentials;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
Expand Down Expand Up @@ -78,7 +69,11 @@ 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;
Expand Down Expand Up @@ -183,6 +178,18 @@ 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 @@ -161,6 +161,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 d2f3f8e

Please sign in to comment.