当前位置: 首页 > 面试题库 >

如何让骆驼FTP按需仅获取一次

秦昂然
2023-03-14
问题内容

我是骆驼的新手。

我一直在尝试让骆驼按需通过FTP仅一次获取单个文件。我无法正常工作。这是我尝试过的。让我知道什么是最好的方法,我的代码有什么问题。

1-读取文件后发送空消息, 并在收到 空消息后 停止路由。

 from("direct:myStart")
    .routeId("routeA")
    .pollEnrich("ftp:...disconnect=true&sendEmptyMessageWhenIdle=true")
    .choice()
       .when(body().isNull()).process(RouteStopper("routeA"))
       .otherwise().to("validator:file.xsd")
    .to("somewhere")
    .end();

还有我的RouteStopper

public class RouteStopper implements Processor {

private String routeId;
private Thread stopper;

public RouteStopper(String routeId) {
    this.routeId = routeId;
}
@Override
public void process(Exchange exchange) throws Exception {
    if (stopper == null) {
        stopper = new Thread() {
            @Override
            public void run() {
                try {
                    exchange.getContext().stopRoute(routeId);
                } catch (Exception e) {
                }
            }
        };
    }
    stopper.start();
}

我第一次打电话时实际上效果很好 producerTemplate.sendBody("direct:myStart", null);

但是第二次我打电话给producerTemplate.sendBody("direct:myStart", null);我:

org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://myStart]. Exchange[Message: [Body is null]]

我想我要做的是不完全停止前进…所以我尝试了另一种方式…

2-实施PollingConsumerPollStrategy

public class FtpPollStrategy implements PollingConsumerPollStrategy {

@Override
public boolean begin(Consumer consumer, Endpoint endpoint) {
    return true;
}

@Override
public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
    try {
        consumer.stop();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception cause) throws Exception {
    return false;
}

}

在我的路线上:

from("ftp:....disconnect=true&pollStrategy=#ftpPollStrategy")

我得到的是

java.lang.IllegalArgumentException: Could not find a suitable setter for property: pollStrategy as there isn't a setter method with same type: java.lang.String nor type conversion possible: No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.spi.PollingConsumerPollStrategy with value #ftpPollStrategy

3- 如果我没有停止任何操作 (如下面的注释中所建议),则好像该路由从未停止轮询我的FTP 时没有找到文件 。我只给路线打过一次电话:

producerTemplate.sendBody("direct:myStart", null);

和路线

 from("direct:myStart")
.routeId("routeA")
.pollEnrich("ftp:...disconnect=true&sendEmptyMessageWhenIdle=true")
.to("validator:file.xsd")
.to("somewhere")
.end();

查看日志:

2015-08-05 08:55:28,942 DEBUG [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:152) TelechargementMetadonneesRouteBuilder- Not connected/logged in, connecting to: ftp://pierre@localhost:21 2015-08-05 08:55:28,943 INFO [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:156) TelechargementMetadonneesRouteBuilder- Connected and logged in to: ftp://pierre@localhost:21 2015-08-05 08:55:28,945 DEBUG [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.GenericFileConsumer:130) TelechargementMetadonneesRouteBuilder- Took 0.002 seconds to poll: 2015-08-05 08:55:28,945 DEBUG [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:121) TelechargementMetadonneesRouteBuilder- Disconnecting from: ftp://pierre@localhost:21 2015-08-05 08:55:29,446 DEBUG [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:152) TelechargementMetadonneesRouteBuilder- Not connected/logged in, connecting to: ftp://pierre@localhost:21 2015-08-05 08:55:29,447 INFO [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:156) TelechargementMetadonneesRouteBuilder- Connected and logged in to: ftp://pierre@localhost:21 2015-08-05 08:55:29,449 DEBUG [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.GenericFileConsumer:130) TelechargementMetadonneesRouteBuilder- Took 0.002 seconds to poll: 2015-08-05 08:55:29,449 DEBUG [correlation- ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:121) TelechargementMetadonneesRouteBuilder- Disconnecting from: ftp://pierre@localhost:21

任何帮助将不胜感激。


问题答案:

因此,我发现的解决方案是使用场景1,但请停止并重新启动路由。

exchange.getContext().stopRoute(routeId);
exchange.getContext().startRoute(routeId);


 类似资料:
  • 我有一个Camel/SpringBoot应用程序,它从GraphQLendpoint检索数据,将数据存储在内存数据库(2个表)中,通过运行SQL查询提取CSV文件,然后将文件上传到FTP服务器。由于将提取约350k条记录,我使用SQLs outputType=StreamList、splitter和stream:file。整个路线如下所示: 提取数据时不会出现任何问题,并使用记录创建CSV文件。但

  • 我有一个Camel 2.13.1应用程序,它使用一个通过CXF组件访问的外部web服务。我使用Spring XML route元素的startupOrder属性来确保在我设置为在启动时调用一次的路由中调用web服务的登录操作(使用计时器组件)。 当我的应用程序关闭时,我想调用web服务并执行注销操作,但我看不到一个好方法。目前我正在使用Spring DSL,并正在研究该组织。阿帕奇。骆驼spi。S

  • 我通过以下代码以编程方式创建JMS路由: 我有课: 我想在上述路由和endpoint之间交换一些信息/参数。根据我想要的参数值,选择要在这个消息侦听器容器中设置的连接工厂。 请让我知道我是否能够解释我的问题陈述。 还有其他方法可以实现这一点吗?我想在运行时构建连接工厂,路由也是如此。JmsEndpoint 中是否有任何方法可以用来了解路由 id?

  • 然后我需要做的是创建另一个FTP连接--技术上是到同一台机器,但路径不同。在我的实验中,我使用了一个带有构造URI的使用者模板来获取另一个文件(基于轮询文件的内容)。 这已经在一个高级别工作,并获取我需要的文件。谁能证实这是不是一件危险的事? 根据文档: 当然,我想要的文件确实会被检索到,而且我可以将它进一步传递到骆驼路由中,然而,当我处理FTP流文件时,我看到了字节级处理(按位计算等)的问题,我

  • 我正在使用带有Apache骆驼的Spring Boot。我正在从控制器调用路由。一旦路由完成,控制就会返回控制器。我正在VerifyLimitProcess和批准限制处理器中生成响应。如果我没有在路由中提供窃听配置,控制器会按预期检索标头和正文。但如果我在路由中引入窃听,控制器会将标头和正文接收为null。如果有人指出我需要做什么,以便我可以在选择语句中引入两个处理器的窃听配置,即VerifyLi

  • 我有一条小路线,我想使用自定义的重新传递策略来重复向endpoint发送消息,但这种行为非常奇怪。看起来,重新交付政策只是在重复一个错误。我试图将所有交换发送到路由的开头,但策略不起作用,因为每次都在创建: 我做错了什么?当错误发生时,我想以间隔重复我的请求。我的骆驼版本是2.6 日志: