2 * Copyright (c) 1995-2015, Index Data
4 * See the file LICENSE for details.
8 import org.yaz4j.exception.ZoomException;
9 import static org.yaz4j.jni.yaz4jlib.*;
10 import org.yaz4j.util.Unstable;
17 public class AsyncConnection extends Connection {
18 private ResultSet lastResultSet;
20 //make sure error is only handled once
21 boolean errorHandled = false;
22 int handledRecordOffset = 0;
27 public interface SearchHandler {
28 public void handle(ResultSet rs);
31 public interface RecordHandler {
32 public void handle(Record r);
35 public interface ErrorHandler {
36 public void handle(ZoomException e);
39 public AsyncConnection(String host, int port) {
41 ZOOM_connection_option_set(zoomConnection, "async", "1");
46 public ResultSet search(Query query) throws ZoomException {
48 lastResultSet = super.search(query);
52 public AsyncConnection onSearch(SearchHandler sh) {
57 public AsyncConnection onRecord(RecordHandler rh) {
62 public AsyncConnection onError(ErrorHandler eh) {
67 public AsyncConnection onRecordError(ErrorHandler reh) {
72 //actuall handler, pkg-private
77 if (sh != null) sh.handle(lastResultSet);
81 //TODO clone the record to detach it from the result set
83 if (rh != null) rh.handle(lastResultSet.getRecord(handledRecordOffset));
84 } catch (ZoomException ex) {
85 if (reh != null) reh.handle(ex);
87 handledRecordOffset++;
94 ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);