Skip to content

Commit

Permalink
Added examples for pipeline and multiplexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed Mar 31, 2024
1 parent 8c763da commit ecce9bf
Show file tree
Hide file tree
Showing 7 changed files with 710 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.refactoring.http5.client.example.async.helper;

import io.refactoring.http5.client.example.RequestProcessingException;
import java.util.concurrent.CountDownLatch;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.message.StatusLine;

/** The pipelined http response callback. */
@Slf4j
public class PipelinedHttpResponseCallback implements FutureCallback<SimpleHttpResponse> {
/** The Http get request. */
private final SimpleHttpRequest httpRequest;

/** The Error message. */
private final String errorMessage;

/** The Latch. */
private final CountDownLatch latch;

/**
* Instantiates a new pipelined http response callback.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param latch the latch
*/
public PipelinedHttpResponseCallback(
SimpleHttpRequest httpRequest, String errorMessage, CountDownLatch latch) {
this.httpRequest = httpRequest;
this.errorMessage = errorMessage;
this.latch = latch;
}

@Override
public void completed(final SimpleHttpResponse response) {
latch.countDown();
log.debug(httpRequest + "->" + new StatusLine(response));
log.debug("Got response: {}", response.getBody());
}

@Override
public void failed(final Exception ex) {
latch.countDown();
log.error(httpRequest + "->" + ex);
throw new RequestProcessingException(errorMessage, ex);
}

@Override
public void cancelled() {
latch.countDown();
log.debug(httpRequest + " cancelled");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.refactoring.http5.client.example.async.helper;

import io.refactoring.http5.client.example.RequestProcessingException;
import java.io.IOException;
import java.nio.CharBuffer;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.message.StatusLine;

/** The Simple http character stream consumer. */
@Slf4j
public class SimpleCharResponseConsumer extends AbstractCharResponseConsumer<SimpleHttpResponse> {
/** The Http get request. */
private final SimpleHttpRequest httpRequest;

private final StringBuilder responseBuilder = new StringBuilder();

/** The Error message. */
private final String errorMessage;

/**
* Instantiates a new Simple http response callback.
*
* @param httpRequest the http request
* @param errorMessage the error message
*/
public SimpleCharResponseConsumer(SimpleHttpRequest httpRequest, String errorMessage) {
this.httpRequest = httpRequest;
this.errorMessage = errorMessage;
}

@Override
protected void start(HttpResponse httpResponse, ContentType contentType)
throws HttpException, IOException {
log.debug(httpRequest + "->" + new StatusLine(httpResponse));
responseBuilder.setLength(0);
}

@Override
protected SimpleHttpResponse buildResult() throws IOException {
return SimpleHttpResponse.create(HttpStatus.SC_OK, responseBuilder.toString());
}

@Override
protected int capacityIncrement() {
return 0;
}

@Override
protected void data(CharBuffer src, boolean endOfStream) throws IOException {
while (src.hasRemaining()) {
responseBuilder.append(src.get());
}
if (endOfStream) {
log.debug(responseBuilder.toString());
}
}

@Override
public void failed(Exception ex) {
log.error(httpRequest + "->" + ex);
throw new RequestProcessingException(errorMessage, ex);
}

@Override
public void releaseResources() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.refactoring.http5.client.example.async.helper;

import io.refactoring.http5.client.example.RequestProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.message.StatusLine;

import java.util.HashMap;
import java.util.Map;

/** The Simple http response callback. */
@Slf4j
public class SimpleHttpResponseCallback implements FutureCallback<SimpleHttpResponse> {
/** The Http get request. */
private final SimpleHttpRequest httpRequest;

/** The Error message. */
private final String errorMessage;

/**
* Instantiates a new Simple http response callback.
*
* @param httpRequest the http request
* @param errorMessage the error message
*/
public SimpleHttpResponseCallback(SimpleHttpRequest httpRequest, String errorMessage) {
this.httpRequest = httpRequest;
this.errorMessage = errorMessage;
}

@Override
public void completed(final SimpleHttpResponse response) {
log.debug(httpRequest + "->" + new StatusLine(response));
log.debug("Got response: {}", response.getBody());
}

@Override
public void failed(final Exception ex) {
log.error(httpRequest + "->" + ex);
throw new RequestProcessingException(errorMessage, ex);
}

@Override
public void cancelled() {
log.debug(httpRequest + " cancelled");
}
}
Loading

0 comments on commit ecce9bf

Please sign in to comment.