Author: jochen Date: Fri May 12 14:22:12 2006 New Revision: 405878 URL: http://svn.apache.org/viewcvs?rev=405878&view=rev Log: Added the TimingOutCallback, which allows to abort a request, if the server doesn't reply within a given time.
Added: webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/java/org/apache/xmlrpc/TimingOutCallback.java webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/TimingOutCallback.java Modified: webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/changes.xml webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/site/fml/faq.fml webservices/xmlrpc/trunk/src/changes/changes.xml webservices/xmlrpc/trunk/src/site/fml/faq.fml Modified: webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/changes.xml URL: http://svn.apache.org/viewcvs/webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/changes.xml?rev=405878&r1=405877&r2=405878&view=diff ============================================================================== --- webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/changes.xml (original) +++ webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/changes.xml Fri May 12 14:22:12 2006 @@ -17,6 +17,11 @@ Made XMLWriter public and added methods for supplying a custom XMLWriter. </action> + <action dev="jochen" type="enhancement" due-to="Ken Weiner" + due-to-email="[EMAIL PROTECTED]" issue="XMLRPC-56"> + Added the TimingOutCallback, which allows to abort a request, + if the server doesn't reply within a given time. + </action> </release> <release version="2.0.1" date="28-Dec-2005"> <action dev="jochen" type="fix" issue="XMLRPC-68" Added: webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/java/org/apache/xmlrpc/TimingOutCallback.java URL: http://svn.apache.org/viewcvs/webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/java/org/apache/xmlrpc/TimingOutCallback.java?rev=405878&view=auto ============================================================================== --- webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/java/org/apache/xmlrpc/TimingOutCallback.java (added) +++ webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/java/org/apache/xmlrpc/TimingOutCallback.java Fri May 12 14:22:12 2006 @@ -0,0 +1,71 @@ +package org.apache.xmlrpc; + +import java.net.URL; + + +/** + * <p>A callback object that can wait up to a specified amount + * of time for the XML-RPC response. Suggested use is as follows: + * </p> + * <pre> + * // Wait for 10 seconds. + * TimingOutCallback callback = new TimingOutCallback(10 * 1000); + * XmlRpcClient client = new XmlRpcClient(url); + * client.executeAsync(methodName, aVector, callback); + * try { + * return callback.waitForResponse(); + * } catch (TimeoutException e) { + * System.out.println("No response from server."); + * } catch (Exception e) { + * System.out.println("Server returned an error message."); + * } + * </pre> + */ +public class TimingOutCallback implements AsyncCallback { + public static class TimeoutException extends XmlRpcException { + private static final long serialVersionUID = 4875266372372105081L; + + public TimeoutException(int code, String message) { + super(code, message); + } + } + + private final long timeout; + private Object result; + private Exception exception; + private boolean responseSeen; + + /** Waits the specified number of milliseconds for a response. + */ + public TimingOutCallback(long pTimeout) { + timeout = pTimeout; + } + + public synchronized void handleError(Exception pException, URL pUrl, String pMethod) { + responseSeen = true; + exception = pException; + notify(); + } + + public synchronized void handleResult(Object pResult, URL pUrl, String pMethod) { + responseSeen = true; + result = pResult; + notify(); + } + + /** Called to wait for the response. + * @throws InterruptedException The thread was interrupted. + * @throws TimeoutException No response was received after waiting the specified time. + * @throws Exception An error was returned by the server. + */ + public synchronized Object waitForResponse() throws Exception { + wait(timeout); + if (!responseSeen) { + throw new TimeoutException(0, "No response after waiting for " + timeout + " milliseconds."); + } + if (exception != null) { + throw exception; + } + return result; + } +} Modified: webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/site/fml/faq.fml URL: http://svn.apache.org/viewcvs/webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/site/fml/faq.fml?rev=405878&r1=405877&r2=405878&view=diff ============================================================================== --- webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/site/fml/faq.fml (original) +++ webservices/xmlrpc/branches/XMLRPC_2_0_BRANCH/src/site/fml/faq.fml Fri May 12 14:22:12 2006 @@ -1,6 +1,6 @@ <faqs title="XML-RPC Version 2 FAQ"> <part id="Customizing the request"> - <faq> + <faq id="basic_authentication"> <question>How do I do basic authentication? The method XmlRpcClient.setBasicAuthentication is deprecated?</question> <answer> @@ -26,10 +26,31 @@ ]]></source> </answer> </faq> + + <faq id="timeout_callback"> + <question>Is it possible to specify a timeout, after which the + client stops waiting for the servers response?</question> + <answer> + <p>Yes, use the class TimingOutCallback.</p> + <source><![CDATA[ + // Wait for 10 seconds. + TimingOutCallback callback = new TimingOutCallback(10 * 1000); + XmlRpcClient client = new XmlRpcClient(url); + client.executeAsync(methodName, aVector, callback); + try { + return callback.waitForResponse(); + } catch (TimeoutException e) { + System.out.println("No response from server."); + } catch (Exception e) { + System.out.println("Server returned an error message."); + } + ]]></source> + </answer> + </faq> </part> <part id="Customizing Output"> - <faq> + <faq id="string_tags"> <question>How do I get a client, which uses "string" tags?</question> <answer> <p> Added: webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/TimingOutCallback.java URL: http://svn.apache.org/viewcvs/webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/TimingOutCallback.java?rev=405878&view=auto ============================================================================== --- webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/TimingOutCallback.java (added) +++ webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/TimingOutCallback.java Fri May 12 14:22:12 2006 @@ -0,0 +1,77 @@ +package org.apache.xmlrpc.client; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.XmlRpcRequest; + + +/** + * <p>A callback object that can wait up to a specified amount + * of time for the XML-RPC response. Suggested use is as follows: + * </p> + * <pre> + * // Wait for 10 seconds. + * TimingOutCallback callback = new TimingOutCallback(10 * 1000); + * XmlRpcClient client = new XmlRpcClient(url); + * client.executeAsync(methodName, aVector, callback); + * try { + * return callback.waitForResponse(); + * } catch (TimeoutException e) { + * System.out.println("No response from server."); + * } catch (Exception e) { + * System.out.println("Server returned an error message."); + * } + * </pre> + */ +public class TimingOutCallback implements AsyncCallback { + /** This exception is thrown, if the request times out. + */ + public static class TimeoutException extends XmlRpcException { + private static final long serialVersionUID = 4875266372372105081L; + + /** Creates a new instance with the given error code and + * error message. + */ + public TimeoutException(int pCode, String message) { + super(pCode, message); + } + } + + private final long timeout; + private Object result; + private Throwable error; + private boolean responseSeen; + + /** Waits the specified number of milliseconds for a response. + */ + public TimingOutCallback(long pTimeout) { + timeout = pTimeout; + } + + /** Called to wait for the response. + * @throws InterruptedException The thread was interrupted. + * @throws TimeoutException No response was received after waiting the specified time. + * @throws Throwable An error was returned by the server. + */ + public synchronized Object waitForResponse() throws Throwable { + wait(timeout); + if (!responseSeen) { + throw new TimeoutException(0, "No response after waiting for " + timeout + " milliseconds."); + } + if (error != null) { + throw error; + } + return result; + } + + public synchronized void handleError(XmlRpcRequest pRequest, Throwable pError) { + responseSeen = true; + error = pError; + notify(); + } + + public void handleResult(XmlRpcRequest pRequest, Object pResult) { + responseSeen = true; + result = pResult; + notify(); + } +} Modified: webservices/xmlrpc/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewcvs/webservices/xmlrpc/trunk/src/changes/changes.xml?rev=405878&r1=405877&r2=405878&view=diff ============================================================================== --- webservices/xmlrpc/trunk/src/changes/changes.xml (original) +++ webservices/xmlrpc/trunk/src/changes/changes.xml Fri May 12 14:22:12 2006 @@ -53,6 +53,11 @@ due-to-email="[EMAIL PROTECTED]"> Added support for initializable handlers. </action> + <action dev="jochen" type="enhancement" due-to="Ken Weiner" + due-to-email="[EMAIL PROTECTED]" issue="XMLRPC-56"> + Added the TimingOutCallback, which allows to abort a request, + if the server doesn't reply within a given time. + </action> </release> <release version="3.0a1" date="17-Feb-2005"> <action dev="jochen" type="enhancement"> Modified: webservices/xmlrpc/trunk/src/site/fml/faq.fml URL: http://svn.apache.org/viewcvs/webservices/xmlrpc/trunk/src/site/fml/faq.fml?rev=405878&r1=405877&r2=405878&view=diff ============================================================================== --- webservices/xmlrpc/trunk/src/site/fml/faq.fml (original) +++ webservices/xmlrpc/trunk/src/site/fml/faq.fml Fri May 12 14:22:12 2006 @@ -32,6 +32,9 @@ <li>Compressing the request doesn't mean that the response will also be compressed. You need to request response compression to achieve that.</li> + <li>Additionally, be aware of the following: Compression + depends on HTTP/1.1 features. In particular, you must + not use the LiteHttpTransport.</li> </ul> </answer> </faq> @@ -49,7 +52,31 @@ </li> <li>However, requesting compression doesn't necessarily mean, that the response *is* compressed. It depends on the server.</li> + <li>Additionally, be aware of the following: Compression + depends on HTTP/1.1 features. In particular, you must + not use the LiteHttpTransport.</li> </ul> + </answer> + </faq> + + <faq id="timeout_callback"> + <question>Is it possible to specify a timeout, after which the + client stops waiting for the servers response?</question> + <answer> + <p>Yes, use the class TimingOutCallback.</p> + <source><![CDATA[ + // Wait for 10 seconds. + TimingOutCallback callback = new TimingOutCallback(10 * 1000); + XmlRpcClient client = new XmlRpcClient(url); + client.executeAsync(methodName, params, callback); + try { + return callback.waitForResponse(); + } catch (TimeoutException e) { + System.out.println("No response from server."); + } catch (Exception e) { + System.out.println("Server returned an error message."); + } + ]]></source> </answer> </faq> </part>