package org.yaz4j;
import org.yaz4j.exception.ZoomException;
-import org.yaz4j.jni.yaz4jlib;
import static org.yaz4j.jni.yaz4jlib.*;
/**
public class AsyncConnection extends Connection {
private ResultSet lastResultSet;
ErrorHandler eh;
+ //make sure error is only handled once
+ boolean errorHandled = false;
+ ErrorHandler reh;
SearchHandler sh;
RecordHandler rh;
public AsyncConnection(String host, int port) {
super(host, port);
ZOOM_connection_option_set(zoomConnection, "async", "1");
- //what about piggy back?
- ZOOM_connection_option_set(zoomConnection, "count", "100");
- ZOOM_connection_option_set(zoomConnection, "step", "20");
closed = false;
}
@Override
public ResultSet search(Query query) throws ZoomException {
+ errorHandled = false;
lastResultSet = super.search(query);
return null;
}
return this;
}
+ public AsyncConnection onRecordError(ErrorHandler reh) {
+ this.reh = reh;
+ return this;
+ }
+
//actuall handler, pkg-private
void handleSearch() {
}
void handleRecord() {
+ //TODO clone the record to detach it from the result set
try {
if (rh != null) rh.handle(lastResultSet.getRecord(lastResultSet.asyncRecordOffset));
} catch (ZoomException ex) {
- if (eh != null) eh.handle(ex);
+ if (reh != null) reh.handle(ex);
} finally {
lastResultSet.asyncRecordOffset++;
}
void handleError() {
//handle error
- ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
- if (err != null) {
- if (eh != null) eh.handle(err);
+ if (!errorHandled) {
+ ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
+ if (err != null) {
+ if (eh != null) {
+ eh.handle(err);
+ errorHandled = true;
+ }
+ }
}
}
@After
public void tearDown() {
}
+
+ /**
+ * Test async ZOOM operation.
+ */
+ @Test
+ public void testBadDatabaseTarget() {
+ out.println("Trying bad async connection...");
+ AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/doesnotexist", 0);
+ conn.option("count", "100");
+ AsyncConnections conns = new AsyncConnections();
+ conns.add(conn);
+ final Box<Boolean> hadError = new Box<Boolean>(false);
+ try {
+ conn.setSyntax("sutrs");
+ conn.connect();
+ conn.search(new PrefixQuery("@attr 1=4 utah"));
+ conn
+ .onError(new AsyncConnection.ErrorHandler() {
+
+ public void handle(ZoomException e) {
+ out.println("There was an error "+e.getMessage());
+ hadError.setItem(true);
+ }
+ });
+
+ } catch (ZoomException ex) {
+ fail(ex.getMessage());
+ }
+ conns.start();
+ assertTrue(hadError.item);
+ }
+
+ /**
+ * Test async ZOOM operation.
+ */
+ @Test
+ public void testBadTarget() {
+ out.println("Trying bad target async connection...");
+ AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:70000/doesnotexist", 0);
+ conn.option("count", "100");
+ AsyncConnections conns = new AsyncConnections();
+ conns.add(conn);
+ final Box<Boolean> hadError = new Box<Boolean>(false);
+ try {
+ conn.setSyntax("sutrs");
+ conn.connect();
+ conn.search(new PrefixQuery("@attr 1=4 utah"));
+ conn
+ .onError(new AsyncConnection.ErrorHandler() {
+
+ public void handle(ZoomException e) {
+ out.println("There was an error "+e.getMessage());
+ hadError.setItem(true);
+ }
+ });
+
+ } catch (ZoomException ex) {
+ fail(ex.getMessage());
+ }
+ conns.start();
+ assertTrue(hadError.item);
+ }
/**
* Test async ZOOM operation.
public void testSingleTarget() {
out.println("Trying async connection...");
AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
+ conn.option("count", "100");
AsyncConnections conns = new AsyncConnections();
conns.add(conn);
int expectedHitCount = 9;
out.println("Trying async with multile connections...");
AsyncConnections conns = new AsyncConnections();
AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
+ conn.option("count", "100");
conns.add(conn);
AsyncConnection conn2 = new AsyncConnection("z3950.indexdata.dk:210/marc", 0);
+ conn2.option("count", "100");
conns.add(conn2);
int expectedHitCount = 19; //for both
final Box<Long> actualHitCount = new Box<Long>(0L);
out.println("Received a record of type "+r.getSyntax());
actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
}
- })
+ })
.onError(new AsyncConnection.ErrorHandler() {
public void handle(ZoomException e) {