Java 简单JMS示例不检索JMS消息。为什么?

Java 简单JMS示例不检索JMS消息。为什么?,java,jms,Java,Jms,请帮忙。这让我痛苦了两周 我只需要一个简单的独立java客户机,它将JMS消息发送到本地主机上运行的glassfish服务器上的JMS队列。然后把它收回来。我就是不能让这一切顺利。代码如下。然后是从Eclipse工作台运行时得到的控制台输出 消息发送时没有错误。(请参阅控制台中的最后一行)但侦听器从未检索到它。有人能帮忙吗。对于下一个尝试精益JMS的人来说,这确实是一个很好的例子——如果它曾经奏效的话。有人能帮忙吗 [注意:我使用大量的print语句生成下面的控制台输出,但为了可读性,我从代码中

请帮忙。这让我痛苦了两周

我只需要一个简单的独立java客户机,它将JMS消息发送到本地主机上运行的glassfish服务器上的JMS队列。然后把它收回来。我就是不能让这一切顺利。代码如下。然后是从Eclipse工作台运行时得到的控制台输出

消息发送时没有错误。(请参阅控制台中的最后一行)但侦听器从未检索到它。有人能帮忙吗。对于下一个尝试精益JMS的人来说,这确实是一个很好的例子——如果它曾经奏效的话。有人能帮忙吗

[注意:我使用大量的print语句生成下面的控制台输出,但为了可读性,我从代码中删除了它们。在运行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: 
            javax.naming.InitialContext@3b9a45b3

    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
JMSTest。()乞求
第1行:InitialContext确定:
javax.naming。InitialContext@3b9a45b3
第2行:工厂不为空:
Sun Java System MQ连接工厂
类别:com.sun.messaging.ConnectionFactory
getVERSION():3.0
isReadonly():true
getProperties():
imqoveridejmspriority=false
imqConsumerFlowLimit=1000
IMQOVERRIDEJMESOPTIATION=false
imqAddressListIterations=1
imqLoadMaxToServerSession=true
imqConnectionType=TCP
imqpininterval=30
imqSetJMSXUserID=false
IMQConfigueredClientId=
imqSSLProviderClassname=com.sun.net.ssl.internal.ssl.Provider
imqJMSDeliveryMode=持久
imqConnectionFlowLimit=1000
imqConnectionURL=http://localhost/imq/tunnel
IMQBrokerService名称=
imqJMSPriority=4
imqBrokerHostName=localhost
imqJMSExpiration=0
imqackonproduct=
imqEnableSharedClientID=false
imqAckTimeout=0
IMQAcknowledge=
imqConsumerFlowThreshold=50
imqDefaultPassword=guest
imqQueueBrowserMaxMessagesPerRetrieve=1000
imqDefaultUsername=guest
imqReconnectEnabled=false
imqConnectionFlowCount=100
imqAddressListBehavior=优先级
imqReconnectAttempts=0
imqSetJMSXAppID=false
imqConnectionHandler=com.sun.messaging.jmq.jmsclient.protocol.tcp.TCPStreamHandler
imqSetJMSXRcvTimestamp=false
imqBrokerServicePort=0
imqDisableSetClientID=false
IMQSetJMSXSConsumerTxid=false
imqOverrideJMSDeliveryMode=false
imqBrokerHostPort=7676
imqQueueBrowserRetrieveTimeout=60000
imqSetJMSXProducerTXID=false
imqsslslsysttrusted=false
imqConnectionFlowLimitEnabled=false
imqReconnectionInterval=3000
imqAddressList=
imqOverrideJMSHeadersToTemporaryDestinations=false}
杰姆斯特斯特。()第3行:连接不为空:
BrokerAddress=localhost:7676(60325)
ConnectionID=967799204788496640
已启用重新连接:false
已连接到广播机:false
杰姆斯特斯特。()第4行:会话不为空:
ConnectionID=967799204788496640
会话ID=967799204788508160
杰姆斯特斯特。()第5行:receiveFromDestination非空:
Sun Java系统MQ目标
getName():JMSSendToTestQueue
类别:com.sun.messaging.Queue
getVERSION():3.0
isReadonly():false
getProperties():
imqDestinationName=JMSSendToTestQueue
imqDestinationDescription=目标对象的描述}
杰姆斯特斯特。()行:msgConsumer非空:
ConnectionID=967799204788496640
会话ID=967799204788508160
ConsumerID=967799204788511232
DestName=JMSSendToTestQueue
杰姆斯特斯特。()第7行:侦听器设置为:
杰姆斯特斯特。()第8行:sendToDestination非空:
Sun Java系统MQ目标
getName():JMSSendToTestQueue
类别:com.sun.messaging.Queue
getVERSION():3.0
isReadonly():false
getProperties():
imqDestinationName=JMSSendToTestQueue
imqDestinationDescription=目标对象的描述}
杰姆斯特斯特。()行:msgProducer非空:
ConnectionID=967799204788496640
会话ID=967799204788508160
ProducerID=967799204788513792
DestName=JMSSendToTestQueue
杰姆斯特斯特。()第10行:已创建ObjectMessage。
对象是字符串:您好。我是一个测试对象。
消息已发送
您需要在连接上调用start(),以便MessageConsumer能够调度message start。您可以在不启动连接的情况下发送消息,但在连接完成之前,您无法接收任何内容