Skip to content

Instantly share code, notes, and snippets.

@brianm
Created March 4, 2010 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianm/322123 to your computer and use it in GitHub Desktop.
Save brianm/322123 to your computer and use it in GitHub Desktop.
package org.skife.jdbi.v2.unstable.async;
import org.skife.jdbi.derby.Tools;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.DBITestCase;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.skife.jdbi.v2.util.StringMapper;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestAsyncDBI extends DBITestCase
{
private Handle h;
private ExecutorService exec;
private AsyncDBI dbi;
public void setUp() throws Exception {
super.setUp();
exec = Executors.newCachedThreadPool();
dbi = new AsyncDBI(new DBI(Tools.getDataSource()), exec);
h = openHandle();
}
public void tearDown() throws Exception {
if (h != null) h.close();
Tools.stop();
exec.shutdown();
}
public void testWiffle() throws Exception
{
h.insert("insert into something (id, name) values (1, 'brian')");
ListenableFuture<String> f = dbi.async(new HandleCallback<String>() {
public String withHandle(Handle handle) throws Exception
{
return handle.createQuery("select name from something where id = 1")
.map(StringMapper.FIRST)
.first();
}
});
final CountDownLatch latch = new CountDownLatch(1);
f.addListener(new Runnable() {
public void run()
{
latch.countDown();
}
}, exec);
f.cancel(true);
latch.await();
assertEquals("brian", f.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment