use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean 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;
}
}
use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean 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);
}
Aggregations