Open Kilda Java Documentation
AbstractAction.java
Go to the documentation of this file.
1 package org.openkilda.wfm;
2 
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import org.apache.storm.task.OutputCollector;
5 import org.apache.storm.tuple.Tuple;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 
12 public abstract class AbstractAction implements Runnable {
13  private final Logger logger;
14  private final IKildaBolt bolt;
15  private final Tuple tuple;
16 
17  public AbstractAction(IKildaBolt bolt, Tuple tuple) {
18  this.logger = LoggerFactory.getLogger(StatsTopology.class);
19 
20  this.bolt = bolt;
21  this.tuple = tuple;
22  }
23 
24  @Override
25  public void run() {
26  try {
27  handle();
28  } catch (Exception e) {
29  if (!handleError(e)) {
30  rollback();
31  return;
32  }
33  }
34  commit();
35  }
36 
37  protected abstract void handle()
38  throws MessageFormatException, UnsupportedActionException, JsonProcessingException;
39 
40  protected Boolean handleError(Exception e) {
41  getLogger().error("Unhandled exception", e);
42  return false;
43  }
44 
45  protected void commit() {
46  getOutputCollector().ack(tuple);
47  }
48 
49  protected void rollback() {
50  getOutputCollector().fail(tuple);
51  }
52 
53  public IKildaBolt getBolt() {
54  return bolt;
55  }
56 
57  public Tuple getTuple() {
58  return tuple;
59  }
60 
61  protected OutputCollector getOutputCollector() {
62  return bolt.getOutput();
63  }
64 
65  protected Logger getLogger() {
66  return logger;
67  }
68 }
AbstractAction(IKildaBolt bolt, Tuple tuple)
OutputCollector getOutput()
Boolean handleError(Exception e)