Search in sources :

Example 1 with WebSocketTextListener

use of org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener in project apex-malhar by apache.

the class WebSocketInputOperator method run.

@Override
public void run() {
    try {
        connectionClosed = false;
        AsyncHttpClientConfigBean config = new AsyncHttpClientConfigBean();
        config.setIoThreadMultiplier(ioThreadMultiplier);
        config.setApplicationThreadPool(Executors.newCachedThreadPool(new ThreadFactory() {

            private long count = 0;

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName(ClassUtils.getShortClassName(this.getClass()) + "-AsyncHttpClient-" + count++);
                return t;
            }
        }));
        if (client != null) {
            client.closeAsynchronously();
        }
        client = new AsyncHttpClient(config);
        connection = client.prepareGet(uri.toString()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

            @Override
            public void onMessage(String string) {
                try {
                    T o = convertMessage(string);
                    if (!(skipNull && o == null)) {
                        outputPort.emit(o);
                    }
                } catch (IOException ex) {
                    LOG.error("Got exception: ", ex);
                }
            }

            @Override
            public void onOpen(WebSocket ws) {
                LOG.debug("Connection opened");
            }

            @Override
            public void onClose(WebSocket ws) {
                LOG.debug("Connection connectionClosed.");
                connectionClosed = true;
            }

            @Override
            public void onError(Throwable t) {
                LOG.error("Caught exception", t);
            }
        }).build()).get(5, TimeUnit.SECONDS);
    } catch (Exception ex) {
        LOG.error("Error reading from " + uri, ex);
        if (client != null) {
            client.close();
        }
        connectionClosed = true;
    }
}
Also used : AsyncHttpClientConfigBean(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean) ThreadFactory(java.util.concurrent.ThreadFactory) WebSocketTextListener(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener) IOException(java.io.IOException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler) IOException(java.io.IOException) WebSocket(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket) AsyncHttpClient(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient)

Example 2 with WebSocketTextListener

use of org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener in project apex-malhar by apache.

the class WebSocketOutputOperator method openConnection.

private void openConnection() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    final AsyncHttpClientConfigBean config = new AsyncHttpClientConfigBean();
    config.setIoThreadMultiplier(ioThreadMultiplier);
    config.setApplicationThreadPool(Executors.newCachedThreadPool(new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName(ClassUtils.getShortClassName(this.getClass()) + "-AsyncHttpClient-" + count++);
            return t;
        }
    }));
    client = new AsyncHttpClient(config);
    // force reparse after deserialization
    uri = URI.create(uri.toString());
    LOG.info("Opening URL: {}", uri);
    connection = client.prepareGet(uri.toString()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String string) {
        }

        @Override
        public void onOpen(WebSocket ws) {
            LOG.debug("Connection opened");
        }

        @Override
        public void onClose(WebSocket ws) {
            LOG.debug("Connection closed.");
        }

        @Override
        public void onError(Throwable t) {
            LOG.error("Caught exception", t);
        }
    }).build()).get(5, TimeUnit.SECONDS);
}
Also used : AsyncHttpClientConfigBean(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean) ThreadFactory(java.util.concurrent.ThreadFactory) WebSocketTextListener(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler) AsyncHttpClient(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient) WebSocket(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)2 AsyncHttpClient (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient)2 AsyncHttpClientConfigBean (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean)2 WebSocket (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket)2 WebSocketTextListener (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener)2 WebSocketUpgradeHandler (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler)2 IOException (java.io.IOException)1