-
Notifications
You must be signed in to change notification settings - Fork 140
[Doc] Using QBit microservice lib's HttpClient GET, POST, et al, JSON, Java 8 Lambda
QBit ships with an HttpClient lib. The libs tries to make it easy to make REST style calls. The HttpClient lib is geared towards HTTP and JSON.
The nice thing about the lib is that is allows for Java 8 style lambdas for GET, POST and WebSocket calls.
Before we start up our HTTP client let's start up a server to echo back what we send the server.
/* Create an HTTP server. */
HttpServer httpServer = httpServerBuilder()
.setPort(8080).build();
/* Setting up a request Consumer with Java 8 Lambda expression. */
httpServer.setHttpRequestConsumer(httpRequest -> {
Map<String, Object> results = new HashMap<>();
results.put("method", httpRequest.getMethod());
results.put("uri", httpRequest.getUri());
results.put("body", httpRequest.getBodyAsString());
results.put("headers", httpRequest.getHeaders());
results.put("params", httpRequest.getParams());
httpRequest.getReceiver()
.response(200, "application/json", Boon.toJson(results));
});
/* Start the server. */
httpServer.start();
Now, let's try out our HTTP client.
/* Setup an httpClient. */
HttpClient httpClient = httpClientBuilder()
.setHost("localhost").setPort(8080).build();
httpClient.start();
You just pass the URL, the port and then call start.
Now you can start sending HTTP requests.
/* Send no param get. */
HttpResponse httpResponse = httpClient.get( "/hello/mom" );
puts( httpResponse );
An HTTP response just contains the results from the server.
public interface HttpResponse {
MultiMap<String, String> headers();
int code();
String contentType();
String body();
}
There are helper methods for sync HTTP GET calls.
/* Send one param get. */
httpResponse = httpClient.getWith1Param("/hello/singleParam",
"hi", "mom");
puts("single param", httpResponse );
/* Send two param get. */
httpResponse = httpClient.getWith2Params("/hello/twoParams",
"hi", "mom", "hello", "dad");
puts("two params", httpResponse );
/* Send two param get. */
httpResponse = httpClient.getWith3Params("/hello/3params",
"hi", "mom",
"hello", "dad",
"greetings", "kids");
puts("three params", httpResponse );
/* Send four param get. */
httpResponse = httpClient.getWith4Params("/hello/4params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets");
puts("4 params", httpResponse );
/* Send five param get. */
httpResponse = httpClient.getWith5Params("/hello/5params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets",
"hola", "neighbors");
puts("5 params", httpResponse );
The puts method is a helper method it does System.out.println more or less by the way.
The first five params are covered. Beyond five, you have to use the HttpBuilder.
/* Send six params with get. */
final HttpRequest httpRequest = httpRequestBuilder()
.addParam("hi", "mom")
.addParam("hello", "dad")
.addParam("greetings", "kids")
.addParam("yo", "pets")
.addParam("hola", "pets")
.addParam("salutations", "all").build();
httpResponse = httpClient.sendRequestAndWait(httpRequest);
puts("6 params", httpResponse );
There are async calls for GET as well.
/* Using Async support with lambda. */
httpClient.getAsync("/hi/async", (code, contentType, body) -> {
puts("Async text with lambda", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.getAsyncWith1Param("/hi/async", "hi", "mom", (code, contentType, body) -> {
puts("Async text with lambda 1 param\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.getAsyncWith2Params("/hi/async",
"p1", "v1",
"p2", "v2",
(code, contentType, body) -> {
puts("Async text with lambda 2 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.getAsyncWith3Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
(code, contentType, body) -> {
puts("Async text with lambda 3 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.getAsyncWith4Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
"p4", "v4",
(code, contentType, body) -> {
puts("Async text with lambda 4 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.getAsyncWith5Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
"p4", "v4",
"p5", "v5",
(code, contentType, body) -> {
puts("Async text with lambda 5 params\n", body);
});
Sys.sleep(100);
There are POST HTTP sync helper methods as well.
/* Send no param post. */
HttpResponse httpResponse = httpClient.post( "/hello/mom" );
puts( httpResponse );
/* Send one param post. */
httpResponse = httpClient.postWith1Param("/hello/singleParam",
"hi", "mom");
puts("single param", httpResponse );
/* Send two param post. */
httpResponse = httpClient.postWith2Params("/hello/twoParams",
"hi", "mom", "hello", "dad");
puts("two params", httpResponse );
/* Send two param post. */
httpResponse = httpClient.postWith3Params("/hello/3params",
"hi", "mom",
"hello", "dad",
"greetings", "kids");
puts("three params", httpResponse );
/* Send four param post. */
httpResponse = httpClient.postWith4Params("/hello/4params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets");
puts("4 params", httpResponse );
/* Send five param post. */
httpResponse = httpClient.postWith5Params("/hello/5params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets",
"hola", "neighbors");
puts("5 params", httpResponse );
/* Send six params with post. */
final HttpRequest httpRequest = httpRequestBuilder()
.setUri("/sixPost")
.setMethod("POST")
.addParam("hi", "mom")
.addParam("hello", "dad")
.addParam("greetings", "kids")
.addParam("yo", "pets")
.addParam("hola", "pets")
.addParam("salutations", "all").build();
httpResponse = httpClient.sendRequestAndWait(httpRequest);
puts("6 params", httpResponse );
/* Using Async support with lambda. */
httpClient.postAsync("/hi/async",
(code, contentType, body) -> {
puts("Async text with lambda", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.postFormAsyncWith1Param("/hi/async", "hi", "mom",
(code, contentType, body) -> {
puts("Async text with lambda 1 param\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.postFormAsyncWith2Params("/hi/async",
"p1", "v1",
"p2", "v2",
(code, contentType, body) -> {
puts("Async text with lambda 2 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.postFormAsyncWith3Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
(code, contentType, body) -> {
puts("Async text with lambda 3 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.postFormAsyncWith4Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
"p4", "v4",
(code, contentType, body) -> {
puts("Async text with lambda 4 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.postFormAsyncWith5Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
"p4", "v4",
"p5", "v5",
(code, contentType, body) -> {
puts("Async text with lambda 5 params\n", body);
});
Sys.sleep(100);
Remember if give params is not enough, then you can use the HttpBuilder.
httpRequest = httpRequestBuilder().addParam("hi", "mom")
.addParam("hello", "dad")
.addParam("greetings", "kids")
.addParam("yo", "pets")
.addParam("hola", "pets")
.addParam("salutations", "all")
.setTextReceiver((code, contentType, body)
-> puts(code, contentType, body))
.build();
httpClient.sendHttpRequest(httpRequest);
puts("6 async params", httpResponse );
/* Send no param post. */
HttpResponse httpResponse = httpClient.put( "/hello/mom" );
puts( httpResponse );
/* Send one param post. */
httpResponse = httpClient.putWith1Param("/hello/singleParam",
"hi", "mom");
puts("single param", httpResponse );
/* Send two param post. */
httpResponse = httpClient.putWith2Params("/hello/twoParams",
"hi", "mom", "hello", "dad");
puts("two params", httpResponse );
/* Send two param post. */
httpResponse = httpClient.putWith3Params("/hello/3params",
"hi", "mom",
"hello", "dad",
"greetings", "kids");
puts("three params", httpResponse );
/* Send four param post. */
httpResponse = httpClient.putWith4Params("/hello/4params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets");
puts("4 params", httpResponse );
/* Send five param post. */
httpResponse = httpClient.putWith5Params("/hello/5params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets",
"hola", "neighbors");
puts("5 params", httpResponse );
/* Send six params with post. */
final HttpRequest httpRequest = httpRequestBuilder()
.setUri("/sixPost")
.setMethod("PUT")
.addParam("hi", "mom")
.addParam("hello", "dad")
.addParam("greetings", "kids")
.addParam("yo", "pets")
.addParam("hola", "pets")
.addParam("salutations", "all").build();
httpResponse = httpClient.sendRequestAndWait(httpRequest);
puts("6 params", httpResponse );
/* Using Async support with lambda. */
httpClient.putAsync("/hi/async", (code, contentType, body) -> {
puts("Async text with lambda", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.putFormAsyncWith1Param("/hi/async", "hi", "mom",
(code, contentType, body) -> {
puts("Async text with lambda 1 param\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.putFormAsyncWith2Params("/hi/async",
"p1", "v1",
"p2", "v2",
(code, contentType, body) -> {
puts("Async text with lambda 2 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.putFormAsyncWith3Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
(code, contentType, body) -> {
puts("Async text with lambda 3 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.putFormAsyncWith4Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
"p4", "v4",
(code, contentType, body) -> {
puts("Async text with lambda 4 params\n", body);
});
Sys.sleep(100);
/* Using Async support with lambda. */
httpClient.putFormAsyncWith5Params("/hi/async",
"p1", "v1",
"p2", "v2",
"p3", "v3",
"p4", "v4",
"p5", "v5",
(code, contentType, body) -> {
puts("Async text with lambda 5 params\n", body);
});
Sys.sleep(100);
httpClient.sendJsonPost("/foo/json/",
toJson(new Employee("Rick", "Smith")));
httpClient.sendJsonPostAsync("/foo/json/",
toJson(new Employee("Rick", "Smith")),
(code, contentType, body) ->
puts ("ASYNC POST", code, contentType, fromJson(body)));
httpClient.sendJsonPostAsync("/foo/json/", toJson(new Employee("Rick", "Smith")),
new HttpTextReceiver() {
@Override
public void response(int code, String contentType, String body) {
puts(code, contentType, body);
}
});
HttpResponse httpResponse = httpClient.postJson("/foo/json/sync",
toJson(new Employee("Rick", "Smith")));
puts("POST JSON RESPONSE", httpResponse);
httpClient.sendJsonPut("/foo/json/",
toJson(new Employee("Rick", "Smith")));
httpClient.sendJsonPutAsync("/foo/json/",
toJson(new Employee("Rick", "Smith")),
(code, contentType, body)
-> puts("ASYNC PUT", code, contentType, fromJson(body)));
httpResponse = httpClient.putJson("/foo/json/sync",
toJson(new Employee("Rick", "Smith")));
puts("PUT JSON RESPONSE", httpResponse);
QBit Website What is Microservices Architecture?
QBit Java Micorservices lib tutorials
The Java microservice lib. QBit is a reactive programming lib for building microservices - JSON, HTTP, WebSocket, and REST. QBit uses reactive programming to build elastic REST, and WebSockets based cloud friendly, web services. SOA evolved for mobile and cloud. ServiceDiscovery, Health, reactive StatService, events, Java idiomatic reactive programming for Microservices.
Reactive Programming, Java Microservices, Rick Hightower
Java Microservices Architecture
[Microservice Service Discovery with Consul] (http://www.mammatustech.com/Microservice-Service-Discovery-with-Consul)
Microservices Service Discovery Tutorial with Consul
[Reactive Microservices] (http://www.mammatustech.com/reactive-microservices)
[High Speed Microservices] (http://www.mammatustech.com/high-speed-microservices)
Reactive Microservices Tutorial, using the Reactor
QBit is mentioned in the Restlet blog
All code is written using JetBrains Idea - the best IDE ever!
Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting
Tutorials
- QBit tutorials
- Microservices Intro
- Microservice KPI Monitoring
- Microservice Batteries Included
- RESTful APIs
- QBit and Reakt Promises
- Resourceful REST
- Microservices Reactor
- Working with JSON maps and lists
__
Docs
Getting Started
- First REST Microservice
- REST Microservice Part 2
- ServiceQueue
- ServiceBundle
- ServiceEndpointServer
- REST with URI Params
- Simple Single Page App
Basics
- What is QBit?
- Detailed Overview of QBit
- High level overview
- Low-level HTTP and WebSocket
- Low level WebSocket
- HttpClient
- HTTP Request filter
- HTTP Proxy
- Queues and flushing
- Local Proxies
- ServiceQueue remote and local
- ManagedServiceBuilder, consul, StatsD, Swagger support
- Working with Service Pools
- Callback Builders
- Error Handling
- Health System
- Stats System
- Reactor callback coordination
- Early Service Examples
Concepts
REST
Callbacks and Reactor
Event Bus
Advanced
Integration
- Using QBit in Vert.x
- Reactor-Integrating with Cassandra
- Using QBit with Spring Boot
- SolrJ and service pools
- Swagger support
- MDC Support
- Reactive Streams
- Mesos, Docker, Heroku
- DNS SRV
QBit case studies
QBit 2 Roadmap
-- Related Projects
- QBit Reactive Microservices
- Reakt Reactive Java
- Reakt Guava Bridge
- QBit Extensions
- Reactive Microservices
Kafka training, Kafka consulting, Cassandra training, Cassandra consulting, Spark training, Spark consulting