package org.opensrf.net.http;
-import org.opensrf.*;
-import org.opensrf.util.*;
-
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
+import org.opensrf.*;
+import org.opensrf.util.*;
+
+
+/**
+ * Manages connection parameters and thread limiting for opensrf json gateway connections.
+ */
public class GatewayConnection {
- private String proto; /* http, https */
+ /** http or https */
+ private String proto;
+ /** server name */
private String host;
+ /** URL path to the gateway, defaults to defaultPath */
private String path;
+ /** Compiled URL object */
private URL url;
+ /** Number of threads currently communicating with the server */
protected int activeThreads;
+ /** Queue of pending async requests */
protected Queue<GatewayRequest> pendingThreadQueue;
+ /** maximum number of actively communicating threads allowed */
protected int maxThreads = 10;
+ /** URL path for the gateway */
public static final String defaultPath = "/osrf-gateway-v1";
public GatewayConnection(String proto, String host) {
return maxThreads;
}
+ /**
+ * Set the maximum number of actively communicating threads allowed
+ */
public void setMaxThreads(int max) {
maxThreads = max;
}
- public URL getUrl() throws java.net.MalformedURLException {
+ protected URL getUrl() throws java.net.MalformedURLException {
try {
if (url == null) {
url = new URL(proto + "://" + host + path);
return new GatewayRequest(this, service, method);
}
+ /**
+ * Launches or queues an asynchronous request.
+ *
+ * If the maximum active thread count has not been reached,
+ * start a new thread and use it to send and receive the request.
+ * The response is passed to the request's GatewayRequestHandler
+ * onComplete(). After complete, if the number of active threads
+ * is still lower than the max, one request will be pulled (if
+ * present) from the async queue and fired.
+ *
+ * If there are too many active threads, the main request is
+ * pushed onto the async queue for later processing
+ */
protected void manageAsyncRequest(final GatewayRequest request) {
if (activeThreads >= maxThreads) {
activeThreads++;
- /*
- * Send the request receive the response, fire off the next
- * thread if necessary, then pass the result to the handler
- */
+ //Send the request receive the response, fire off the next
+ //thread if necessary, then pass the result to the handler
Runnable r = new Runnable() {
public void run() {