JMS中为何指定的Queue对象为NULL(附带问题代码)。
kofwr 2008-10-25 12:53:27
我有如下测试代码,用的ActiveMQ做的JMS Server。在Topic发送的时候,会通过setJMSReplyTo指定一个消息接受者反送的Queue消息,但是这里指定的Queue对象实例receiveQueue总是null,很是奇怪。
请各位帮忙指正问题所在。
import java.util.StringTokenizer;
import java.util.Properties;
import javax.naming.*;
import javax.jms.TopicConnectionFactory;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.Queue;
import javax.jms.QueueReceiver;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.MessageConsumer;
import org.apache.activemq.ActiveMQConnectionFactory;
public class QWholesaler implements javax.jms.MessageListener{
private javax.jms.TopicConnection tConnect = null;
private javax.jms.TopicSession tSession = null;
private javax.jms.TopicPublisher tPublisher = null;
private javax.jms.QueueConnection qConnect = null;
private javax.jms.QueueSession qSession = null;
private javax.jms.Queue receiveQueue = null;
private javax.jms.Topic hotDealsTopic = null;
private javax.jms.TemporaryTopic buyOrdersTopic = null;
public QWholesaler(String broker, String username, String password){
try{
ActiveMQConnectionFactory conFactory = new ActiveMQConnectionFactory(username, password, "tcp://localhost:61616");
tConnect = conFactory.createTopicConnection (username, password);
qConnect = conFactory.createQueueConnection (username, password);
tSession = tConnect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
qSession = qConnect.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
org.apache.activemq.command.ActiveMQTopic hotDealsTopic = new org.apache.activemq.command.ActiveMQTopic();
hotDealsTopic.setPhysicalName("Hot Deals");
tSession.createTopic("Hot Deals");
org.apache.activemq.command.ActiveMQQueue receiveQueue = new org.apache.activemq.command.ActiveMQQueue();
receiveQueue.setPhysicalName("Reply Q");
receiveQueue = (org.apache.activemq.command.ActiveMQQueue)qSession.createQueue("Reply Q");
tPublisher = tSession.createPublisher(hotDealsTopic);
QueueReceiver qReceiver = qSession.createReceiver(receiveQueue);
qReceiver.setMessageListener(this);
qConnect.start();
tConnect.start();
}catch (javax.jms.JMSException jmse){
jmse.printStackTrace(); System.exit(1);
}
}
private void publishPriceQuotes(String dealDesc, String username,
String itemDesc, float oldPrice,
float newPrice){
try{
javax.jms.StreamMessage message = tSession.createStreamMessage();
message.writeString(dealDesc);
message.writeString(itemDesc);
message.writeFloat(oldPrice);
message.writeFloat(newPrice);
message.setStringProperty("Username", username);
message.setStringProperty("itemDesc", itemDesc);
message.setJMSReplyTo(receiveQueue);
if(hotDealsTopic==null){
System.out.println("hotDealsTopic is null");
}else{
System.out.println("hotDealsTopic is NOT null");
}
if(receiveQueue==null){
System.out.println("receiveQueue is null");
}else{
System.out.println("receiveQueue is NOT null");
}
tPublisher.publish(message,javax.jms.DeliveryMode.PERSISTENT,javax.jms.Message.DEFAULT_PRIORITY,1800000);
}catch ( javax.jms.JMSException jmse ){
jmse.printStackTrace();
}
}
public void onMessage( javax.jms.Message message){
try{
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Order received - " + text + " from " + message.getJMSCorrelationID());
}catch (java.lang.Exception rte){
rte.printStackTrace();
}
}
public void exit(){
try{
tConnect.close();
qConnect.close();
}catch (javax.jms.JMSException jmse){
jmse.printStackTrace();
}
System.exit(0);
}
public static void main(String argv[]) {
String broker = "broker";
String username = "username";
String password = "password";
QWholesaler wholesaler = new QWholesaler(broker, username, password);
try{
// Read all standard input and send it as a message.
java.io.BufferedReader stdin = new java.io.BufferedReader
(new java.io.InputStreamReader( System.in ) );
System.out.println ("Enter: Item, Old Price, New Price ");
System.out.println("\ne.g. Bowling Shoes, 100.00, 55.00");
while ( true ){
String dealDesc = stdin.readLine();
if(dealDesc != null && dealDesc.length() > 0){
// parse the deal description
StringTokenizer tokenizer =
new StringTokenizer(dealDesc,",") ;
String itemDesc = tokenizer.nextToken();
String temp = tokenizer.nextToken();
float oldPrice =
Float.valueOf(temp.trim()).floatValue();
temp = tokenizer.nextToken();
float newPrice =
Float.valueOf(temp.trim()).floatValue();
wholesaler.publishPriceQuotes(dealDesc,username, itemDesc, oldPrice,newPrice);
}else{
wholesaler.exit();
}
}
}catch( java.io.IOException ioe ){
ioe.printStackTrace();
}
}
}