title: MessageConsumer.receive() vs MessageConsumer.setMessageListener(new ClassName());
body: <p>I know the small part that setMessageListener is used when we implements MessageListener on the class.
  This below class is for reciever. Both subscribe and publish method are in same class. When i try to execute this my application won't recieve message. It is not producing message also probably because of some error in subscribe method , i am not sure.</p>
  
  <pre><code>public void subscribe(Connection topicConnection, Topic topic) throws    JMSException{        
      TopicSession subscribeSession= (TopicSession) topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);            
      topic=subscribeSession.createTopic("topic/mohit");            
      TopicSubscriber topicSubscriber=subscribeSession.createSubscriber(topic);              
      topicConnection.start();             
      Message message=topicSubscriber.receive();            
      TextMessage textmessage=(TextMessage) message;               
      System.out.println(textmessage.getText());                
  }            
  </code></pre>
  
  <p>}</p>
  
  <p>But when i have below code with class extending from MessageListener interface and instead of <strong>Message message=topicSubscriber.receive();</strong>  i use <strong>topicSubscriber.setMessageListener(new Chat());</strong> the application runs fine.Please tell me what is wrong in first implementation.</p>
  
  <pre><code>  public void subscribe(Connection topicConnection, Topic topic) throws JMSException{
      TopicSession subscribeSession= (TopicSession) topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      topic=subscribeSession.createTopic("topic/mohit");
      TopicSubscriber topicSubscriber=subscribeSession.createSubscriber(topic);
      topicSubscriber.setMessageListener(new Chat());
  }
  
  @Override
  public void onMessage(Message message) {
      try {
          System.out.println(((TextMessage) message).getText());
      } catch (JMSException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  </code></pre>