Open Kilda Java Documentation
HealthCheckBolt.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.wfm.topology.utils;
17 
18 import static org.openkilda.messaging.Utils.MAPPER;
20 
27 
28 import org.apache.storm.task.OutputCollector;
29 import org.apache.storm.task.TopologyContext;
30 import org.apache.storm.topology.OutputFieldsDeclarer;
31 import org.apache.storm.topology.base.BaseRichBolt;
32 import org.apache.storm.tuple.Tuple;
33 import org.apache.storm.tuple.Values;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 
37 import java.io.IOException;
38 import java.util.Map;
39 
40 public class HealthCheckBolt extends BaseRichBolt {
41  private static final Logger logger = LoggerFactory.getLogger(HealthCheckBolt.class);
42 
43  private final HealthCheckInfoData healthCheck;
44  private final String healthCheckTopic;
45 
46  private OutputCollector collector;
47 
48  public HealthCheckBolt(String service, String healthCheckTopic) {
49  healthCheck = new HealthCheckInfoData(service, Utils.HEALTH_CHECK_OPERATIONAL_STATUS);
50 
51  this.healthCheckTopic = healthCheckTopic;
52  }
53 
54  @Override
55  public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
56  this.collector = collector;
57  }
58 
59  @Override
60  public void execute(Tuple input) {
61  String request = input.getString(0);
62  try {
63  Message message = MAPPER.readValue(request, Message.class);
64  if (message instanceof CommandMessage) {
65  Values values = new Values(Utils.MAPPER.writeValueAsString(new InfoMessage(healthCheck,
66  System.currentTimeMillis(), message.getCorrelationId(), Destination.NORTHBOUND)));
67  collector.emit(healthCheckTopic, input, values);
68  }
69  } catch (IOException exception) {
70  logger.error("Could not deserialize message: ", request, exception);
71  } finally {
72  collector.ack(input);
73  }
74  }
75 
76  @Override
77  public void declareOutputFields(OutputFieldsDeclarer declarer) {
78  declarer.declareStream(healthCheckTopic, fieldMessage);
79  }
80 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
void declareOutputFields(OutputFieldsDeclarer declarer)
HealthCheckBolt(String service, String healthCheckTopic)
void prepare(Map stormConf, TopologyContext context, OutputCollector collector)
static final String HEALTH_CHECK_OPERATIONAL_STATUS
Definition: Utils.java:77