0

I have an application deployed on tomcat which tries to work with remote JMS queues of external system A and B. External systems are deployed on Weblogic, so in order to communicate with them I am providing also wlthint3client.

That is my code:

public void myMethod () {
   // Sending message to first ext system
   // jndi queue name - topic1.extsys1.tosend.messages
   magicMethod(Params of External system A);

   // Sending message to second ext system
   // jndi queue name - topic1.extsys2.tosend.messages
   magicMethod(Params of External system B);

   // AGAIN Sending message to FIRST ext system
   // jndi queue name - topic1.extsys1.tosend.messages
   magicMethod(Params of External system A);  
}

private void magicMethod(String factoryName, String url, String connectionFactoryJNDI, String queueName) throws Exception {

    javax.jms.QueueConnectionFactory queueConnectionFactory = null;
    javax.jms.QueueSession queueSession = null;
    javax.jms.Queue queue = null;
    javax.jms.QueueSender queueSender = null;
    javax.jms.QueueConnection queueConnection = null;
    InitialContext ic = null;

    try {
        final Properties initialContextProperties = new Properties();
        initialContextProperties.put(Context.INITIAL_CONTEXT_FACTORY, factoryName);
        initialContextProperties.put(Context.PROVIDER_URL, url);
        initialContextProperties.put(Context.SECURITY_PRINCIPAL, "");
        initialContextProperties.put(Context.SECURITY_CREDENTIALS, "");
        ic = new InitialContext(initialContextProperties);
        queueConnectionFactory = (QueueConnectionFactory) ic.lookup(connectionFactoryJNDI);
        queue = (javax.jms.Queue) ic.lookup(queueName);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI context: " + e.getMessage());
    }

    try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        ObjectMessage objectMessage = queueSession.createObjectMessage("message");
        queueSender.send(objectMessage);
    } catch (JMSException e) {
        System.out.println(e.getMessage());
    } finally {
        ic.close();
        queueConnection.close();
        queueSession.close();
        queueSender.close();
    }
}

When I am sending message to ext system A - everything is ok. Sending message to ext system B - still message is sent, everything is fine.

When Trying AGAIN send message to ext system A - I am failing.

Could not create JNDI context: While trying to lookup 'topic1.extsys1.tosend.messages' didn't find subcontext 'extsys1'. Resolved 'topic1'

So I am trying understand:

  1. What I did wrong?
  2. Why it is not sending messages to system A, after I succeeded to send to ext system B?
  3. Do these internal java objects (InitialContext, JNDI objects, etc...) having some state somewhere? Maybe I need clean something?
  4. I feel like something messed up with JNDI names...
  5. Maybe I need to change names? (Actually I have already tried to have totally different jndi queue names but it had no effect, however I didn't restart external systems, don't sure if it is requeired).

Any Ideas, directions to move??? Please

ps - jms server and jms module has the same name in both A and B external systems. Provider URL's are - ext1.xxx.corp.com, ext2.xxx.corp.com

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
liotur
  • 809
  • 2
  • 17
  • 36

2 Answers2

0

I'm not exactly sure why the JNDI lookup is failing. The JNDI objects don't have any state, at least not according to the specification, but your particular implementation may have some non-spec behavior. This could also just be a bug in your JNDI implementation.

However, I can say that your code here employs a significant anti-pattern because it performs the following for every message you send:

  • JNDI lookup (likely requires a network round-trip)
  • Create a JMS connection (certainly requires a network round-trip)
  • Create a JMS session (almost certainly requires a network round-trip)
  • Create a JMS producer

This is a significant waste of resources. At the very least you should be caching the results of your JNDI lookups (which may resolve your problem) as well as your JMS connection factories. Ideally you'd use pooling for your JMS connections.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
0

This is dummy code, just to discribe situation.

Answer is here - JMSTemplate with multiple brokers. Destination resolving exception

liotur
  • 809
  • 2
  • 17
  • 36