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

查看:22
本文介绍了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");

注意,当路由完成时,Camel 再次调用 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");

更多详情请访问

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

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