source: SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClientCallback.java @ 2467

Last change on this file since 2467 was 2467, checked in by oschonef, 11 years ago
  • (re-factor) rename package from "eu.clarin.sru.fcs" to "eu.clarin.sru.client.fcs"

HEADS UP: This breaks existing code! Please update your sources accordingly.

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1/**
2 * This software is copyright (c) 2011-2013 by
3 *  - 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 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;
18
19import java.util.concurrent.CountDownLatch;
20
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import eu.clarin.sru.client.fcs.ClarinFCSRecordParser;
25
26
27public class TestThreadedClientCallback {
28    private static final Logger logger =
29            LoggerFactory.getLogger(TestThreadedClientCallback.class);
30
31    public static void main(String[] args) {
32        if (args.length > 0) {
33            logger.info("initializing client ...");
34            SRUThreadedClient client = new SRUThreadedClient();
35
36            try {
37                client.registerRecordParser(new ClarinFCSRecordParser());
38            } catch (SRUClientException e) {
39                logger.error("error adding record parser", e);
40                System.exit(1);
41            }
42
43            try {
44                final CountDownLatch latch = new CountDownLatch(3);
45
46                /*
47                 * the following requests will be run asynchronously and
48                 * concurrently
49                 * Invoke requests and supply a callback, that
50                 *  a) prints the results
51                 *  b) downs a latch (which is used to make the main-thread wait
52                 *     on the requests to be completed)
53                 */
54                logger.info("submitting 'explain' request ...");
55                SRUExplainRequest request1 =
56                        TestUtils.makeExplainRequest(args[0]);
57                client.explain(request1, new SRUCallback<SRUExplainRequest, SRUExplainResponse>() {
58                    @Override
59                    public void onSuccess(SRUExplainResponse response) {
60                        TestUtils.printExplainResponse(response);
61                        latch.countDown();
62                    }
63
64                    @Override
65                    public void onError(SRUExplainRequest request,
66                            SRUClientException error) {
67                        logger.error("error while performing request", error);
68                        latch.countDown();
69                    }
70                });
71
72                logger.info("submitting 'scan' request ...");
73                SRUScanRequest request2 =
74                        TestUtils.makeScanRequest(args[0]);
75                client.scan(request2, new SRUCallback<SRUScanRequest, SRUScanResponse>() {
76                    @Override
77                    public void onSuccess(SRUScanResponse response) {
78                        TestUtils.printScanResponse(response);
79                        latch.countDown();
80                    }
81
82                    @Override
83                    public void onError(SRUScanRequest request,
84                            SRUClientException error) {
85                        logger.error("error while performing request", error);
86                        latch.countDown();
87                    }
88                });
89
90                logger.info("submitting 'searchRetrieve' request ...");
91                SRUSearchRetrieveRequest request3 =
92                        TestUtils.makeSearchRequest(args[0], null);
93                client.searchRetrieve(request3, new SRUCallback<SRUSearchRetrieveRequest, SRUSearchRetrieveResponse>() {
94                    @Override
95                    public void onSuccess(SRUSearchRetrieveResponse response) {
96                        TestUtils.printSearchResponse(response);
97                        latch.countDown();
98                    }
99
100                    @Override
101                    public void onError(SRUSearchRetrieveRequest request,
102                            SRUClientException error) {
103                        logger.error("error while performing request", error);
104                        latch.countDown();
105                    }
106                });
107
108                latch.await();
109            } catch (Exception e) {
110                logger.error("a fatal error occured while performing request", e);
111            }
112
113            client.shutdown();
114            logger.info("done");
115        } else {
116            System.err.println("missing args");
117            System.exit(64);
118        }
119    }
120
121
122    static {
123        org.apache.log4j.BasicConfigurator.configure(
124                new org.apache.log4j.ConsoleAppender(
125                        new org.apache.log4j.PatternLayout("%-5p [%t] %m%n"),
126                        org.apache.log4j.ConsoleAppender.SYSTEM_ERR));
127        org.apache.log4j.Logger logger =
128                org.apache.log4j.Logger.getRootLogger();
129        logger.setLevel(org.apache.log4j.Level.INFO);
130        logger.getLoggerRepository().getLogger("eu.clarin").setLevel(
131                org.apache.log4j.Level.DEBUG);
132    }
133
134}
Note: See TracBrowser for help on using the repository browser.