add SocketTimeoutException listener

This commit is contained in:
Jörg Prante 2021-08-31 11:40:33 +02:00
parent 809792e64e
commit 31fa5fc126
14 changed files with 72 additions and 42 deletions

View file

@ -1,6 +1,6 @@
group = org.xbib
name = z3950
version = 2.3.3
version = 2.3.4
gradle.wrapper.version = 6.6.1
netty.version = 4.1.66.Final

View file

@ -0,0 +1,6 @@
package org.xbib.z3950.api;
public interface TimeoutListener {
void onTimeout();
}

View file

@ -1,4 +0,0 @@
/**
* API for Z39.50 applications.
*/
package org.xbib.z3950.api;

View file

@ -3,6 +3,8 @@ package org.xbib.z3950.client.api;
import org.xbib.z3950.api.RecordListener;
import org.xbib.z3950.api.ScanListener;
import org.xbib.z3950.api.SearchListener;
import org.xbib.z3950.api.TimeoutListener;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
@ -11,14 +13,17 @@ public interface Client extends Closeable {
int searchCQL(String query, int offset, int length,
SearchListener searchListener,
RecordListener recordListener) throws IOException;
RecordListener recordListener,
TimeoutListener timeoutListener) throws IOException;
int searchPQF(String query, int offset, int length,
SearchListener searchListener,
RecordListener recordListener) throws IOException;
RecordListener recordListener,
TimeoutListener timeoutListener) throws IOException;
void scanPQF(String query, int nTerms, int step, int position,
ScanListener scanListener) throws IOException;
ScanListener scanListener,
TimeoutListener timeoutListener) throws IOException;
String getHost();

View file

