Apache Camel解封装到单例作用域Bean

3

我正在使用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声明为Singleton,并让Camel传递一个未组合的对象给处理器,然后更新该单例bean。这样行吗?

1个回答

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 在 camel-spring 上下文 XML 中被定义为一种数据格式。就目前而言,每个非编组对象都会创建一个新的非编组 Bean 实例,而不是使用单例。我该如何将 Castor 从数据格式重新定义为单例 Bean? - Omar Darwish
抱歉,我现在不确定我是否正确理解了你第一个问题。但是请参考 http://camel.apache.org/castor.html 中的<dataFormats><castor id="myCastor"/></dataFormats>然后您就可以使用unmarshal("myCastor"),但我想您也可以这样做:<bean id="myCastor" class="org.apache.camel.dataformat.castor.CastorDataFormat" />。 - J2B
它目前的设置与描述完全一致。问题在于,生成的POJO是一个新的POJO实例,而不是单例。 - Omar Darwish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接