Package org.xbib.elasticsearch.http.netty.client

Examples of org.xbib.elasticsearch.http.netty.client.NettyWebSocketClientFactory


   
    public void testOneClient() {
        try {
            final String subscriberId = "oneclient";
            final String topic = "oneclienttest";
            final WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
           
            final WebSocketClientRequest subscribe = clientFactory.newRequest()
                    .type("subscribe").data(jsonBuilder().startObject()
                    .field("subscriber", "singleclient")
                    .field("topic", topic).endObject());
           
            final WebSocketClientRequest publish = clientFactory.newRequest()
                    .type("publish").data(jsonBuilder().startObject()
                    .field("message", "Hello World")
                    .field("topic", topic).endObject());
           
            WebSocketClient client = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) throws IOException {
                                logger.info("sending subscribe command, channel = {}", client.channel());
                                subscribe.send(client);
                                logger.info("sending publish command (to ourselves), channel = {}", client.channel());
                                publish.send(client);
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame received: " + frame);
                        }
                    });
            client.connect().await(1000, TimeUnit.MILLISECONDS);           
            Thread.sleep(1000);
            client.send(new CloseWebSocketFrame());
            Thread.sleep(1000);
            client.disconnect();
           
            clientFactory.shutdown();
           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }       
    }
View Full Code Here


    @Test
    public void testTwoClients() {
        try {
            final String subscriberId = "twoclients";
            final String topic = "twoclienttest";
            final WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();

            final WebSocketClientRequest subscribe = clientFactory.newRequest()
                    .type("subscribe").data(jsonBuilder().startObject()
                    .field("subscriber", "doubleclient")
                    .field("topic", topic).endObject());
           
            final WebSocketClientRequest publish = clientFactory.newRequest()
                    .type("publish").data(jsonBuilder().startObject()
                    .field("message", "Hi there, I'm another client")
                    .field("topic", topic).endObject());
           
            // open first client
            WebSocketClient subscribingClient = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending subscribe command, channel {}", client.channel());
                                subscribe.send(client);
                            } catch (Exception e) {
                            }
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame {} received for subscribing client {}", frame, client.channel());
                        }

                    });
           
            // open two client
            WebSocketClient publishingClient = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending publish command, channel = {}", client.channel());
                                publish.send(client);
                            } catch (Exception e) {
                            }
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame {} received for publishing client {}", frame, client.channel());
                        }

                    });
           
            // connect both clients to node
            subscribingClient.connect().await(1000, TimeUnit.MILLISECONDS);           
            publishingClient.connect().await(1000, TimeUnit.MILLISECONDS);           
           
            // wait for publish/subscribe
            Thread.sleep(1000);
           
            // close first client
            publishingClient.send(new CloseWebSocketFrame());
            publishingClient.disconnect();

            // close second client
            subscribingClient.send(new CloseWebSocketFrame());
            subscribingClient.disconnect();
           
            Thread.sleep(1000);
           
            clientFactory.shutdown();
           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }       
    }
View Full Code Here

     * In this test, we just use simple TextWebSocketFrame to send publish/subscribe requests.
     */
    @Test
    public void subscribeToOurselves() {
        try {
            WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
            WebSocketClient client = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending subscribe command");
View Full Code Here

    private final static ESLogger logger = ESLoggerFactory.getLogger("test");

    @Test
    public void testBulk() {
        try {
            final WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
            WebSocketClient client = clientFactory.newClient(
                    getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending some index requests (longer than a single bulk size)");
                                for (int i = 0; i < 250; i++) {
                                    clientFactory.indexRequest()
                                            .data(jsonBuilder()                                           
                                            .startObject()
                                            .field("index", "test")
                                            .field("type", "test")
                                            .field("id", Integer.toString(i))
                                            .startObject("data")
                                            .field("field1", "value" + i)
                                            .field("field2", "value" + i)
                                            .endObject()
                                            .endObject())
                                            .send(client);
                                }
                                // more bulks could be added here ...
                                logger.info("at the end, let us flush the bulk");
                                clientFactory.flushRequest().send(client);
                            } catch (Exception e) {
                                onError(e);
                            }
                        }

                        @Override
                        public void onDisconnect(WebSocketClient client) {
                            logger.info("disconnected");
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame received: {}", frame);
                        }

                        @Override
                        public void onError(Throwable t) {
                            logger.error(t.getMessage(), t);
                        }
                    });
            client.connect().await(1000, TimeUnit.MILLISECONDS);
            Thread.sleep(1000);
            logger.info("closing bulk client");
            client.send(new CloseWebSocketFrame());
            Thread.sleep(1000);
            client.disconnect();
            clientFactory.shutdown();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }
View Full Code Here

     * Primitive websocket client, just send "Hello World"
     */
    @Test
    public void helloWorld() {
        try {
            WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
            WebSocketClient client = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            logger.info("web socket connected");
                            String s = "{\"Hello\":\"World\"}";
View Full Code Here

TOP

Related Classes of org.xbib.elasticsearch.http.netty.client.NettyWebSocketClientFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.