Confusion with @Override annotation

时间:2016-04-04 17:43:18

标签: apache-storm

I am new to Apache Storm 0.9.5.I am using eclipse with maven. Java version-1.7.

Attempt 1: I am trying to do a sample code and followed the instructions given in the page

import java.util.*;
//import storm tuple packages
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;

//import Spout interface packages
import backtype.storm.topology.IRichSpout;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;

//Create a class FakeLogReaderSpout which implement IRichSpout interface 
   to access functionalities

public class FakeCallLogReaderSpout implements IRichSpout {
   //Create instance for SpoutOutputCollector which passes tuples to bolt.
   private SpoutOutputCollector collector;
   private boolean completed = false;

   //Create instance for TopologyContext which contains topology data.
   private TopologyContext context;

   //Create instance for Random class.
   private Random randomGenerator = new Random();
   private Integer idx = 0;

   @Override
   public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
      this.context = context;
      this.collector = collector;
   }

   @Override
   public void nextTuple() {
      if(this.idx <= 1000) {
         List<String> mobileNumbers = new ArrayList<String>();
         mobileNumbers.add("1234123401");
         mobileNumbers.add("1234123402");
         mobileNumbers.add("1234123403");
         mobileNumbers.add("1234123404");

         Integer localIdx = 0;
         while(localIdx++ < 100 && this.idx++ < 1000) {
            String fromMobileNumber = mobileNumbers.get(randomGenerator.nextInt(4));
            String toMobileNumber = mobileNumbers.get(randomGenerator.nextInt(4));

            while(fromMobileNumber == toMobileNumber) {
               toMobileNumber = mobileNumbers.get(randomGenerator.nextInt(4));
            }

            Integer duration = randomGenerator.nextInt(60);
            this.collector.emit(new Values(fromMobileNumber, toMobileNumber, duration));
         }
      }
   }

   @Override
   public void declareOutputFields(OutputFieldsDeclarer declarer) {
      declarer.declare(new Fields("from", "to", "duration"));
   }

   //Override all the interface methods
   @Override
   public void close() {}

   public boolean isDistributed() {
      return false;
   }

   @Override
   public void activate() {}

   @Override 
   public void deactivate() {}

   @Override
   public void ack(Object msgId) {}

   @Override
   public void fail(Object msgId) {}

   @Override
   public Map<String, Object> getComponentConfiguration() {
      return null;
   }
}

The error message I receive is as follows

The method cleanup() of type CallLogCreatorBolt must override a superclass method

And the quick fix is to

remove @Override annotation

When I removed the annotation I don’t see any error.

As we know, IRichBolt API has a method by the name cleanup() with same parameters. The idea of my class CallLogCreatorBolt is to override cleanup() present in IRichBolt class.

1. My question is what is the problem in my using @Override?

Attempt 2: I deleted all the methods inside the class CallLogCreatorBolt. Then I clicked on Add unimplemented methods option that eclipse provides.

public class CallLogCreatorBolt implements IRichBolt {

    public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        // TODO Auto-generated method stub
    }

    public void execute(Tuple input) {
        // TODO Auto-generated method stub
    }

    @Override
    public void cleanup() {
        // TODO Auto-generated method stub
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        // TODO Auto-generated method stub
    }

    public Map<String, Object> getComponentConfiguration() {
        // TODO Auto-generated method stub
        return null;
    }
}

If you notice, I have added @Override for cleanup() method alone. I get the same error as I receive in my first attempt. But other methods are fine.

Please forgive me if I am missing out something very basic! Thanks in advance.

1 个答案:

答案 0 :(得分:1)

感谢@ f1sherox

问题出在JRE版本上。 该项目在JRE1.5中执行。但理想情况下,它至少应该是1.6。

修复方法如下:

右键单击Project Explorer / Navigator面板中看到的Project - &gt;选择属性 - &gt; Java构建路径 - &gt;在JRE系统库[JAVASE-1.5]上选择 - &gt;点击编辑按钮 - &gt;选择你想要的合适的JRE版本[我选择版本1.7] - &gt;完成 - &gt;申请,好的。

我们完成了:)