流关闭错误 - 风暴

时间:2015-06-01 11:47:18

标签: java bufferedreader filereader apache-storm

我正在尝试读取文件中的一行,在spout中,然后将其发送到螺栓,但我不断收到流关闭错误。我关闭它错了或者这里有什么问题?

public class InputSpout extends BaseRichSpout {
    private SpoutOutputCollector collector;
    public void
    declareOutputFields( OutputFieldsDeclarer declarer) {
        declarer.declare( new Fields("logfile"));
    }
    private FileReader fileReader;
    private boolean completed = false;
    private TopologyContext context;
    @Override
    public void open( Map config, TopologyContext context, SpoutOutputCollector collector) { 
        try {
            this.context = context;
            this.fileReader = new FileReader(("logfile.txt").toString());
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Error reading file "
                    + ("logfile"));
        }
        this.collector = collector;
    }
    public void nextTuple() {


        if (completed) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }
        String str;
        BufferedReader reader = new BufferedReader(fileReader);
        str = null;
        try {
            str = reader.readLine();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            while (str != null) {
                this.collector.emit(new Values(str));
                str = reader.readLine();
            }

        } catch (Exception e) {
            throw new RuntimeException("Error reading typle", e);
        } finally {
            completed = true;
        }
        try {
            reader.close();
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

这是我得到的错误:

  

java.io.IOException:Stream已关闭         at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:46)         at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:147)         在java.io.InputStreamReader.read(InputStreamReader.java:184)         在java.io.BufferedReader.fill(BufferedReader.java:154)         在java.io.BufferedReader.readLine(BufferedReader.java:317)         在java.io.BufferedReader.readLine(BufferedReader.java:382)         at myStorm.InputSpout.nextTuple(InputSpout.java:52)         at backtype.storm.daemon.executor $ fn__4654 $ fn__4669 $ fn__4698.invoke(executor.clj:565)         at backtype.storm.util $ async_loop $ fn__458.invoke(util.clj:463)         在clojure.lang.AFn.run(AFn.java:24)         在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

您正在关闭array([False, True, False, True, True, True], dtype=bool) InputStream使用的fileReader,使其无法用于后续通话。无需关闭此nextTuple - 只需在读取所有数据后关闭单个Reader