@ -2,6 +2,7 @@ package org.xbib.z3950.client.jdk;
import org.xbib.asn1.io.InputStreamBERReader;
import org.xbib.asn1.io.OutputStreamBERWriter;
import org.xbib.z3950.api.TimeoutListener;
import org.xbib.z3950.common.operations.CloseOperation;
import org.xbib.z3950.common.operations.InitOperation;
import org.xbib.z3950.common.operations.PresentOperation;
@ -20,6 +21,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;
@ -106,7 +108,8 @@ public class JDKZClient implements Client, Closeable {
@Override
public int searchCQL(String query, int offset, int length,
SearchListener searchListener,
RecordListener recordListener) throws IOException {
RecordListener recordListener,
TimeoutListener timeoutListener) throws IOException {
if (query == null) {
throw new IllegalArgumentException("no query");
}
@ -139,6 +142,11 @@ public class JDKZClient implements Client, Closeable {
}
}
return searchOperation.getCount();
} catch (SocketTimeoutException e) {
if (timeoutListener != null) {
timeoutListener.onTimeout();
}
return 0;
} finally {
lock.unlock();
}
@ -147,7 +155,8 @@ public class JDKZClient implements Client, Closeable {
@Override
public int searchPQF(String query, int offset, int length,
SearchListener searchListener,
RecordListener recordListener) throws IOException {
RecordListener recordListener,
TimeoutListener timeoutListener) throws IOException {
if (query == null) {
throw new IllegalArgumentException("no query");
}
@ -181,19 +190,32 @@ public class JDKZClient implements Client, Closeable {
}
}
return search.getCount();
} catch (SocketTimeoutException e) {
if (timeoutListener != null) {
timeoutListener.onTimeout();
}
return 0;
} finally {
lock.unlock();
}
}
@Override
public void scanPQF(String query, int nTerms, int step, int position,
ScanListener scanListener) throws IOException {
public void scanPQF(String query,
int nTerms,
int step,
int position,
ScanListener scanListener,
TimeoutListener timeoutListener) throws IOException {
ensureConnected();
try {
lock.lock();
ScanOperation scanOperation = new ScanOperation(berReader, berWriter, databases);
scanOperation.executePQF(nTerms, step, position, query, scanListener);
} catch (SocketTimeoutException e) {
if (timeoutListener != null) {
timeoutListener.onTimeout();
}
} finally {
lock.unlock();
}
@ -280,7 +302,7 @@ public class JDKZClient implements Client, Closeable {
}
}
public void disconnect() throws IOException {
public void disconnect() {
try {
lock.lock();
try {
@ -311,7 +333,7 @@ public class JDKZClient implements Client, Closeable {
}
@Override
public void close() throws IOException {
public void close() {
if (isConnected()) {
disconnect();
}

View file

@ -30,7 +30,8 @@ class DefaultClientTest {
int count = client.searchCQL(query, from, size,
(status, total, returned, elapsedMillis) ->
logger.log(Level.INFO, serviceName + " total results = " + total),
record -> logger.log(Level.INFO, "record = " + record));
record -> logger.log(Level.INFO, "record = " + record),
() -> logger.log(Level.INFO, "timeout"));
logger.log(Level.INFO, "returned records = " + count);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
@ -49,7 +50,8 @@ class DefaultClientTest {
int count = client.searchPQF(query, from, size,
(status, total, returned, elapsedMillis) ->
logger.log(Level.INFO, serviceName + " status = " + status + " total results = " + total),
record -> logger.log(Level.INFO, "record = " + record.toString(Charset.forName(client.getEncoding()))));
record -> logger.log(Level.INFO, "record = " + record.toString(Charset.forName(client.getEncoding()))),
() -> logger.log(Level.WARNING, "timeout"));
logger.log(Level.INFO, "returned records = " + count);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);

View file

@ -30,7 +30,8 @@ class GBVZClientTest {
int count = client.searchPQF(query, from, size,
(status, total, returned, elapsedMillis) ->
logger.log(Level.INFO, "total results = " + total + " millis = " + elapsedMillis),
record -> logger.log(Level.INFO, "record = " + record.toString(StandardCharsets.UTF_8)));
record -> logger.log(Level.INFO, "record = " + record.toString(StandardCharsets.UTF_8)),
() -> logger.log(Level.WARNING, "timeout"));
logger.log(Level.INFO, "record count = " + count);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
@ -48,7 +49,8 @@ class GBVZClientTest {
int count = client.searchCQL(query, from, size,
(status, total, returned, elapsedMillis) ->
logger.log(Level.INFO, serviceName + " total results = " + total),
record -> logger.log(Level.INFO, "record = " + record));
record -> logger.log(Level.INFO, "record = " + record),
() -> logger.log(Level.WARNING, "timeout"));
logger.log(Level.INFO, "returned records = " + count);
assertEquals(1, count);
} catch (Exception e) {

View file

@ -35,7 +35,8 @@ class SearchTest {
.build();
client.searchPQF(query, from, length,
(status, total, returned, elapsedMillis) -> logger.log(Level.INFO, "total records = " + total),
record -> logger.log(Level.INFO, "found record " + record));
record -> logger.log(Level.INFO, "found record " + record),
() -> logger.log(Level.WARNING, "timeout"));
client.close();
} catch (NoRecordsReturnedException | MessageSizeTooSmallException e) {
logger.log(Level.SEVERE, e.getMessage(), e);

View file

@ -16,6 +16,7 @@ import io.netty.util.CharsetUtil;
import org.xbib.z3950.api.RecordListener;
import org.xbib.z3950.api.ScanListener;
import org.xbib.z3950.api.SearchListener;
import org.xbib.z3950.api.TimeoutListener;
import org.xbib.z3950.client.api.Client;
import java.io.Closeable;
import java.io.IOException;
@ -59,17 +60,32 @@ public class NettyZClient implements Client, Closeable {
}
@Override
public int searchCQL(String query, int offset, int length, SearchListener searchListener, RecordListener recordListener) throws IOException {
public int searchCQL(String query,
int offset,
int length,
SearchListener searchListener,
RecordListener recordListener,
TimeoutListener timoutListener) throws IOException {
return 0;
}
@Override
public int searchPQF(String query, int offset, int length, SearchListener searchListener, RecordListener recordListener) throws IOException {
public int searchPQF(String query,
int offset,
int length,
SearchListener searchListener,
RecordListener recordListener,
TimeoutListener timeoutListener) throws IOException {
return 0;
}
@Override
public void scanPQF(String query, int nTerms, int step, int position, ScanListener scanListener) throws IOException {
public void scanPQF(String query,
int nTerms,
int step,
int position,
ScanListener scanListener,
TimeoutListener timeoutListener) throws IOException {
}

View file

@ -1,4 +0,0 @@
/**
*
*/
package org.xbib.z3950.common.cql;

View file

@ -1,4 +0,0 @@
/**
*
*/
package org.xbib.z3950.common.exceptions;

View file

@ -1,4 +0,0 @@
/**
*
*/
package org.xbib.z3950.common.operations;

View file

@ -1,4 +0,0 @@
/**
*
*/
package org.xbib.z3950.common.pqf;

View file

@ -1,4 +0,0 @@
/**
*
*/
package org.xbib.z3950.common.v3;