如何让Spring接线我JmsComponent [英] How to get Spring to wire my JmsComponent

查看:264
本文介绍了如何让Spring接线我JmsComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用阿卡,阿卡,骆驼弹簧配置应用程序和。该应用程序需要作为针对各种应用服务器独立JMS客户端,它需要建立使用JNDI JMS连接工厂到结束。我与JBoss测试此。我有JBOSS 5和6(这似乎是一个客户端弹簧的问题,不相关JBOSS)

I am writing an application using Akka, Akka-Camel and Spring for configuration. The application needs to act as a standalone JMS client against a variety of application servers, to which end it needs to set up the JMS connection factory using JNDI. I'm testing this with jBoss. I have the same problem with jBoss 5 and 6 (this seems to be a client-side Spring problem, not related to jBoss).

我与该XML配置的Spring bean:

I am configuring the Spring beans with this xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
         ">

    <camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/spring">
        <jmxAgent id="agent" disabled="true"/>
    </camelContext>

    <jee:jndi-lookup id="jmsConnectionFactory" jndi-name="ConnectionFactory">
        <jee:environment>
            java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
            java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
            java.naming.provider.url=jnp://192.168.0.109:1099
        </jee:environment>
    </jee:jndi-lookup>

    <bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>

</beans>

正如你可以看到我设置:

As you can see I'm setting up:


  • 通过JNDI初始化一个连接工厂,名为jmsConnectionFactory

  • 与connectionFactory的属性的JmsComponent设置为previous豆

使用这个配置我的应用程序在启动时失败,这样的:

With this configuration my app fails at startup with this:

java.lang.IllegalArgumentException: connectionFactory must be specified
    at org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:294) ~[camel-core.jar:2.10.4]
    at org.apache.camel.component.jms.JmsConfiguration.createConnectionFactory(JmsConfiguration.java:1053) ~[camel-jms.jar:2.10.4]
    at org.apache.camel.component.jms.JmsConfiguration.getConnectionFactory(JmsConfiguration.java:416) ~[camel-jms.jar:2.10.4]
    at org.apache.camel.component.jms.JmsConfiguration.createListenerConnectionFactory(JmsConfiguration.java:1062) ~[camel-jms.jar:2.10.4]
    at org.apache.camel.component.jms.JmsConfiguration.getListenerConnectionFactory(JmsConfiguration.java:435) ~[camel-jms.jar:2.10.4]
    at org.apache.camel.component.jms.JmsConfiguration.configureMessageListenerContainer(JmsConfiguration.java:889) ~[camel-jms.jar:2.10.4]
    at org.apache.camel.component.jms.JmsConfiguration.createMessageListenerContainer(JmsConfiguration.java:379) ~[camel-jms.jar:2.10.4]

这来自于JmsConfiguration.java这个code:

This comes from this code in JmsConfiguration.java:

protected ConnectionFactory createConnectionFactory() {
    ObjectHelper.notNull(connectionFactory, "connectionFactory");
    return null;
}

所以它看起来像Spring bean的初始化未能/丝豆这里指示(从pviously粘贴完整的XML Spring配置$ P $中提取)关联:

So it looks like the Spring beans initialization is failing to associate / wire the beans as instructed here (extracted from the full XML Spring configuration previously pasted):

    <bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>

我也试图建立一个中间JmsConfiguration豆,并设置JmsComponent的配置属性,而不是直接设置的ConnectionFactory属性,但我得到了同样的结果在这两个设置。

I have also tried to create an intermediate JmsConfiguration bean, and setting the configuration property of the JmsComponent, instead of setting the connectionFactory property directly, but I get the same result in both setups.

顺便说一句,我可以通过电汇code好吗豆。我的意思是,这:

By the way, I can wire the beans by code alright. I mean that this:

val connFactory = springContext.getBean[javax.jms.ConnectionFactory]("jmsConnectionFactory", classOf[javax.jms.ConnectionFactory])
camelContext.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connFactory))

工作完全正常。所以我知道我正在从JNDI的ConnectionFactory,它只是我不能击中右Spring配置文件来连线它的XML。

works perfectly fine. So I know that I am getting the ConnectionFactory from JNDI, it's just that I can't hit the right Spring configuration to wire it in XML.

我需要这个应用程序能够无需重新编译,所以获得XML工作非常配置是必须的我。

I need this application to be very configurable without recompiling, so getting the XML to work is a must for me.

在情况下,它是不明确的,问题是:如何让Spring来建立我JmsComponent豆,其connectionFactory的设置为JNDI获得的工厂

In case it's not clear, the question is: How do I get Spring to set up my JmsComponent bean, with its connectionFactory set to the JNDI-obtained factory?

编辑:使用骆驼的一点是,它应该让我掉这个组件甚至一个又一个不同的类型。所以今天我使用JMS,也许明天我将使用TCP。这就是为什么将能够确定在XML中的一切是重要的。

The point of using Camel is that it should allow me to swap this component even for another of a different type. So today I'm using JMS, maybe tomorrow I'll be using TCP. This is why it would be important to be able to define everything in XML.

推荐答案

我认为问题是,阿卡使用它自己的CamelContext,而不是在Spring配置中定义的。在早期版本的阿卡似乎有可能到通过设置阿卡使用的上下文,但不再出现在最新的版本就可以了。

I believe the problem is that Akka uses it's own CamelContext rather than the one defined in the Spring configuration. In earlier versions of Akka it seemed it was possible to set the context used by Akka, but that no longer appears to be possible in the latest versions.

我也遇到同样的问题,我使用基于注解的配置,在Java中(而非斯卡拉),并使用该code围绕这一问题已经得到:

I have encountered the same issue, I use annotation based configuration, in Java (rather than Scala), and have got around the issue using this code:

@Bean
public ActorSystem getCamelActorSystem(ConnectionFactory factory) {

    ActorSystem system = ActorSystem.create("some-system");

    Camel camel = CamelExtension.get(system);
    CamelContext camelContext = camel.context();
    camelContext.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(factory));

    return system;
}

本注入别处定义的连接工厂的依赖,并使用该给JMS组件添加到由阿卡使用的camelContext。

This injects a ConnectionFactory dependency defined elsewhere and uses this to add the jms component to the camelContext used by Akka.

另一个解决方案可以是在某种方式扩展CamelExtension code键允许camelContext依赖性被注入,因为是previously可能。不过,我假设他们有充分的理由的变化,所以我只留下它。我觉得是这样他们就可以确保上下文不能改变这样演员系统总是使用相同的骆驼的背景下,这是基于以下几点:

Another solution may be to extend the CamelExtension code in some way to allow the camelContext dependency to be injected, as was previously possible. However, I am assuming they had good reason for the change, and so am leaving it alone. I think it is so they can ensure the context cannot be changed so the Actor System always uses the same Camel context, this is based on the following:

一CamelExtension只装载一次为每一位ActorSystem,
  这使得它的安全致电CamelExtension在任何时候你
  code键到达与它相关的Apache的骆驼对象。有
  1 CamelContext并为每一位ActorSystem 1 ProducerTemplate
  一个使用CamelExtension

One CamelExtension is only loaded once for every one ActorSystem, which makes it safe to call the CamelExtension at any point in your code to get to the Apache Camel objects associated with it. There is one CamelContext and one ProducerTemplate for every one ActorSystem that uses a CamelExtension.

http://doc.akka.io/docs/阿卡/电流/斯卡拉/ camel.html#CamelExtension

这篇关于如何让Spring接线我JmsComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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