且构网

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

在Apache Storm螺栓中使用Apache Camel ProducerTemplate

更新时间: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.

  1. WebSocketBolt:将您的producerTemplate私有成员更改为临时成员:private transient ProducerTemplate producerTemplate;,以便它不会尝试进行序列化(将其放入conf时遇到同样的问题).
  2. WebSocketBolt:在prepare方法而不是拓扑中初始化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();
    }
}