Gábor Major
Gábor Major

Reputation: 484

JMS queue not found in JNDI lookup when it's there

I have created a sample setup to test JMS message queues. The setup is as follows:

Wildfly 8.1.0.Final server with switchyard projects (I'll have a switchyard assignment soon). I added a JMS queue to the server using the following code snippet:

if (outcome != success) of /subsystem=messaging/hornetq-server=default/jms-queue=HelloQueue:read-resource
    jms-queue add --queue-address=HelloQueue --entries=java:/jms/queue/HelloQueue, java:jboss/exported/jms/queue/HelloQueue
end-if

I received the following response from Wildfly:

20:13:36,647 INFO  [org.jboss.as.messaging] (ServerService Thread Pool -- 59) JBAS011601: Bound messaging object to jndi name java:/jms/queue/HelloQueue

I created a switchyard project which is bound to this queue, and prints everything received through this queue to the standard output. Then I created a simple client to send messages through this queue. The client code:

package soi.syhello.jms.client;

import java.util.Properties;
import java.util.logging.Logger;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class HelloJmsClient {

    private static final Logger log = Logger.getLogger(HelloJmsClient.class.getName());
    private static final String DEFAULT_MESSAGE = "Alice & Bob";
    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "java:/jms/queue/HelloQueue";
    private static final String DEFAULT_USERNAME = "guest";
    private static final String DEFAULT_PASSWORD = "guest";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "http-remoting://localhost:18080";

    public static void main(String[] args) {
        Context namingContext = null;
        try {
            try {
                String userName = System.getProperty("username", DEFAULT_USERNAME);
                String password = System.getProperty("password", DEFAULT_PASSWORD);

                final Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
                env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
                env.put(Context.SECURITY_PRINCIPAL, userName);
                env.put(Context.SECURITY_CREDENTIALS, password);
                namingContext = new InitialContext(env);

                String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
                log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
                ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
                log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");

                String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
                log.info("Attempting to acquire destination \"" + destinationString + "\"");
                Destination destination = (Destination) namingContext.lookup(destinationString);
                log.info("Found destination \"" + destinationString + "\" in JNDI");

                String content = System.getProperty("message.content", DEFAULT_MESSAGE);
                JMSContext context = connectionFactory.createContext(userName, password);
                log.info("Sending message with content: " + content);
                context.createProducer().send(destination, content);
                context.close();
            } catch (Exception e) {
                e.printStackTrace();
                log.severe(e.getMessage());
            }
        } finally {
            if (namingContext != null) {
                try {
                    namingContext.close();
                } catch (NamingException e) {
                    log.severe(e.getMessage());
                }
            }
        }
    }

}

Now, when running the client, I get an error:

ápr. 09, 2015 8:16:24 DU org.xnio.Xnio <clinit>
INFO: XNIO version 3.2.2.Final
ápr. 09, 2015 8:16:24 DU org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.2.2.Final
ápr. 09, 2015 8:16:24 DU org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.3.Final
ápr. 09, 2015 8:16:25 DU soi.syhello.jms.client.HelloJmsClient main
INFO: Attempting to acquire connection factory "jms/RemoteConnectionFactory"
ápr. 09, 2015 8:16:26 DU soi.syhello.jms.client.HelloJmsClient main
INFO: Found connection factory "jms/RemoteConnectionFactory" in JNDI
ápr. 09, 2015 8:16:26 DU soi.syhello.jms.client.HelloJmsClient main
INFO: Attempting to acquire destination "java:/jms/queue/HelloQueue"
javax.naming.NameNotFoundException: jms/queue/HelloQueue -- service jboss.naming.context.java.jboss.exported.jms.queue.HelloQueue
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:104)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:202)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179)
    at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
    at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
ápr. 09, 2015 8:16:26 DU soi.syhello.jms.client.HelloJmsClient main
SEVERE: jms/queue/HelloQueue -- service jboss.naming.context.java.jboss.exported.jms.queue.HelloQueue

Although the queue is visible in the admin console:

http://kepfeltoltes.hu/150409/1142213920K_pkiv_g_s_www.kepfeltoltes.hu_.png

The problem with this whole situation is, I created this from a detailed tutorial given to me by my professor. Every other part worked so far. What could be the problem here?

Upvotes: 3

Views: 16506

Answers (1)

Aparna Chaudhary
Aparna Chaudhary

Reputation: 1335

The queue you created is not bound under java:jboss/exported context. In case of WildFly, only entries under java:jboss/exported are accessible through remote JNDI lookup.

Change the JNDI entry java:/jms/queue/HelloQueue to queue/HelloQueue.

if (outcome != success) of /subsystem=messaging/hornetq-server=default/jms-queue=HelloQueue:read-resource
    jms-queue add --queue-address=HelloQueue --entries=queue/HelloQueue,java:jboss/exported/jms/queue/HelloQueue
end-if

Upvotes: 3

Related Questions