Question

I have an application in Spring with is using RabbitMQ. I'm using xml based context configuration and rabbit namespace in this xml. So far it looked as this:

<rabbit:connection-factory id="rabbitConnectionFactory" host="localhost" port="5672"/>

but now I want to use JNDI variables for host and port properties. I know how to use JNDI when I have "classic" bean definitions but I have know idea how to use JNDI and rabbit namespace at the same time. I wasn't able to define any child elements of rabbit:connection element. I get an error saying that it can't have any child nodes.

Any help appreciated :)

EDIT This is how I use JNDI lookup in "standard" beans (with no special namespace)

<bean id="connector" class="com.foo.ConnectionProvider">
   <constructor-arg name="url">
        <jee:jndi-lookup expected-type="java.lang.String" jndi-name="java:comp/env/service/url"/>
   </constructor-arg>
</bean>

And in META-INF/context.xml:

<ResourceLink name="service/url" global="service/url" type="java.lang.String"/>

But as I said I don't know how to accomplish it using rabbit namespace. Have tried googling with no success..

Was it helpful?

Solution

I figured out I can use "standard" bean definition. I'm not completely happy with the solution, but at least, it works.

    <bean id="rabbitConnectionFactory"               class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
        <constructor-arg name="hostname" >
            <jee:jndi-lookup expected-type="java.lang.String" 
                              jndi-name="java:comp/env/rabbit/host"/>
        </constructor-arg>
        <constructor-arg name="port" >
            <jee:jndi-lookup expected-type="java.lang.Integer" 
                              jndi-name="java:comp/env/rabbit/port"/>
        </constructor-arg>
        <property name="username" value="guest"/>
        <property name="password" value="guest"/>
    </bean>

    <rabbit:template id="amqpTemplate" connection-factory="rabbitConnectionFactory" exchange="mcs-notifications.topic"/>
    <rabbit:admin connection-factory="rabbitConnectionFactory"/> 

OTHER TIPS

Another option is to use RMQConnectionFactory

<Resource name="jms/rabbitConnectionFactory" type="com.rabbitmq.jms.admin.RMQConnectionFactory"
        factory="org.apache.naming.factory.BeanFactory" username="guest"
        password="guest" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top