Open Kilda Java Documentation
TestAwareProducer.java
Go to the documentation of this file.
1 /* Copyright 2018 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.floodlight.kafka;
17 
23 
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 
27 // TODO(surabujin): avoid usage, replace with more correct network interrupting method
28 public class TestAwareProducer extends Producer {
29  private static final Logger logger = LoggerFactory.getLogger(TestAwareProducer.class);
30 
31  private KafkaBreakTrigger breakTrigger;
32 
33  public TestAwareProducer(KafkaProducerConfig kafkaConfig) {
34  super(kafkaConfig);
35 
37  }
38 
39  @Override
40  public void sendMessageAndTrack(String topic, Message message) {
41  if (!breakTrigger.isCommunicationEnabled()) {
42  logger.info("Suppress record : {} <= {}", topic, message);
43  return;
44  }
45  super.sendMessageAndTrack(topic, message);
46  }
47 
48  @Override
49  public SendStatus sendMessage(String topic, Message message) {
50  if (!breakTrigger.isCommunicationEnabled()) {
51  throw new IllegalArgumentException(String.format(
52  "Can't emulate behaviour of %s.sendMessage in network outage state",
53  getClass().getCanonicalName()));
54  }
55  return super.sendMessage(topic, message);
56  }
57 
59  return breakTrigger;
60  }
61 }
void sendMessageAndTrack(String topic, Message message)
TestAwareProducer(KafkaProducerConfig kafkaConfig)
SendStatus sendMessage(String topic, Message message)