且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Apache Storm bolt 中使用 Apache Camel ProducerTemplate

更新时间:2023-01-18 11:26:23

您问题的根本原因是您将 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.

  1. WebSocketBolt:将您的 producerTemplate 私有成员更改为瞬态:private瞬态 ProducerTemplate producerTemplate; 以便它不会尝试序列化(与将其放入 conf 时遇到的问题相同).
  2. WebSocketBolt:在准备方法中而不是在拓扑中初始化 producerTemplate.
  1. WebSocketBolt: Change your producerTemplate private member to be transient: private transient ProducerTemplate producerTemplate; so that it will not attempt to be serialized (same problem you have with putting it into conf).
  2. WebSocketBolt: Initialize producerTemplate inside your prepare method rather than in your topology.

像这样:

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();
    }
}