【问题标题】:How to create ConsumerTemplate in apache camel如何在 Apache Camel 中创建 ConsumerTemplate
【发布时间】:2021-02-24 00:18:54
【问题描述】:

我有骆驼处理器,它使用 ConsumerTemplate 从目录中读取文件,如下所示

@Component
public class FileReaderProcessor implements Processor {

    @Consume
    private ConsumerTemplate consumerTemplate;

    /**
     * {@inheritDoc}
     */
    @Override
    public void process(Exchange exchange) throws Exception {
        final byte[] filecontent = consumerTemplate.receiveBody(SOURCE_FILE_COMPONENT,
                byte[].class);

    }
}

但是当我尝试下面的代码时它可以工作

final byte[] filecontent = exchange.getContext().createConsumerTemplate().receiveBody(SOURCE_FILE_COMPONENT,
                byte[].class);

问题是我需要注释 ConsumerTemplate 并在我的代码中使用它。请帮助我使用带有注释的 ConsumerTemplate

@Consume
    private ConsumerTemplate consumerTemplate;

【问题讨论】:

  • 既然它是一个带有@Component的spring注释类,那么你应该能够使用spring的@Autowired等。
  • 类似@Autowired private ConsumerTemplate consumerTemplate;

标签: apache-camel spring-camel camel-ftp


【解决方案1】:

如果您检查 ConsumerTemplate 类,您会注意到它扩展了 Service。这意味着它可以像 Spring Boot 中的任何其他组件一样自动装配,我猜你正在使用基于代码中使用的 @Component 注释。所以解决方案很简单:

@Autowired
private ConsumerTemplate consumerTemplate;

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2019-11-28
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多