0
请帮忙。这让我连续两周丧生。简单的JMS示例不检索JMS消息。为什么?
我只想要一个简单的独立java客户端,它将JMS消息发送到本地主机上运行的glassfish服务器上的JMS队列。并收回它。我似乎无法完成这项工作。代码如下。随后从Eclipse工作台运行它时获得控制台输出。
消息发送时没有错误。 (请参阅Console中的最后一行)但侦听器从不检索它。任何人都可以帮忙对于试图依靠JMS的下一个人来说,这真是一个很好的例子 - 如果它有效的话。谁能提供帮助?
[注意:我使用大量的打印语句来生成下面的控制台输出,但是为了便于阅读,我将它们从代码中移除。在运行之前,我清空了glassfish服务器日志。它运行后,日志添加任何行]
代码:
public class JMSTest implements MessageListener {
public static void main(String[] args) {
JMSTest messageCenter = new JMSTest();
messageCenter.sendMessage();
}
static final Properties JNDI_PROPERTIES = new Properties() {
private static final long serialVersionUID = 1L;
{this.put ("java.naming.factory.initial", "com.sun.jndi.fscontext.RefFSContextFactory");
this.put ("java.naming.provider.url","file:///C:/glassfish4/mq/opt/java/my_broker");
}
};
String QUEUE_NAME = "jms/JMSSendToTestQueue";
MessageConsumer msgConsumer = null;
MessageProducer msgProducer = null;
ObjectMessage msg = null;
Connection connection = null;
//constructor
public JMSTest() {
try {
/*1*/ Context jndiContext = (Context) new InitialContext(JNDI_PROPERTIES);
/*2*/ ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("jms/goConnectionFactory");
/*3*/ connection = factory.createConnection();
/*4*/ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
/*5*/ Destination receiveFromDestination = (Destination) jndiContext.lookup(QUEUE_NAME);
/*6*/ this.msgConsumer = session.createConsumer(receiveFromDestination);
/*7*/ this.msgConsumer.setMessageListener(this);
/*8*/ Destination sendToDestination = (Destination) jndiContext.lookup(QUEUE_NAME);
/*9*/ msgProducer = session.createProducer(sendToDestination);
/*10*/this.msg = session.createObjectMessage();
this.msg.setObject("Hi There. I'm a Test Object.");
} catch (Exception e) {
System.out.println(" " + iAmM + "msg: " + e.getMessage());
e.printStackTrace();
}
}
public void sendMessage() {
try {
this.msgProducer.send(this.msg);
System.out.println("Message Was Sent");
} catch (JMSException e) {
System.out.println("Attempt to send message failed.");
e.printStackTrace();
}
}
public void onMessage(Message msg) {
System.out.println("TEST MESSAGE RECEIVED");
if(this.connection!=null) {
try {
this.connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
ObjectMessage objMsg = (ObjectMessage) msg;
System.out.println((String) objMsg.getObject());
} catch (JMSException e) {
e.printStackTrace();
}
}
控制台:
JMSTest.<init>() beg
Line 1: InitialContext ok:
[email protected]
Line 2: factory is not null:
Sun Java System MQ ConnectionFactory
Class: com.sun.messaging.ConnectionFactory
getVERSION(): 3.0
isReadonly(): true
getProperties():
imqOverrideJMSPriority = false
imqConsumerFlowLimit = 1000
imqOverrideJMSExpiration = false
imqAddressListIterations = 1
imqLoadMaxToServerSession = true
imqConnectionType = TCP
imqPingInterval = 30
imqSetJMSXUserID = false
imqConfiguredClientID =
imqSSLProviderClassname = com.sun.net.ssl.internal.ssl.Provider
imqJMSDeliveryMode = PERSISTENT
imqConnectionFlowLimit = 1000
imqConnectionURL = http://localhost/imq/tunnel
imqBrokerServiceName =
imqJMSPriority = 4
imqBrokerHostName = localhost
imqJMSExpiration = 0
imqAckOnProduce =
imqEnableSharedClientID = false
imqAckTimeout = 0
imqAckOnAcknowledge =
imqConsumerFlowThreshold = 50
imqDefaultPassword = guest
imqQueueBrowserMaxMessagesPerRetrieve = 1000
imqDefaultUsername = guest
imqReconnectEnabled = false
imqConnectionFlowCount = 100
imqAddressListBehavior = PRIORITY
imqReconnectAttempts = 0
imqSetJMSXAppID = false
imqConnectionHandler = com.sun.messaging.jmq.jmsclient.protocol.tcp.TCPStreamHandler
imqSetJMSXRcvTimestamp = false
imqBrokerServicePort = 0
imqDisableSetClientID = false
imqSetJMSXConsumerTXID = false
imqOverrideJMSDeliveryMode = false
imqBrokerHostPort = 7676
imqQueueBrowserRetrieveTimeout = 60000
imqSetJMSXProducerTXID = false
imqSSLIsHostTrusted = false
imqConnectionFlowLimitEnabled = false
imqReconnectInterval = 3000
imqAddressList =
imqOverrideJMSHeadersToTemporaryDestinations=false}
JMSTest.<init>() Line 3: connection is not null:
BrokerAddress=localhost:7676(60325)
ConnectionID=967799204788496640
ReconnectEnabled: false
IsConnectedToHABroker: false
JMSTest.<init>() Line 4: session is not null:
ConnectionID = 967799204788496640
SessionID = 967799204788508160
JMSTest.<init>() Line 5: receiveFromDestination not null:
Sun Java System MQ Destination
getName(): JMSSendToTestQueue
Class: com.sun.messaging.Queue
getVERSION(): 3.0
isReadonly(): false
getProperties():
imqDestinationName = JMSSendToTestQueue
imqDestinationDescription = A Description for the Destination Object}
JMSTest.<init>() Line : msgConsumer not null:
ConnectionID = 967799204788496640
SessionID = 967799204788508160
ConsumerID = 967799204788511232
DestName = JMSSendToTestQueue
JMSTest.<init>() Line 7: listener set to this:
JMSTest.<init>() Line 8: sendToDestination not null:
Sun Java System MQ Destination
getName(): JMSSendToTestQueue
Class: com.sun.messaging.Queue
getVERSION(): 3.0
isReadonly(): false
getProperties():
imqDestinationName = JMSSendToTestQueue
imqDestinationDescription = A Description for the Destination Object}
JMSTest.<init>() Line : msgProducer not null:
ConnectionID = 967799204788496640
SessionID = 967799204788508160
ProducerID = 967799204788513792
DestName = JMSSendToTestQueue
JMSTest.<init>() Line 10: ObjectMessage created.
Object is String: Hi There. I'm a Test Object.
Message Was Sent
谢谢。显然是对的。显然很尴尬。但与此同时,我尝试更新我的glassfish ...当我尝试重新配置它使用的资源(一个JDBC连接池 - 与此问题无关)时,它抛出了“java.lang.IllegalStateException:getOutputStream()”已被调用这个回应。“这看起来像一个可以治愈的野兽。但只要我再次使用它,我会确认这个工作,更新代码并点击解决的复选标记。再次感谢glb – George