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