[2466] | 1 | /** |
---|
[7274] | 2 | * This software is copyright (c) 2012-2022 by |
---|
| 3 | * - Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
[2466] | 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 | * |
---|
[7274] | 12 | * @copyright Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
[2466] | 13 | * |
---|
| 14 | * @license http://www.gnu.org/licenses/gpl-3.0.txt |
---|
| 15 | * GNU General Public License v3 |
---|
| 16 | */ |
---|
[6942] | 17 | package eu.clarin.sru.client.fcs; |
---|
[2183] | 18 | |
---|
| 19 | import java.util.concurrent.CountDownLatch; |
---|
| 20 | |
---|
| 21 | import org.slf4j.Logger; |
---|
| 22 | import org.slf4j.LoggerFactory; |
---|
| 23 | |
---|
[6942] | 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; |
---|
[5743] | 33 | import eu.clarin.sru.client.fcs.ClarinFCSClientBuilder; |
---|
[5804] | 34 | import eu.clarin.sru.client.fcs.ClarinFCSEndpointDescriptionParser; |
---|
[2183] | 35 | |
---|
[2190] | 36 | |
---|
[2183] | 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 | |
---|
[5748] | 45 | SRUThreadedClient client = new ClarinFCSClientBuilder() |
---|
| 46 | .addDefaultDataViewParsers() |
---|
[5804] | 47 | .unknownDataViewAsString() |
---|
| 48 | .enableLegacySupport() |
---|
| 49 | .registerExtraResponseDataParser( |
---|
| 50 | new ClarinFCSEndpointDescriptionParser()) |
---|
[5743] | 51 | .buildThreadedClient(); |
---|
[2183] | 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 ..."); |
---|
[2388] | 65 | SRUExplainRequest request1 = |
---|
| 66 | TestUtils.makeExplainRequest(args[0]); |
---|
[2190] | 67 | client.explain(request1, new SRUCallback<SRUExplainRequest, SRUExplainResponse>() { |
---|
[2183] | 68 | @Override |
---|
[2190] | 69 | public void onSuccess(SRUExplainResponse response) { |
---|
[2388] | 70 | TestUtils.printExplainResponse(response); |
---|
[2183] | 71 | latch.countDown(); |
---|
| 72 | } |
---|
[2190] | 73 | |
---|
| 74 | @Override |
---|
| 75 | public void onError(SRUExplainRequest request, |
---|
| 76 | SRUClientException error) { |
---|
| 77 | logger.error("error while performing request", error); |
---|
| 78 | latch.countDown(); |
---|
| 79 | } |
---|
[2183] | 80 | }); |
---|
| 81 | |
---|
| 82 | logger.info("submitting 'scan' request ..."); |
---|
[2388] | 83 | SRUScanRequest request2 = |
---|
| 84 | TestUtils.makeScanRequest(args[0]); |
---|
[2190] | 85 | client.scan(request2, new SRUCallback<SRUScanRequest, SRUScanResponse>() { |
---|
[2183] | 86 | @Override |
---|
[2190] | 87 | public void onSuccess(SRUScanResponse response) { |
---|
[2388] | 88 | TestUtils.printScanResponse(response); |
---|
[2183] | 89 | latch.countDown(); |
---|
| 90 | } |
---|
[2190] | 91 | |
---|
| 92 | @Override |
---|
| 93 | public void onError(SRUScanRequest request, |
---|
| 94 | SRUClientException error) { |
---|
| 95 | logger.error("error while performing request", error); |
---|
| 96 | latch.countDown(); |
---|
| 97 | } |
---|
[2183] | 98 | }); |
---|
| 99 | |
---|
| 100 | logger.info("submitting 'searchRetrieve' request ..."); |
---|
| 101 | SRUSearchRetrieveRequest request3 = |
---|
[2388] | 102 | TestUtils.makeSearchRequest(args[0], null); |
---|
[2190] | 103 | client.searchRetrieve(request3, new SRUCallback<SRUSearchRetrieveRequest, SRUSearchRetrieveResponse>() { |
---|
[2183] | 104 | @Override |
---|
[2190] | 105 | public void onSuccess(SRUSearchRetrieveResponse response) { |
---|
[2388] | 106 | TestUtils.printSearchResponse(response); |
---|
[2183] | 107 | latch.countDown(); |
---|
| 108 | } |
---|
[2190] | 109 | |
---|
| 110 | @Override |
---|
| 111 | public void onError(SRUSearchRetrieveRequest request, |
---|
| 112 | SRUClientException error) { |
---|
| 113 | logger.error("error while performing request", error); |
---|
| 114 | latch.countDown(); |
---|
| 115 | } |
---|
[2183] | 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 | } |
---|