Announcement Announcement Module
No announcement yet.
Using JNDI to configure a JndiTemplate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using JNDI to configure a JndiTemplate

    I'm using Spring to configure my JMS connection to Tibco EMS 4.4.1 for an application deployed on Tomcat.

    I have everything working now by setting the environment for the JndiTemplate using hardcoded strings for the prop entries on the environment.

    My challenge is the desire to use the Tomcat JNDI context to get the java.naming.provider.url value instead of hardcoding it or putting it in a properties file.

    This app gets deployed to two different dev and test environments before it ever gets to production, and every one has different values for the provider url; each environment already has a context set up with the provider url in the tomcat instances, so obviously, if I can set it up this way, it'll make deployment far simpler.

    I just can't seem to reason my way past the chicken and egg - how can I use JNDI to configure a JNDI context?

    My current setup is pretty standard:

    <bean id="topicConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiTemplate" ref="jndiTemplate"/>
    <property name="jndiName" value="FTTopicConnectionFactory"/>

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
    <prop key="java.naming.factory.initial">
    com.tibco.tibjms.naming.TibjmsInitialContextFactor y
    <prop key="java.naming.provider.url">tibjmsnaming://localhost:7222

    Using <jee:jndi-lookup /> doesn't work for prop values.

    If all else fails, I'll probably use PropertyPlaceholderConfigurer.

    But if somebody has a solution that works, either by adding a specific resource to the tomcat context or just looking up this one value, I'd really appreciate it.

  • #2
    any luck?

    any luck? or a reference to a howto?


    • #3

      I forgot to post my solution here.

      Here's a link to the solution that worked for me:

      The summary is I used a PropertyPlaceholderConfigurer to hold the TIBCO EMS Server URL, and then put the file in Tomcat's conf directory. Has been working exactly the way I want in all my environments.


      • #4
        Not sure if I could still be of help here, but I was looking to do something similar and created my own custom jnditemplate

        public class CustomJndiTemplate extends JndiTemplate{
        	protected Context createInitialContext() throws NamingException {
   "Lookup environment properties for datasource");
        		//Create a context to the local naming service
        	    InitialContext ctx = new InitialContext();
        	    String url = ctx.lookup("myprovider.url").toString();
        	    String factory = ctx.lookup("mycontext.factory").toString();
        	    Hashtable ht = new Hashtable();
        	    ht.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        	    ht.put(Context.PROVIDER_URL, url);
   "Creating new initial context for datasource, with url: " + url);
        	    //Create a new initial context with the jndi url for the datasource and return
        	    return new InitialContext(ht);
        Then, in my context I reference my customJndiTemplate for my datasource:

        <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        		<property name="jndiTemplate" ref="customJndiTemplate"/>
        		<property name="jndiName" value="jndiFor.jdbc"/>


        • #5


          Could you please post what did you configured in Tomcat?

          mycontext.factory - what configuration you added for this in your tomcat server.xml?

          And where did you configured myprovider.url and how?

          Please let me know.