java.io.NotSerializableException:org.apache.storm.spout.SpoutOutputCollector

时间:2018-09-29 08:03:08

标签: java apache-storm

我有一个spout发出一些查询,但是我收到了SpoutOutputCollector无法序列化的错误。这似乎是不可能的。 异常消息是:

  

java.lang.RuntimeException:java.lang.RuntimeException:java.io.NotSerializableException:org.apache.storm.spout.SpoutOutputCollector

我尝试在发出之前记录日志,并且可以获取日志。但是,该任务将在日志消息后不久终止。这是我的代码。

    package tiger.server.queryreceiver;

    import static tiger.util.Util.QUERY_STREAM;

    import com.typesafe.config.Config;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    import org.apache.storm.spout.SpoutOutputCollector;
    import org.apache.storm.task.TopologyContext;
    import org.apache.storm.topology.OutputFieldsDeclarer;
    import org.apache.storm.topology.base.BaseRichSpout;
    import org.apache.storm.tuple.Fields;
    import org.apache.storm.tuple.Values;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import tiger.graph.event.EventTemplate;
    import tiger.message.QueryMessage;
    import tiger.query.Query;

    public abstract class QueryReceiver extends BaseRichSpout {
      public static final Logger logger = LoggerFactory.getLogger(QueryReceiver.class);
      private static final long serialVersionUID = 5169597660682854052L;
      private final EventTemplate template;
      private final int queryInterval;
      protected SpoutOutputCollector collector;

      public QueryReceiver(EventTemplate template, int queryInterval) {
        this.template = template;
        this.queryInterval = queryInterval;
      }

      public static QueryReceiver fromConfig(Config config, EventTemplate template) {
        //this method will reture the FinalQueryReceiver based on the config
      }

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

      @Override
      public void nextTuple() {
        List<Query> queries = fetchQueries();
        if (queries != null) {
          logger.debug("*** emit queries {}", queries);
          collector.emit(QUERY_STREAM, new Values(new QueryMessage(queries)));
        }
        try {
          TimeUnit.MILLISECONDS.sleep(queryInterval);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      public abstract List<Query> fetchQueries();
    }

和实现类

    package tiger.server.queryreceiver;

    import java.util.Arrays;
    import java.util.List;
    import tiger.graph.event.Event;
    import tiger.graph.event.EventTemplate;
    import tiger.query.Condition;
    import tiger.query.Condition.AlwaysTrueMatcher;
    import tiger.query.Condition.SeqMatcher;
    import tiger.query.LastEventValueOp;
    import tiger.query.NFABuilder;
    import tiger.query.Nfa;
    import tiger.query.Query;

    public class FinalQueryReceiver extends QueryReceiver {

      private static final long serialVersionUID = -4372885166586608338L;
      private long start;
      private final long length;
      private final long slide;
      private int numSlide;

      public FinalQueryReceiver(EventTemplate template, int queryInterval, long start, long length) {
        super(template,queryInterval);
        this.start = start;
        this.length = length;
        this.slide = 0;
        this.numSlide = 1;
      }

      public FinalQueryReceiver(EventTemplate template, int queryInterval, long start, long length, long slide, int numSlide) {
        super(template, queryInterval);
        this.start = start;
        this.length = length;
        this.slide = slide;
        this.numSlide = numSlide;
      }

      @Override
      public List<Query> fetchQueries() {
        if(numSlide > 0 || numSlide < 0) {
          Nfa nfa = new NFABuilder()
              .start(new SeqMatcher() {
                @Override
                public boolean apply(List<Event> seq, Event event) {
                  return true;
                }
              }).op("equal", new LastEventValueOp())
              .followedByKleeneEnd(Condition.alwaysTrueMatcher(), Condition.alwaysTrueMatcher())
              .op("equal", new LastEventValueOp())
              .build();

          Query query = new Query(nfa, start, start + length);
          start += slide;
          if(numSlide > 0) {
            numSlide = numSlide - 1;
          }
          return Arrays.asList(query);
        }else {
          return null;
        }
      }
    }

我们可以获取日志“发出查询”,但是工作人员将在日志之后立即死亡

一些发现后更新:   我在本地模式下运行该项目,并尝试一个一个地发出queryMessage的属性,最后,我发现在发出实例状态时,发生了相同的问题。状态代码如下:

    public abstract class State implements Serializable {

      public Condition.SeqMatcher self;
      public Condition.SeqMatcher next;
      public String op;
      public SeqOperator extractor;
      // the rest are all methods and the subclass will only override the methods. the self, next, op, and extractor attributions are all a interface which extend the Serializable.
   }

此外,我对java ObjectOutputStream对QueryMessage的序列化进行了一些测试,没有发现异常。因此,我认为这可能不是序列化问题,但真正的问题是什么?

1 个答案:

答案 0 :(得分:0)

不是SpoutOutputCollector不可序列化。 SpoutOutputCollector无法序列化您的查询。

您没有提供完整的堆栈跟踪信息,所以不能确定,但​​是我的猜测是QueryMessageQuery没有实现Serializable接口。

您可以在这里找到Java序列化的不错介绍:https://www.baeldung.com/java-serialization