我正在尝试从 GlassFish (2.1) 服务器远程接收 JMS 消息。我已按照此处的说明进行操作: http: //www.tidytutorials.com/2009/06/jms-example-using-glassfish-and-remote.html以及此处:http ://www.novell.com/documentation/ extend52/Docs/help/MP/jms/tutorial/pointToPoint-1.htm 但它似乎不起作用。
我创建了 QueueConnectionFactory sampleFactory 和 Queue sampleQueue 用于测试。
我使用以下代码发送消息:
HashMap<String,List<String>> values = getValues();
InitialContext context = new InitialContext();
QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("sampleFactory");
Queue queue = (Queue) context.lookup("sampleQueue");
queueConnection = factory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
ObjectMessage message = queueSession.createObjectMessage();
message.setObject(values);
queueSender.send(message);
我的客户正在使用以下代码:
class JMSListener implements Runnable, MessageListener {
....
public void run() {
try {
System.out.println("Started JMS Listener");
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory");
props.put(Context.PROVIDER_URL,"iiop://192.168.38.164:3700");
Context ctx = new InitialContext(props);
Queue queue = (Queue) ctx.lookup("sampleQueue");
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx.lookup("sampleFactory");
QueueConnection queueConnection = connectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
queueReceiver.setMessageListener(new JMSListener());
while (true) {
if(!Thread.currentThread().equals(runner)) break;
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(JMSListenerRemote.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("CLOSING!");
// Don't leak
queueConnection.close();
} catch (NamingException ex) {
Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
} catch (JMSException ex) {
Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void onMessage(Message message) {
System.out.println("Got a message!");
try {
ObjectMessage obj = (ObjectMessage) message;
HashMap<String, List<String>> values = (HashMap<String, List<String>>) obj.getObject();
for (Entry<String, List<String>> entry : values.entrySet()) {
System.out.println("Got key: "+entry.getKey());
}
} catch (JMSException ex) {
Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
它似乎能够正确连接,因为我添加了一些日志记录以查看队列和工厂是否都正常,并且它们是。如果我更改队列或工厂的名称,我会收到错误消息,因此可以按预期工作。
但是,实际上无法接收消息是行不通的。他们似乎可以发送出去,但他们永远不会到达客户那里。
有没有人对可能出了什么问题有任何想法?
谢谢!
编辑:哦,我正在使用 GlassFish 的内置 JMS 提供程序。