如何在使用 bean 引用的 Java 中设置路由 [英] How to set up route in java that uses bean references

查看:19
本文介绍了如何在使用 bean 引用的 Java 中设置路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置以下路线:

I am trying to set up the following route:

  @Override
    public void configure() throws Exception {
        log.info("Creating ftp rout from " + config.ftpUrl().split("\\?")[0]);
        from(config.ftpUrl()
             + "&stepwise=true"
             + "&delay=1000"
             + "&move=${file:name}.trans"
             + "&recursive=true"
             + "&binary=true"
             + "&filter=#" + Beans.doneFilter.name()
             + "&maxMessagesPerPoll=200"
             + "&eagerMaxMessagesPerPoll=false"
             + "&sorter=#" + Beans.sorter.name())
                                                     .log("Downloading file ${file:name}")
                                                     .to("file:" + config.ftpTargetPostpaid())
                                                     .to("file:" + config.ftpTargetPrepaid())
                                                     .to("file:" + config.ftpTargetFonic())
                                                     //.routePolicyRef(Beans.policy.name())
                                                     .autoStartup(false)
                                                     .routeId("ftp");
    }

错误是:

找不到合适的属性设置器:过滤器,因为没有具有相同类型的 setter 方法:java.lang.String 或类型转换可能:没有可用于从类型转换的类型转换器:java.lang.String 到所需的类型:org.apache.camel.component.file.GenericFileFilter 带值#doneFilter

Could not find a suitable setter for property: filter 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.component.file.GenericFileFilter with value #doneFilter

我认为我引用绑定 bean 的方式是错误的?我以这种方式绑定所有 bean(独立应用程序):

I think the way i reference a bound bean is wrong? I bound all beans this way(stand alone app):

public class MainApp {

/**
 * A main() so we can easily run these routing rules in our IDE
 */
public static void main(String... args) throws Exception {
    Injector i = Guice.createInjector(new CepModule());
    Main main = new Main();
    bindBeans(main);
    main.enableHangupSupport();
    main.addRouteBuilder(i.getInstance(FetchFtp.class));
    main.run();

}

private static void bindBeans(Main main) {
    for (Beans bean : Beans.values()) {
        main.bind(bean.name(), bean.clazz());
    }
}

/**
 * This is the java style bean registry. Use the enums name as reference to the bean.
 * 
 *
 */
public static enum Beans{
    sorter(SortingStrategy.class),
    policy(PolicyForStartAndStopRoutes.class),
    doneFilter(ExcludeDoneFilesFilter.class);

    private final Class<?> clazz;

    Beans(Class<?> clazz){
        this.clazz = clazz;;
    }

    public Class<?> clazz(){
        return clazz;
    }
}

}

那么如何在java中配置的路由中使用beans/classes?

So how to use beans/classes in routes configured in java?

推荐答案

Camel 不仅需要 class 引用,还需要 bean 实例.

Camel expects not only class references but bean instances.

假设类有一个默认构造函数,将 bean.clazz().newInstance() 绑定到注册表而不是 bean.clazz(),i.例如:

Assuming the classes have a default constructor, bind bean.clazz().newInstance() to the registry instead of bean.clazz(), i. e.:

main.bind(bean.name(), bean.clazz().newInstance());

这篇关于如何在使用 bean 引用的 Java 中设置路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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