在 JNDI 查找中找不到 JMS 队列
JMS queue not found in JNDI lookup when it's there
我已经创建了一个示例设置来测试 JMS 消息队列。设置如下:
Wildfly 8.1.0.Final 带有开关站项目的服务器(我很快就会有开关站任务)。我使用以下代码片段向服务器添加了一个 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
我创建了一个绑定到该队列的开关站项目,并将通过该队列接收到的所有内容打印到标准输出。然后我创建了一个简单的客户端来通过这个队列发送消息。客户代码:
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.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever.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
整个情况的问题是,我是根据教授给我的详细教程创建的。到目前为止,其他所有部分都有效。这可能是什么问题?
您创建的队列未绑定在 java:jboss/exported
上下文中。对于 WildFly,只有 java:jboss/exported
下的条目可通过远程 JNDI 查找访问。
将 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
我已经创建了一个示例设置来测试 JMS 消息队列。设置如下:
Wildfly 8.1.0.Final 带有开关站项目的服务器(我很快就会有开关站任务)。我使用以下代码片段向服务器添加了一个 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
我创建了一个绑定到该队列的开关站项目,并将通过该队列接收到的所有内容打印到标准输出。然后我创建了一个简单的客户端来通过这个队列发送消息。客户代码:
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.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever.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
整个情况的问题是,我是根据教授给我的详细教程创建的。到目前为止,其他所有部分都有效。这可能是什么问题?
您创建的队列未绑定在 java:jboss/exported
上下文中。对于 WildFly,只有 java:jboss/exported
下的条目可通过远程 JNDI 查找访问。
将 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