Kodi
Kodi

Reputation: 787

Accessing a remote JNDI in Spring

I was wondering how one would go about getting an object from a remote JNDI in Spring 3. Where do you specify the URL, how do you set it all up in a context file, etc. I have found some disperate information suggesting this is possible, but no singular source for how to do it for a JNDi that is on a different server.

Upvotes: 3

Views: 4125

Answers (2)

Bernard Hauzeur
Bernard Hauzeur

Reputation: 2403

expanding on the above with an example for connecting to a remote activeMQ server in JBoss EAP7 using CAMEL Jms component.

You will need these 3 beans in your Spring XML application context:

<bean id="remoteQCF" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="${factoryJndiName}" />
  <property name="jndiEnvironment">
    <props>
      <prop key="java.naming.provider.url">http-remoting://${remoteHost}:${remotePort}</prop>
      <prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</prop>
      <!-- other key=values here <prop key="java.naming.factory.url.pkgs">yourPackagePrefixesGoHere</prop> -->
    </props>
  </property>
</bean>
<bean id="remoteQCFproxy"
    class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
  <property name="targetConnectionFactory" ref="remoteQCF" />
  <property name="username" value="${username}" />
  <property name="password" value="${password}" />
</bean>
<bean id="jmsremote" class="org.apache.camel.component.jms.JmsComponent">
  <property name="connectionFactory" ref="remoteQCFproxy" />
</bean>

where each ${xxx} denotes a value that you shall supply in place or with a property placeholder in your application context.

If you do not need a user and password to open a JMS queue connection, you may omit the second bean and reference directly the first bean as connectionFactory in the Camel JmsComponent bean.

The 'jmsremote' bean can then be used in CAML URI's like "jmsremote:queue:myQueue1"

Upvotes: 2

Bogdan
Bogdan

Reputation: 24590

You could use, for example, the JndiObjectFactoryBean class within a basic configuration like this one:

<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="yourLookupNameGoesHere" />
        <property name="jndiEnvironment">
            <props>
                <prop key="java.naming.provider.url">yourRemoteServerGoesHere:PortGoesHere</prop>
                <prop key="java.naming.factory.initial">yourNamingContextFactoryGoesHere</prop>
                <prop key="java.naming.factory.url.pkgs">yourPackagePrefixesGoHere</prop>
                <!-- other key=values here -->
            </props>
        </property>
        <!-- other properties here-->
    </bean>

You can then specify other environment properties as needed and you can also simplify your configuration by using the Spring jee schema.

Upvotes: 3

Related Questions