Apache Camel-如何使用InputStream作为源? [英] Apache Camel - how to use InputStream as source?

查看:169
本文介绍了Apache Camel-如何使用InputStream作为源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做的事情似乎很简单,或者至少是一项常见的任务.但是,我无法找到任何示例这一事实告诉我,我将以错误的方式进行操作.

What I'm trying to do seems simple enough, or at least a common task. The fact that I'm unable to find any examples, however, tells me I'm going about this the wrong way.

我有一个InputStream我需要用作来源. InputStream提供了我需要路由到一个或多个目标端点的文本行. InputStream的来源有点黑框-它不是来自文件或URL.我有最基本的流示例正在使用...使用System.in和System.out:

I have an InputStream I need to use as a source. The InputStream provides lines of text that I need to route to one or more destination endpoints. The source of the InputStream is a bit of a black box - it's not from a file or a URL. I have the most basic of stream examples working...using System.in and System.out:

public class InOutRoute extends RouteBuilder
{
    @Override
    public void configure() throws Exception
    {
        from("stream:in")
                .to("stream:out");
    }

}

我现在想要做的就是用已提供的InputStream替换stream:in.我以为这是一个常见的操作,但显然我在想这是错误的方式.

All I'm trying to do right now is replace stream:in with the InputStream I've been given. I thought this would be a common operation but clearly I'm thinking about this the wrong way.

(编辑)有关流的更多信息:我必须使用的黑匣子"执行操作,然后通过InputStream提供有关这些操作的状态更新.黑匣子完成动作后,我将放弃对InputStream的引用.一种潜在的并发症:我可能必须同时处理多个InputStreams.

(Edit) More information about the stream: This "black box" I have to work with performs actions and then provides status updates about those actions via an InputStream. When the black box has completed its actions I discard the reference to the InputStream. One potential complication: I will likely have to handle multiple InputStreams at the same time.

推荐答案

您要从输入流进行路由吗?您可以为此使用bean组件,以在bean上调用返回输入流的方法.

So you want to route from an input stream? You can use the bean component for that, to call a method on a bean that returns your input stream.

public InputStream giveItToMe() {
   ...
}


from("bean:myBean?method=giveItToMe")
  .to("stream:out");

请注意,当路由完成后,骆驼再次在bean上调用该方法(无限循环).因此,如果没有流,则阻止该调用或返回null,然后需要在路由中对其进行过滤,因为InputStream将为null.

Notice that when the route is complete, Camel calls the method on the bean again (in endless loop). So if you have no stream, then either block the call, or return null, and then you would need to filter that in the route, as then the InputStream would be null.

您还可以只使用ProducerTemplate并使用该流调用Camel路由,当您要在Camel中路由时从该流中获取该流.

You can also just use a ProducerTemplate and call a Camel route with the stream, from where you have the stream when you want to route it in Camel

public void someBusinessLogic() {
   while (!done) {
     InputStream is = ...
     template.sendBody("direct:routeMe", is);

     ...
     // logic to know if we should continue or break out
   }
}

from("direct:routeMe")
  .to("stream:out");

更多详情,

  • http://camel.apache.org/bean
  • http://camel.apache.org/direct
  • http://camel.apache.org/producertemplate.html

这篇关于Apache Camel-如何使用InputStream作为源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