【问题标题】:Apache Camel Unmarshal to Singleton Scoped BeanApache Camel Unmarshal 到 Singleton Scoped Bean
【发布时间】:2015-04-26 14:48:32
【问题描述】:

我使用 Spring 作为我的 Bean 注册表,我目前有一个使用 Java DSL 定义的路由,如下所示:

@Override
public void configure() throws Exception {
   from("netty:tcp://{{netty.host}}:{{netty.port}}?sync=false")
         .routeId("pbxToBean").unmarshal("castor").to("mock:result");
}

它从 Netty 端点获取 XML 编组输入,并将其解组到 PBX bean。

我在想我可以将我的结果 PBX bean 声明为单例,并让 Camel 将一个未编组的对象传递给处理器,然后处理器更新所述单例 bean。这行得通吗?

【问题讨论】:

    标签: java spring apache-camel


    【解决方案1】:

    是的,如果 casor 是绑定在注册表中的 bean,

    /**
     * <a href="http://camel.apache.org/data-format.html">DataFormat:</a>
     * Unmarshals the in body using the specified {@link DataFormat}
     * reference in the {@link org.apache.camel.spi.Registry} and sets
     * the output on the out message body.
     *
     * @param dataTypeRef  reference to a {@link DataFormat} to lookup in the registry
     * @return the builder
     */
    @SuppressWarnings("unchecked")
    public Type unmarshal(String dataTypeRef) {
        addOutput(new UnmarshalDefinition(dataTypeRef));
        return (Type) this;
    }
    

    【讨论】:

    • 所以现在,Castor 被定义为骆驼弹簧上下文 xml 中的一种数据格式。就目前而言,每个未编组的对象都会创建一个新实例,即未编组的 bean,而不是使用单例。我将如何将 Castor 从数据格式重新定义为单例 bean?
    • 对不起,现在我不确定我是否正确理解了您的第一个问题。但是camel.apache.org/castor.html 那么你可以使用 unmarshal("myCastor"),但我想你也可以使用
    • 它目前的设置与描述的完全一样,实际上。问题是,生成的 POJO 是所述 POJO 的新实例,而不是单例。
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2014-10-13
    相关资源
    最近更新 更多