我已经使用standalone-full.xml配置文件配置了在JBoss AS 7.1.1.FINAL上运行的JMS队列.
然后我编写了一个独立的 Java程序来连接队列并根据JBoss示例代码发送消息.
public class RemoteProducer {
private static final Logger log = Logger.getLogger(RemoteProducer.class.getName());
// Set up all the default values
private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
private static final String DEFAULT_DESTINATION = "jms/queue/test";
private static final String DEFAULT_USERNAME = "jmstest";
private static final String DEFAULT_PASSWORD = "fluppy";
private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
private static final String PROVIDER_URL = "remote://localhost:4447";
/**
* @param args
*/
public static void main(String[] args) {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
Destination destination = null;
Context context = null;
try {
// Set up the context for the JNDI lookup
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, System.getProperty("username", DEFAULT_USERNAME));
env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
context = new InitialContext(env);
// Perform the JNDI lookups
String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
connectionFactory = (ConnectionFactory) context.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) context.lookup(destinationString);
log.info("Found destination \"" + destinationString + "\" in JNDI");
// Create the JMS connection, session, producer, and consumer
connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
connection.start();
// TODO send messages
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,调用connectionFactory.createConnection失败并出现JMS异常(HornetQException[errorCode=3 message=Timed out waiting to receive cluster topology. Group:null):
日志:
Nov 06, 2012 3:37:42 PM de.test.jms.RemoteProducer main
INFO: Attempting to acquire connection factory "jms/RemoteConnectionFactory"
Nov 06, 2012 3:37:42 PM de.test.jms.RemoteProducer main
INFO: Found connection factory "jms/RemoteConnectionFactory" in JNDI
Nov 06, 2012 3:37:42 PM de.test.jms.RemoteProducer main
INFO: Attempting to acquire destination "jms/queue/test"
Nov 06, 2012 3:37:42 PM de.test.jms.RemoteProducer main
INFO: Found destination "jms/queue/test" in JNDI
javax.jms.JMSException: Failed to create session factory
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:605)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:119)
at de.test.jms.RemoteProducer.main(RemoteProducer.java:60)
Caused by: HornetQException[errorCode=3 message=Timed out waiting to receive cluster topology. Group:null]
at org.hornetq.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:804)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:601)
... 2 more
Run Code Online (Sandbox Code Playgroud)
问题:没有涉及集群,两个程序都在同一台机器上运行,我没有配置任何IP地址.有没有人知道我在JBoss配置中可能缺少什么?
任何提示欢迎:-)
我通过检查和更改项目的Maven依赖项解决了这个问题.
有了这个Maven依赖,该程序按预期工作:
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jms-client-bom</artifactId>
<version>7.1.1.Final</version>
<type>pom</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)