Example usage for org.apache.commons.httpclient DefaultMethodRetryHandler setRequestSentRetryEnabled

List of usage examples for org.apache.commons.httpclient DefaultMethodRetryHandler setRequestSentRetryEnabled

Introduction

In this page you can find the example usage for org.apache.commons.httpclient DefaultMethodRetryHandler setRequestSentRetryEnabled.

Prototype

public void setRequestSentRetryEnabled(boolean paramBoolean) 

Source Link

Usage

From source file:net.mumie.cocoon.msg.AbstractPostMessage.java

/**
 * Sends this message./*from w  ww. j  a v a 2 s.co  m*/
 */

public boolean send() {
    final String METHOD_NAME = "send";
    this.logDebug(METHOD_NAME + " 1/3: Started");
    boolean success = true;

    MessageDestinationTable destinationTable = null;
    SimpleHttpClient httpClient = null;

    try {
        // Init services:
        destinationTable = (MessageDestinationTable) this.serviceManager.lookup(MessageDestinationTable.ROLE);
        httpClient = (SimpleHttpClient) this.serviceManager.lookup(SimpleHttpClient.ROLE);

        // Get destination:
        MessageDestination destination = destinationTable.getDestination(this.destinationName);

        // Create and setup a Http Post method object:
        PostMethod method = new PostMethod(destination.getURL());
        DefaultMethodRetryHandler retryHandler = new DefaultMethodRetryHandler();
        retryHandler.setRequestSentRetryEnabled(false);
        retryHandler.setRetryCount(3);
        method.setMethodRetryHandler(retryHandler);

        // Set login name and password:
        method.addParameter("name", destination.getLoginName());
        method.addParameter("password", destination.getPassword());

        // Specific setup:
        this.init(method, destination);

        // Execute method:
        try {
            if (httpClient.executeMethod(method) != HttpStatus.SC_OK) {
                this.logError(METHOD_NAME + ": Failed to send message: " + method.getStatusLine());
                success = false;
            }
            String response = new String(method.getResponseBody());
            if (response.trim().startsWith("ERROR")) {
                this.logError(METHOD_NAME + ": Failed to send message: " + response);
                success = false;
            }
            this.logDebug(METHOD_NAME + " 2/3: response = " + response);
        } finally {
            method.releaseConnection();
        }
    } catch (Exception exception) {
        this.logError(METHOD_NAME, exception);
        success = false;
    } finally {
        if (httpClient != null)
            this.serviceManager.release(httpClient);
        if (destinationTable != null)
            this.serviceManager.release(destinationTable);
    }

    this.logDebug(METHOD_NAME + " 3/3: Done. success = " + success);
    return success;
}