Changeset 2183


Ignore:
Timestamp:
08/30/12 13:57:14 (12 years ago)
Author:
oschonef
Message:
  • add callback mechanism to SRUThreadedClient
Location:
SRUClient/trunk/src
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUAbstractResponse.java

    r2168 r2183  
    3131 * @see SRUSearchRetrieveResponse
    3232 */
    33 class SRUAbstractResponse<T> {
     33class SRUAbstractResponse<T extends SRUAbstractRequest> {
    3434    private final T request;
    3535    private final List<SRUDiagnostic> diagnostics;
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUThreadedClient.java

    r2168 r2183  
    3636 * client object and may shared it between multiple threads. <br />
    3737 * NB: The registered {@link SRURecordDataParser} need to be thread-safe
    38  * </p> 
     38 * </p>
    3939 */
    4040public class SRUThreadedClient {
     
    140140     * @param request
    141141     *            an instance of a {@link SRUExplainRequest} object
    142      * @return a {@link SRUExplainResponse} object
     142     * @return a {@link Future} object that wraps a {@link SRUExplainResponse}
     143     *         object
    143144     * @throws SRUClientException
    144145     *             if an unrecoverable error occurred
     
    165166
    166167    /**
     168     * Perform a <em>explain</em> operation and invoke a user supplied callback
     169     * after the request has been completed.
     170     *
     171     * @param request
     172     *            an instance of a {@link SRUExplainRequest} object
     173     * @param callback
     174     *            the callback to be invoked
     175     * @throws SRUClientException
     176     *             if an unrecoverable error occurred
     177     * @throws NullPointerException
     178     *             if any required argument is <code>null</code>
     179     * @see SRUCallback
     180     */
     181    public void explain(final SRUExplainRequest request,
     182            final SRUCallback<SRUExplainResponse> callback)
     183            throws SRUClientException {
     184        if (request == null) {
     185            throw new NullPointerException("request == null");
     186        }
     187        if (callback == null) {
     188            throw new NullPointerException("callback == null");
     189        }
     190        if (executor.isShutdown()) {
     191            throw new SRUClientException("client is shutting down");
     192        }
     193        executor.submit(new AsyncRequest<SRUExplainRequest, SRUExplainResponse>(
     194                request, callback) {
     195            @Override
     196            protected SRUExplainResponse doRequest(SRUClient client)
     197                    throws SRUClientException {
     198                return client.explain(request);
     199            }
     200        });
     201    }
     202
     203
     204    /**
    167205     * Perform a <em>scan</em> operation.
    168206     *
    169207     * @param request
    170208     *            an instance of a {@link SRUScanRequest} object
    171      * @return a {@link SRUScanResponse} object
     209     * @return a {@link Future} object that wraps a {@link SRUScanResponse}
     210     *         object
    172211     * @throws SRUClientException
    173212     *             if an unrecoverable error occurred
     
    195234
    196235    /**
     236     * Perform a <em>scan</em> operation and invoke a user supplied callback
     237     * after the request has been completed.
     238     *
     239     * @param request
     240     *            an instance of a {@link SRUScanRequest} object
     241     * @param callback
     242     *            the callback to be invoked
     243     * @throws SRUClientException
     244     *             if an unrecoverable error occurred
     245     * @throws NullPointerException
     246     *             if any required argument is <code>null</code>
     247     * @see SRUCallback
     248     */
     249    public void scan(final SRUScanRequest request,
     250            final SRUCallback<SRUScanResponse> callback)
     251            throws SRUClientException {
     252        if (request == null) {
     253            throw new NullPointerException("request == null");
     254        }
     255        if (callback == null) {
     256            throw new NullPointerException("callback == null");
     257        }
     258        if (executor.isShutdown()) {
     259            throw new SRUClientException("client is shutting down");
     260        }
     261        executor.submit(new AsyncRequest<SRUScanRequest, SRUScanResponse>(
     262                request, callback) {
     263            @Override
     264            protected SRUScanResponse doRequest(SRUClient client)
     265                    throws SRUClientException {
     266                return client.scan(request);
     267            }
     268        });
     269    }
     270
     271
     272    /**
    197273     * Perform a <em>searchRetrieve</em> operation.
    198274     *
    199275     * @param request
    200276     *            an instance of a {@link SRUSearchRetrieveRequest} object
    201      * @return a {@link SRUSearchRetrieveRequest} object
     277     * @return a {@link Future} object that wraps a {@link SRUExplainResponse}
     278     *         object
    202279     * @throws SRUClientException
    203280     *             if an unrecoverable error occurred
     
    213290            throw new SRUClientException("client is shutting down");
    214291        }
    215         return executor.submit(new Request<SRUSearchRetrieveRequest, SRUSearchRetrieveResponse>(request) {
     292        return executor.submit(new Request<SRUSearchRetrieveRequest,
     293                SRUSearchRetrieveResponse>(request) {
     294            @Override
     295            protected SRUSearchRetrieveResponse doRequest(SRUClient client)
     296                    throws SRUClientException {
     297                return client.searchRetrieve(request);
     298            }
     299        });
     300    }
     301
     302
     303    /**
     304     * Perform a <em>searchRetrieve</em> operation and invoke a user supplied
     305     * callback after the request has been completed.
     306     *
     307     * @param request
     308     *            an instance of a {@link SRUSearchRetrieveRequest} object
     309     * @param callback
     310     *            the callback to be invoked
     311     * @throws SRUClientException
     312     *             if an unrecoverable error occurred
     313     * @throws NullPointerException
     314     *             if any required argument is <code>null</code>
     315     * @see SRUCallback
     316     */
     317    public void searchRetrieve(final SRUSearchRetrieveRequest request,
     318            final SRUCallback<SRUSearchRetrieveResponse> callback)
     319            throws SRUClientException {
     320        if (request == null) {
     321            throw new NullPointerException("request == null");
     322        }
     323        if (callback == null) {
     324            throw new NullPointerException("callback == null");
     325        }
     326        if (executor.isShutdown()) {
     327            throw new SRUClientException("client is shutting down");
     328        }
     329        executor.submit(new AsyncRequest<SRUSearchRetrieveRequest,
     330                SRUSearchRetrieveResponse>(request, callback) {
    216331            @Override
    217332            protected SRUSearchRetrieveResponse doRequest(SRUClient client)
     
    293408    }
    294409
     410
     411    private abstract class AsyncRequest<V extends SRUAbstractRequest,
     412                                        S extends SRUAbstractResponse<?>>
     413            implements Runnable {
     414        protected final V request;
     415        private final SRUCallback<S> callback;
     416
     417
     418        public AsyncRequest(V request, SRUCallback<S> callback) {
     419            this.callback = callback;
     420            this.request = request;
     421        }
     422
     423
     424        @Override
     425        public void run() {
     426            try {
     427                S response = doRequest(client.get());
     428                callback.done(response);
     429            } catch (SRUClientException e) {
     430                logger.debug("exception", e);
     431            } catch (Throwable t) {
     432                logger.error("something went wrong", t);
     433            }
     434        }
     435
     436        protected abstract S doRequest(SRUClient client) throws SRUClientException;
     437    }
     438
    295439} // class SRUThreadedClient
Note: See TracChangeset for help on using the changeset viewer.