Handled interrupted exception
[appc.git] / appc-client / client-lib / src / main / java / org / onap / appc / client / impl / core / AbstractRequestResponseHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2019 IBM
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.client.impl.core;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 /** Abstract request response handler class, responsible for common functionality of
32  * @{@link AsyncRequestResponseHandler} and @{@link SyncRequestResponseHandler}
33  */
34 abstract class AbstractRequestResponseHandler implements RequestResponseHandler {
35
36     private final EELFLogger LOG = EELFManager.getInstance().getLogger(AbstractRequestResponseHandler.class);
37     ICoreResponseHandler businessCallback;
38     protected String corrID;
39     CoreManager coreManager;
40
41
42     AbstractRequestResponseHandler(String corrID,
43                                    ICoreResponseHandler businessCallback,
44                                    CoreManager coreManager)
45     {
46         this.businessCallback = businessCallback;
47         this.corrID = corrID;
48         this.coreManager = coreManager;
49     }
50
51     public synchronized void handleResponse(final MessageContext ctx, final String response) {
52         try {
53             coreManager.submitTask(ctx.getCorrelationID(), new Runnable() {
54                 @Override
55                 public void run() {
56                     LOG.info("handling response of corrID <" + corrID + ">" + "response " + response);
57                     if(coreManager.isExistHandler(corrID)) {
58                         runTask(response, ctx.getType());
59                     }
60
61                 }
62             });
63         } catch (InterruptedException e) {
64             Thread.currentThread().interrupt();
65             LOG.error("could not handle response <" + response + "> of corrID <" + corrID + ">", e);
66         }
67     }
68
69     /**
70      *
71      * @param response - Response
72      * @param type - Type of Response
73      */
74     abstract void runTask(String response, String type);
75
76     @Override
77     public void sendRequest(String request, String corrId, String rpcName) throws CoreException {
78         if(!coreManager.isShutdownInProgress()) {
79             coreManager.registerHandler(corrId, this);
80             coreManager.sendRequest(request, corrId, rpcName);
81             coreManager.startTimer(corrId);
82         }else{
83             throw new CoreException("Shutdown is in progress. Request will not be handled");
84         }
85     }
86
87 }