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