在JNDI查找中找不到JMS队列

时间:2015-04-09 18:22:33

标签: queue jms jndi lookup wildfly

我已经创建了一个示例设置来测试JMS消息队列。设置如下:

Wildfly 8.1.0。带有switchyard项目的最终服务器(我很快就会有一个switchyard任务)。我使用以下代码片段将JMS队列添加到服务器:

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

我从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

我创建了一个switchyard项目,该项目绑定到此队列,并将通过此队列接收的所有内容打印到标准输出。然后我创建了一个简单的客户端来通过这个队列发送消息。客户端代码:

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());
                }
            }
        }
    }

}

现在,在运行客户端时,出现错误:

á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

虽然队列在管理控制台中可见:

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

整个情况的问题是,我是根据教授给我的详细教程创建的。到目前为止,其他所有部这可能是什么问题?

1 个答案:

答案 0 :(得分:1)

您创建的队列未绑定在java:jboss/exported上下文中。对于WildFly,只能通过远程JNDI查找访问java:jboss/exported下的条目。

将JNDI条目java:/jms/queue/HelloQueue更改为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
相关问题