source: FCSSimpleClient/trunk/src/test/java/eu/clarin/sru/client/fcs/TestThreadedClientCallback.java @ 7280

Last change on this file since 7280 was 7280, checked in by Oliver Schonefeld, 2 years ago
  • cleanup
  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1/**
2 * This software is copyright (c) 2012-2022 by
3 *  - Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
4 * This is free software. You can redistribute it
5 * and/or modify it under the terms described in
6 * the GNU General Public License v3 of which you
7 * should have received a copy. Otherwise you can download
8 * it from
9 *
10 *   http://www.gnu.org/licenses/gpl-3.0.txt
11 *
12 * @copyright Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
17package eu.clarin.sru.client.fcs;
18
19import java.util.concurrent.CountDownLatch;
20
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import eu.clarin.sru.client.SRUCallback;
25import eu.clarin.sru.client.SRUClientException;
26import eu.clarin.sru.client.SRUExplainRequest;
27import eu.clarin.sru.client.SRUExplainResponse;
28import eu.clarin.sru.client.SRUScanRequest;
29import eu.clarin.sru.client.SRUScanResponse;
30import eu.clarin.sru.client.SRUSearchRetrieveRequest;
31import eu.clarin.sru.client.SRUSearchRetrieveResponse;
32import eu.clarin.sru.client.SRUThreadedClient;
33
34
35public class TestThreadedClientCallback {
36    private static final Logger logger =
37            LoggerFactory.getLogger(TestThreadedClientCallback.class);
38
39    public static void main(String[] args) {
40        if (args.length > 0) {
41            logger.info("initializing client ...");
42
43            SRUThreadedClient client = new ClarinFCSClientBuilder()
44                    .addDefaultDataViewParsers()
45                    .unknownDataViewAsString()
46                    .enableLegacySupport()
47                    .registerExtraResponseDataParser(
48                            new ClarinFCSEndpointDescriptionParser())
49                    .buildThreadedClient();
50
51            try {
52                final CountDownLatch latch = new CountDownLatch(3);
53
54                /*
55                 * the following requests will be run asynchronously and
56                 * concurrently
57                 * Invoke requests and supply a callback, that
58                 *  a) prints the results
59                 *  b) downs a latch (which is used to make the main-thread wait
60                 *     on the requests to be completed)
61                 */
62                logger.info("submitting 'explain' request ...");
63                SRUExplainRequest request1 =
64                        TestUtils.makeExplainRequest(args[0]);
65                client.explain(request1, new SRUCallback<SRUExplainRequest, SRUExplainResponse>() {
66                    @Override
67                    public void onSuccess(SRUExplainResponse response) {
68                        TestUtils.printExplainResponse(response);
69                        latch.countDown();
70                    }
71
72                    @Override
73                    public void onError(SRUExplainRequest request,
74                            SRUClientException error) {
75                        logger.error("error while performing request", error);
76                        latch.countDown();
77                    }
78                });
79
80                logger.info("submitting 'scan' request ...");
81                SRUScanRequest request2 =
82                        TestUtils.makeScanRequest(args[0]);
83                client.scan(request2, new SRUCallback<SRUScanRequest, SRUScanResponse>() {
84                    @Override
85                    public void onSuccess(SRUScanResponse response) {
86                        TestUtils.printScanResponse(response);
87                        latch.countDown();
88                    }
89
90                    @Override
91                    public void onError(SRUScanRequest request,
92                            SRUClientException error) {
93                        logger.error("error while performing request", error);
94                        latch.countDown();
95                    }
96                });
97
98                logger.info("submitting 'searchRetrieve' request ...");
99                SRUSearchRetrieveRequest request3 =
100                        TestUtils.makeSearchRequest(args[0], null);
101                client.searchRetrieve(request3, new SRUCallback<SRUSearchRetrieveRequest, SRUSearchRetrieveResponse>() {
102                    @Override
103                    public void onSuccess(SRUSearchRetrieveResponse response) {
104                        TestUtils.printSearchResponse(response);
105                        latch.countDown();
106                    }
107
108                    @Override
109                    public void onError(SRUSearchRetrieveRequest request,
110                            SRUClientException error) {
111                        logger.error("error while performing request", error);
112                        latch.countDown();
113                    }
114                });
115
116                latch.await();
117            } catch (Exception e) {
118                logger.error("a fatal error occured while performing request", e);
119            }
120
121            client.shutdown();
122            logger.info("done");
123        } else {
124            System.err.println("missing args");
125            System.exit(64);
126        }
127    }
128
129
130    static {
131        org.apache.log4j.BasicConfigurator.configure(
132                new org.apache.log4j.ConsoleAppender(
133                        new org.apache.log4j.PatternLayout("%-5p [%t] %m%n"),
134                        org.apache.log4j.ConsoleAppender.SYSTEM_ERR));
135        org.apache.log4j.Logger logger =
136                org.apache.log4j.Logger.getRootLogger();
137        logger.setLevel(org.apache.log4j.Level.INFO);
138        logger.getLoggerRepository().getLogger("eu.clarin").setLevel(
139                org.apache.log4j.Level.DEBUG);
140    }
141
142}
Note: See TracBrowser for help on using the repository browser.