Open Kilda Java Documentation
MessageExchanger.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound;
17 
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 
29 public class MessageExchanger implements MessageConsumer<Message>, MessageProducer {
30 
31  private Map<String, Message> pendingResponses = new HashMap<>();
32 
33  public MessageExchanger() { }
34 
35  public MessageExchanger(Message response, String correlationId) {
36  pendingResponses.put(correlationId, response);
37  }
38 
39  @Override
40  public Message poll(String correlationId) {
41  return pendingResponses.remove(correlationId);
42  }
43 
44  @Override
45  public void clear() {
46  }
47 
48  @Override
49  public void send(String topic, Message message) {
50  final String requestId = message.getCorrelationId();
51  if (!pendingResponses.containsKey(requestId)) {
52  throw new IllegalStateException(String.format(
53  "There is no pending response for request \"%s\"", requestId));
54  }
55  }
56 
57  public void mockResponse(Message message) {
58  pendingResponses.put(message.getCorrelationId(), message);
59  }
60 
61  public void resetMockedResponses() {
62  pendingResponses.clear();
63  }
64 }
MessageExchanger(Message response, String correlationId)
void send(String topic, Message message)