-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples for pipeline and multiplexing.
- Loading branch information
1 parent
8c763da
commit ecce9bf
Showing
7 changed files
with
710 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
.../java/io/refactoring/http5/client/example/async/helper/PipelinedHttpResponseCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...ain/java/io/refactoring/http5/client/example/async/helper/SimpleCharResponseConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ain/java/io/refactoring/http5/client/example/async/helper/SimpleHttpResponseCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.