use of java.util.concurrent.LinkedBlockingQueue in project druid by druid-io.
the class IrcFirehoseFactory method connect.
@Override
public Firehose connect(final IrcInputRowParser firehoseParser) throws IOException {
final IRCApi irc = new IRCApiImpl(false);
final LinkedBlockingQueue<Pair<DateTime, ChannelPrivMsg>> queue = new LinkedBlockingQueue<Pair<DateTime, ChannelPrivMsg>>();
irc.addListener(new VariousMessageListenerAdapter() {
@Override
public void onChannelMessage(ChannelPrivMsg aMsg) {
try {
queue.put(Pair.of(DateTime.now(), aMsg));
} catch (InterruptedException e) {
throw new RuntimeException("interrupted adding message to queue", e);
}
}
});
log.info("connecting to irc server [%s]", host);
irc.connect(new IServerParameters() {
@Override
public String getNickname() {
return nick;
}
@Override
public List<String> getAlternativeNicknames() {
return Lists.newArrayList(nick + UUID.randomUUID(), nick + UUID.randomUUID(), nick + UUID.randomUUID());
}
@Override
public String getIdent() {
return "druid";
}
@Override
public String getRealname() {
return nick;
}
@Override
public IRCServer getServer() {
return new IRCServer(host, false);
}
}, new Callback<IIRCState>() {
@Override
public void onSuccess(IIRCState aObject) {
log.info("irc connection to server [%s] established", host);
for (String chan : channels) {
log.info("Joining channel %s", chan);
irc.joinChannel(chan);
}
}
@Override
public void onFailure(Exception e) {
log.error(e, "Unable to connect to irc server [%s]", host);
throw new RuntimeException("Unable to connect to server", e);
}
});
closed = false;
return new Firehose() {
InputRow nextRow = null;
@Override
public boolean hasMore() {
try {
while (true) {
Pair<DateTime, ChannelPrivMsg> nextMsg = queue.poll(100, TimeUnit.MILLISECONDS);
if (closed) {
return false;
}
if (nextMsg == null) {
continue;
}
try {
nextRow = firehoseParser.parse(nextMsg);
if (nextRow != null) {
return true;
}
} catch (IllegalArgumentException iae) {
log.debug("ignoring invalid message in channel [%s]", nextMsg.rhs.getChannelName());
}
}
} catch (InterruptedException e) {
Thread.interrupted();
throw new RuntimeException("interrupted retrieving elements from queue", e);
}
}
@Override
public InputRow nextRow() {
return nextRow;
}
@Override
public Runnable commit() {
return new Runnable() {
@Override
public void run() {
// nothing to see here
}
};
}
@Override
public void close() throws IOException {
try {
log.info("disconnecting from irc server [%s]", host);
irc.disconnect("");
} finally {
closed = true;
}
}
};
}
use of java.util.concurrent.LinkedBlockingQueue in project jetty.project by eclipse.
the class OverlayedAppProviderTest method testTriageURI.
@Test
public void testTriageURI() throws Exception {
final BlockingQueue<String> scanned = new LinkedBlockingQueue<String>();
OverlayedAppProvider provider = new OverlayedAppProvider() {
protected void removeInstance(String name) {
scanned.add("removeInstance " + name);
}
protected Instance loadInstance(String name, File origin) {
scanned.add("loadInstance " + name);
scanned.add(origin.getAbsolutePath());
return null;
}
protected void removeNode() {
scanned.add("removeNode");
}
protected Node loadNode(File origin) {
scanned.add("loadNode");
scanned.add(origin.getAbsolutePath());
return null;
}
protected void removeTemplate(String name) {
scanned.add("removeTemplate " + name);
}
protected Template loadTemplate(String name, File origin) {
scanned.add("loadTemplate " + name);
scanned.add(origin.getAbsolutePath());
return null;
}
protected void removeWebapp(String name) {
scanned.add("removeWebapp " + name);
}
protected Webapp loadWebapp(String name, File origin) {
scanned.add("loadWebapp " + name);
scanned.add(origin.getAbsolutePath());
return null;
}
protected void redeploy() {
}
};
provider.setScanInterval(0);
provider.setNodeName("nodeA");
provider.setScanDir(_scan);
provider.start();
provider.scan();
assertTrue(scanned.isEmpty());
// Add a war
File war = new File(_webapps, "foo-1.2.3.war");
touch(war);
provider.scan();
provider.scan();
assertEquals("loadWebapp foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(war.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add a template
File template = new File(_templates, "foo=foo-1.2.3.war");
touch(template);
provider.scan();
provider.scan();
assertEquals("loadTemplate foo=foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(template.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add a node
File nodeA = new File(_nodes, "nodeA.war");
touch(nodeA);
provider.scan();
provider.scan();
assertEquals("loadNode", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(nodeA.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add another node
File nodeB = new File(_nodes, "nodeB.war");
provider.scan();
provider.scan();
assertTrue(scanned.isEmpty());
// Add an instance
File instance = new File(_instances, "foo=instance.war");
touch(instance);
provider.scan();
provider.scan();
assertEquals("loadInstance foo=instance", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(instance.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add a war dir
File warDir = new File(_webapps, "foo-1.2.3");
warDir.mkdir();
File warDirWI = new File(warDir, "WEB-INF");
warDirWI.mkdir();
touch(warDirWI, "web.xml");
provider.scan();
provider.scan();
assertEquals("loadWebapp foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(warDir.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add a template dir
File templateDir = new File(_templates, "foo=foo-1.2.3");
templateDir.mkdir();
File templateDirWI = new File(templateDir, "WEB-INF");
templateDirWI.mkdir();
touch(templateDirWI, "web.xml");
provider.scan();
provider.scan();
assertEquals("loadTemplate foo=foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(templateDir.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add a node dir
File nodeADir = new File(_nodes, "nodeA");
nodeADir.mkdir();
File nodeADirWI = new File(nodeADir, "WEB-INF");
nodeADirWI.mkdir();
touch(nodeADirWI, "web.xml");
provider.scan();
provider.scan();
assertEquals("loadNode", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(nodeADir.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Add another node dir
File nodeBDir = new File(_nodes, "nodeB");
nodeBDir.mkdir();
File nodeBDirWI = new File(nodeBDir, "WEB-INF");
nodeBDirWI.mkdir();
touch(nodeBDirWI, "web.xml");
provider.scan();
provider.scan();
assertTrue(scanned.isEmpty());
// Add an instance dir
File instanceDir = new File(_instances, "foo=instance");
instanceDir.mkdir();
File instanceDirWI = new File(instanceDir, "WEB-INF");
instanceDirWI.mkdir();
touch(instanceDirWI, "web.xml");
provider.scan();
provider.scan();
assertEquals("loadInstance foo=instance", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(instanceDir.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// touch archives will be ignored.
Thread.sleep(1000);
touch(war);
touch(template);
touch(nodeA);
touch(nodeB);
touch(instance);
provider.scan();
provider.scan();
assertTrue(scanned.isEmpty());
// Touch directories
for (File d : new File[] { warDir, templateDir, nodeADir, nodeBDir, instanceDir }) touch(d, "WEB-INF/web.xml");
provider.scan();
provider.scan();
assertEquals(8, scanned.size());
scanned.clear();
// Remove web dir
IO.delete(warDir);
provider.scan();
provider.scan();
assertEquals("removeWebapp foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals("loadWebapp foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(war.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Remove template dir
IO.delete(templateDir);
provider.scan();
provider.scan();
assertEquals("removeTemplate foo=foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals("loadTemplate foo=foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(template.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Remove nodeA dir
IO.delete(nodeADir);
provider.scan();
provider.scan();
assertEquals("removeNode", scanned.poll(1, TimeUnit.SECONDS));
assertEquals("loadNode", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(nodeA.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Remove nodeB dir
IO.delete(nodeBDir);
provider.scan();
provider.scan();
assertTrue(scanned.isEmpty());
// Remove instance dir
IO.delete(instanceDir);
provider.scan();
provider.scan();
assertEquals("removeInstance foo=instance", scanned.poll(1, TimeUnit.SECONDS));
assertEquals("loadInstance foo=instance", scanned.poll(1, TimeUnit.SECONDS));
assertEquals(instance.getAbsolutePath(), scanned.poll(1, TimeUnit.SECONDS));
// Remove web
IO.delete(war);
provider.scan();
provider.scan();
assertEquals("removeWebapp foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
// Remove template
IO.delete(template);
provider.scan();
provider.scan();
assertEquals("removeTemplate foo=foo-1.2.3", scanned.poll(1, TimeUnit.SECONDS));
// Remove nodeA dir
IO.delete(nodeA);
provider.scan();
provider.scan();
assertEquals("removeNode", scanned.poll(1, TimeUnit.SECONDS));
// Remove nodeB dir
IO.delete(nodeB);
provider.scan();
provider.scan();
assertTrue(scanned.isEmpty());
// Remove instance dir
IO.delete(instance);
provider.scan();
provider.scan();
assertEquals("removeInstance foo=instance", scanned.poll(1, TimeUnit.SECONDS));
provider.scan();
provider.scan();
assertTrue(scanned.isEmpty());
}
use of java.util.concurrent.LinkedBlockingQueue in project jetty.project by eclipse.
the class ServerTimeoutsTest method testBlockingTimeoutSmallerThanIdleTimeoutBlockingWriteBlockingTimeoutFires.
@Test
public void testBlockingTimeoutSmallerThanIdleTimeoutBlockingWriteBlockingTimeoutFires() throws Exception {
long blockingTimeout = 2500;
httpConfig.setBlockingTimeout(blockingTimeout);
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new BlockingWriteHandler(handlerLatch));
long idleTimeout = 3 * blockingTimeout;
setServerIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
// Do not succeed the callback so the server will block writing.
callbacks.offer(callback);
}).send(result -> {
if (result.isFailed())
resultLatch.countDown();
});
// Blocking write should timeout.
Assert.assertTrue(handlerLatch.await(2 * blockingTimeout, TimeUnit.MILLISECONDS));
// After the server stopped sending, consume on the client to read the early EOF.
while (true) {
Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
if (callback == null)
break;
callback.succeeded();
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
}
use of java.util.concurrent.LinkedBlockingQueue in project jetty.project by eclipse.
the class ServerTimeoutsTest method testAsyncWriteIdleTimeoutFires.
@Test
public void testAsyncWriteIdleTimeoutFires() throws Exception {
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
ServletOutputStream output = response.getOutputStream();
output.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
output.write(new byte[64 * 1024 * 1024]);
}
@Override
public void onError(Throwable failure) {
if (failure instanceof TimeoutException) {
asyncContext.complete();
handlerLatch.countDown();
}
}
});
}
});
long idleTimeout = 2500;
setServerIdleTimeout(idleTimeout);
BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
// Do not succeed the callback so the server will block writing.
callbacks.offer(callback);
}).send(result -> {
if (result.isFailed())
resultLatch.countDown();
});
// Async write should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// After the server stopped sending, consume on the client to read the early EOF.
while (true) {
Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
if (callback == null)
break;
callback.succeeded();
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
use of java.util.concurrent.LinkedBlockingQueue in project jetty.project by eclipse.
the class ServerTimeoutsTest method testBlockingTimeoutLargerThanIdleTimeoutBlockingWriteIdleTimeoutFires.
@Test
public void testBlockingTimeoutLargerThanIdleTimeoutBlockingWriteIdleTimeoutFires() throws Exception {
long idleTimeout = 2500;
long blockingTimeout = 3 * idleTimeout;
httpConfig.setBlockingTimeout(blockingTimeout);
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new BlockingWriteHandler(handlerLatch));
setServerIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
// Do not succeed the callback so the server will block writing.
callbacks.offer(callback);
}).send(result -> {
if (result.isFailed())
resultLatch.countDown();
});
// Blocking read should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// After the server stopped sending, consume on the client to read the early EOF.
while (true) {
Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
if (callback == null)
break;
callback.succeeded();
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
}
Aggregations