-
Notifications
You must be signed in to change notification settings - Fork 140
[Doc] Using QBit microservice lib's WebSocket support
QBit microservice lib comes with a WebSocket lib that is geared towards JSON.
It is very easy to use.
/* Create an HTTP server. */
HttpServer httpServer = httpServerBuilder()
.setPort(8080).build();
/* Setup WebSocket Server support. */
httpServer.setWebSocketOnOpenConsumer(webSocket -> {
webSocket.setTextMessageConsumer(message -> {
webSocket.sendText("ECHO " + message);
});
});
/* Start the server. */
httpServer.start();
/** CLIENT. */
/* Setup an httpClient. */
HttpClient httpClient = httpClientBuilder()
.setHost("localhost").setPort(8080).build();
httpClient.start();
/* Setup the client websocket. */
WebSocket webSocket = httpClient
.createWebSocket("/websocket/rocket");
/* Setup the text consumer. */
webSocket.setTextMessageConsumer(message -> {
System.out.println(message);
});
webSocket.openAndWait();
/* Send some messages. */
webSocket.sendText("Hi mom");
webSocket.sendText("Hello World!");
ECHO Hi mom
ECHO Hello World!
Now stop the server and client. Pretty easy eh?
Sys.sleep(1000);
webSocket.close();
httpClient.stop();
httpServer.stop();
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.http.websocket.WebSocket;
import org.boon.core.Sys;
import static io.advantageous.qbit.http.client.HttpClientBuilder.httpClientBuilder;
import static io.advantageous.qbit.http.server.HttpServerBuilder.httpServerBuilder;
public class EchoWebSocket {
public static void main(String... args) {
/* Create an HTTP server. */
HttpServer httpServer = httpServerBuilder()
.setPort(8080).build();
/* Setup WebSocket Server support. */
httpServer.setWebSocketOnOpenConsumer(webSocket -> {
webSocket.setTextMessageConsumer(message -> {
webSocket.sendText("ECHO " + message);
});
});
/* Start the server. */
httpServer.start();
/** CLIENT. */
/* Setup an httpClient. */
HttpClient httpClient = httpClientBuilder()
.setHost("localhost").setPort(8080).build();
httpClient.start();
/* Setup the client websocket. */
WebSocket webSocket = httpClient
.createWebSocket("/websocket/rocket");
webSocket.setTextMessageConsumer(message -> {
System.out.println(message);
});
webSocket.openAndWait();
/* Send some messages. */
webSocket.sendText("Hi mom");
webSocket.sendText("Hello World!");
Sys.sleep(1000);
webSocket.close();
httpClient.stop();
httpServer.stop();
}
}
First lets show the server side setup.
/* Create an HTTP server. */
HttpServer httpServer = httpServerBuilder()
.setPort(8080).build();
/* Setup WebSocket Server support. */
httpServer.setWebSocketOnOpenConsumer(webSocket -> {
/** Set up onMessage. */
webSocket.setTextMessageConsumer(message -> {
webSocket.sendText("ECHO " + message);
});
/** Set up onClose. */
webSocket.setCloseConsumer(obj -> {
puts("SERVER CLOSE ");
});
/** Set up onError. */
webSocket.setErrorConsumer(error -> {
puts("SERVER ERROR", error);
});
});
Now here is the client side setup.
/* Setup an httpClient. */
HttpClient httpClient = httpClientBuilder()
.setHost("localhost").setPort(8080).build();
httpClient.start();
/* Setup the client websocket. */
WebSocket webSocket = httpClient
.createWebSocket("/websocket/rocket");
webSocket.setTextMessageConsumer(message -> {
System.out.println("CLIENT ON MESSAGE \n" + message);
});
/** Set up onClose. */
webSocket.setCloseConsumer(obj -> {
puts("CLIENT CLOSE");
});
/** Set up onError. */
webSocket.setErrorConsumer(error -> {
puts("CLIENT ERROR", error);
});
Now the complete example
package io.advantageous.qbit.http.jetty;
import io.advantageous.qbit.http.client.HttpClient;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.http.websocket.WebSocket;
import org.boon.core.Sys;
import static io.advantageous.qbit.http.client.HttpClientBuilder.httpClientBuilder;
import static io.advantageous.qbit.http.server.HttpServerBuilder.httpServerBuilder;
import static org.boon.Boon.puts;
/**
* Created by rhightower on 2/16/15.
*/
public class EchoWebSocketMoreComplex {
public static void main(String... args) {
/* Create an HTTP server. */
HttpServer httpServer = httpServerBuilder()
.setPort(8080).build();
/* Setup WebSocket Server support. */
httpServer.setWebSocketOnOpenConsumer(webSocket -> {
/** Set up onMessage. */
webSocket.setTextMessageConsumer(message -> {
webSocket.sendText("ECHO " + message);
});
/** Set up onClose. */
webSocket.setCloseConsumer(obj -> {
puts("SERVER CLOSE ");
});
/** Set up onError. */
webSocket.setErrorConsumer(error -> {
puts("SERVER ERROR", error);
});
});
/* Start the server. */
httpServer.start();
/** CLIENT. */
/* Setup an httpClient. */
HttpClient httpClient = httpClientBuilder()
.setHost("localhost").setPort(8080).build();
httpClient.start();
/* Setup the client websocket. */
WebSocket webSocket = httpClient
.createWebSocket("/websocket/rocket");
webSocket.setTextMessageConsumer(message -> {
System.out.println("CLIENT ON MESSAGE \n" + message);
});
/** Set up onClose. */
webSocket.setCloseConsumer(obj -> {
puts("CLIENT CLOSE");
});
/** Set up onError. */
webSocket.setErrorConsumer(error -> {
puts("CLIENT ERROR", error);
});
webSocket.openAndWait();
/* Send some messages. */
webSocket.sendText("Hi mom");
webSocket.sendText("Hello World!");
Sys.sleep(1000);
puts("----------- SHUTDOWN --------------");
webSocket.close();
Sys.sleep(100);
httpClient.stop();
httpServer.stop();
}
}
CLIENT ON MESSAGE
ECHO Hi mom
CLIENT ON MESSAGE
ECHO Hello World!
----------- SHUTDOWN --------------
SERVER CLOSE
CLIENT CLOSE
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