更新时间:2023-01-18 11:34:54
问题的根本原因是您正在将ProducerTemplate添加到您的风暴配置中,并且由于无法序列化而引发了异常.如果那是您自己的课,则可以更改代码以使其起作用,但是由于这是骆驼课,所以我建议您使用其他方法.
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 transient 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();
}
}