当相同 class 的不同实例在总线上注册时,Guava EventBus 抛出 handlerExcetion

Guava EventBus throwing handlerExcetion when different instances of the same class are registered on the bus

我很难将相同 class 的多个实例注册到一个 Google 番石榴事件总线上。如果注册了 class 的单个实例,它运行良好,如果我尝试注册多个实例,它会抛出 handlerException。 完整错误:

Feb 24, 2015 10:37:25 PM com.google.common.eventbus.EventBus$LoggingSubscriberExceptionHandler handleException

SEVERE: Could not dispatch event: backend.modules.SingleSeriesLineChartModule[,2,25,834x485,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] to public void backend.modules.SingleSeriesLineChartModule.serialDataEventHandler(backend.serial.SerialDataEvent)

这是我注册听众的方式:

        module1 = new SingleSeriesLineChartModule("Test Graph1", moduleContainer, tabbedPane1, "AA", "Var");
        serialCommunicator.serialRegister(module1);
        module2 = new SingleSeriesLineChartModule("Test Graph1", moduleContainer, tabbedPane2, "AA", "Var");
        serialCommunicator.serialRegister(module2);
        module3 = new SingleSeriesLineChartModule("Test Graph3", moduleContainer, tabbedPane3, "AA", "Var");
        serialCommunicator.serialRegister(module3);
        module4 = new SingleSeriesLineChartModule("Test Graph4", moduleContainer, tabbedPane4, "AA", "Var");
        serialCommunicator.serialRegister(module4);

SerialCommunicator中的注册码class:

    public void serialRegister(Object registree){
        serialEventBus.register(registree);
    }

    public void serialUnregister(Object unregistree){
        serialEventBus.unregister(unregistree);
    }

serialEventBus 只是一个未经编辑的 guava eventBus

SingleSeriesLineChartModule(我知道一个很长的名字,我正在研究它):

public class SingleSeriesLineChartModule extends AbstractModule{

    private final String SERIAL_KEY;
    private final String Y_AXIS_LABEL;
    private static XYSeries series;

    public SingleSeriesLineChartModule(String title, ModuleContainer container, JTabbedPane tabbedPane, String serialKey, String yAxisLabel) {
        super(title, container, tabbedPane);
        SERIAL_KEY = serialKey;
        Y_AXIS_LABEL = yAxisLabel;

        series = new XYSeries(SERIAL_KEY, false, false);

        JFreeChart chart = createChart(new XYSeriesCollection(series));
        ChartPanel chartPanel = new ChartPanel(chart);
        this.add(chartPanel);

    }

    private JFreeChart createChart(XYDataset data){
        JFreeChart chart = ChartFactory.createXYLineChart(TITLE, "Time(s)", Y_AXIS_LABEL, data);
        XYPlot plot = (XYPlot)chart.getPlot();
        plot.setRangePannable(true);
        plot.setDomainPannable(true);
        NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        return chart;
    }

    @Override
    @Subscribe
    public void serialDataEventHandler(SerialDataEvent event) {
        if(event.getKey().equals(SERIAL_KEY)){
            series.add(System.currentTimeMillis(), event.getData());
        }
    }
}

AbstractModule 与 eventBussing 无关并扩展了 JPanel。我非常困惑,看不出有任何理由说明为什么这不起作用。如果有人有任何想法,我将不胜感激,如果您需要更多代码,我很乐意提供。

谢谢。

编辑:要创建的最后一个 SingleSeriesLineChartModules 可以正常恢复事件,其他 3 个无法恢复事件。如果我注册除了一个实例之外的所有实例似乎并不重要,它们似乎仍然被视为已注册,即如果我注册模块 1,模块 2-4 就像已注册

我找到问题了。看起来使 XYSeries 静态意味着它随后会在 class 的所有实例中携带,因此当一个新值被添加到每个实例的系列中时,在它处理队列中的第一个实例之后其他人获得相同的系列,当您尝试添加另一个与现有数据点具有相同 X 值的数据点时(您不允许这样做),它会抛出一个错误,该错误一直运行到番石榴事件.最后实际上不是番石榴问题!我所要做的就是使 XYSeries 成为非静态

抱歉浪费时间!