更新时间:2023-01-18 11:21:36
您问题的根本原因是您将 ProducerTemplate 添加到您的 Storm 配置中,并且由于它不可序列化而引发异常.如果那是您自己的类,您可以更改代码以使其正常工作,但由于这是一个 Camel 类,我建议采用不同的方法.
The root cause of your problem is that you're adding ProducerTemplate to your storm config and it is throwing an exception because it isn't serializable. If that were your own class, you could change the code to make it work but since that is a Camel class I would recommend a different approach.
private瞬态 ProducerTemplate producerTemplate;
以便它不会尝试序列化(与将其放入 conf 时遇到的问题相同).private transient ProducerTemplate producerTemplate;
so that it will not attempt to be serialized (same problem you have with putting it into conf).像这样:
public class WebSocketBolt extends BaseBasicBolt {
private transient ProducerTemplate producerTemplate;
@Override
public void execute(Tuple input, BasicOutputCollector basicOutputCollector) {
Status s = (Status) input.getValueByField("tweet");
producerTemplate.sendBody("direct:main", s.getText());
}
@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
}
@Override
public void prepare(Map stormConf, TopologyContext context) {
super.prepare(stormConf, context);
CamelContext producerTemplate = new RouteStarter().buildRoute();
this.producerTemplate = producerTemplate.createProducerTemplate();
}
}