我已经尽可能多地寻找我的问题的解决方案。我的应用程序在 Glassfish v3 上运行。此应用程序向 Glassfish JMS 队列发送一条消息,并且该消息应该由同一主机上但在 Glassfish JVM 之外的独立客户端读取。
我已经编写了独立的客户端 java 代码 - 包括来自 Glassfish 安装目录的 appserv-rt.jar 和 gf-client.jar。
此客户端代码无法接收消息。系统输出语句打印直到“得到消费者”。之后没有任何反应。
如果我更改队列的名称 - 我收到一条错误消息,提示找不到队列。所以客户端代码似乎能够找到队列,但它没有收到任何消息。我需要在客户端代码中包含什么?
这是我的Java类:-
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Category;
import domain.RedirectFile;
public class ZblBulkUploadThread implements Runnable,MessageListener{
private static final Category log = Category.getInstance(ZblBulkUploadThread.class) ;
private Queue queue;
public void run()
{
try
{
System.out.println(" inside try") ;
InitialContext jndiContext = null;
MessageConsumer messageConsumer=null;
jndiContext = new InitialContext();
System.out.println(" got context ") ;
ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.
lookup("jms/SimpleConnectionFactory");
System.out.println("got connectionfactory") ;
Connection connection = connectionFactory.createConnection();
System.out.println("got connection") ;
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
queue = (Queue)jndiContext.lookup("jms/SimpleQueue") ;
System.out.println("got queue"+queue.getQueueName()) ;
messageConsumer = session.createConsumer(queue);
System.out.println(" selector "+messageConsumer.getMessageSelector()) ;
System.out.println("got consumer") ;
Message message = messageConsumer.receive() ;
System.out.println("Message is "+message) ;
System.out.println("destination is "+message.getJMSDestination()) ;
ObjectMessage om = ((ObjectMessage)message) ;
try
{
RedirectFile file = (RedirectFile)om.getObject() ;
log.debug("filePath "+file.getFilePath()) ;
log.debug(" userName "+file.getUserName()) ;
log.debug(" mode is "+file.getMode()) ;
System.out.println("filePath "+file.getFilePath()) ;
System.out.println(" userName "+file.getUserName()) ;
System.out.println(" mode is "+file.getMode()) ;
}
catch(Exception ex)
{
log.error("ERROR "+ex.getMessage()) ;
ex.printStackTrace() ;
}
log.debug("session created") ;
}
catch(Exception ex)
{
ex.printStackTrace() ;
log.error("Error "+ex.getMessage()) ;
}
}
public void onMessage(Message message)
{
System.out.println("Message received "+message) ;
}
public static void main(String[] args)
{
ZblBulkUploadThread zbut = new ZblBulkUploadThread() ;
new Thread(zbut).start() ;
}
}