2016-08-18 8 views
0

Ich folge this Youtube-Lernprogramm auf Java Message Service mit JBoss. Meine Codes sind dem Video gleich, aber wenn ich meine TopicConsumer und TopicProducer Anwendungen starte, enden beide und bleiben nicht am Leben, um meine Nachricht zu erhalten. Ich habe gelesen, dass setMessageListener einen neuen Thread erstellt haben, so dass die Nachricht empfangen werden sollte, auch wenn der Haupt-Thread beendet wurde, aber ich immer noch nicht die Nachricht erhalten.JMS-Consumer beendet und empfängt keine Nachricht

Ich fand heraus, dass es nicht ruft onMessage, ist es, weil TopicConsumer wurde beendet, bevor es eine Chance bekommt?

Ich habe meine JBoss 5.0 Server läuft, genau wie im Video ich TopicConsumer zuerst laufen (aber es endet nach der Druckanweisung im Gegensatz zum Video) dann TopicProduver (die auch direkt nach der Druckanweisung endet) und ich don Ich empfange meine Nachricht nicht.

Danke.

TopicConsumer.java

package jmspubsubtutorial; 

import java.util.Properties; 

import javax.jms.JMSException; 
import javax.jms.Message; 
import javax.jms.MessageListener; 
import javax.jms.TextMessage; 
import javax.jms.Topic; 
import javax.jms.TopicConnection; 
import javax.jms.TopicConnectionFactory; 
import javax.jms.TopicSession; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 

public class TopicConsumer implements MessageListener { 

    public static void main(String[] args) throws JMSException, NamingException{ 
     System.out.println("---Starting TopicConsumer---"); 
     Context context = TopicConsumer.getInitialContext(); 
     TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); 
     Topic topic = (Topic) context.lookup("topic/JMS_tutorial"); 
     TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); 
     TopicSession topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); 

     topicSession.createSubscriber(topic).setMessageListener(new TopicConsumer()); 
     topicConnection.start(); 

     System.out.println("---Exiting TopicConsumer---"); 
    } 

    @Override 
    public void onMessage(Message message) { 
     System.out.println("--- onMessage ---"); 
     try { 
      System.out.println("Incoming message: " + ((TextMessage)message).getText()); 
     } catch (JMSException e) { 
      System.out.println("onMessage failed"); 
      e.printStackTrace(); 
     } 
    } 

    public static Context getInitialContext() throws JMSException, NamingException { 
     Properties props = new Properties(); 
     props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 
     props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); 
     props.setProperty("java.naming.provider.url", "localhost:1099"); 
     Context context = new InitialContext(props); 
     return context; 
    } 
} 

TopicProducer.java

package jmspubsubtutorial; 

import javax.jms.JMSException; 
import javax.jms.TextMessage; 
import javax.jms.Topic; 
import javax.jms.TopicConnection; 
import javax.jms.TopicConnectionFactory; 
import javax.jms.TopicPublisher; 
import javax.jms.TopicSession; 
import javax.naming.Context; 
import javax.naming.NamingException; 

public class TopicProducer { 

    public static void main(String[] args) throws JMSException, NamingException{ 
     System.out.println("---Starting TopicProducer---"); 
     Context context = TopicConsumer.getInitialContext(); 
     TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); 
     Topic topic = (Topic) context.lookup("topic/JMS_tutorial"); 
     TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); 
     TopicSession topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); 
     topicConnection.start(); 
     TopicProducer topicProducer = new TopicProducer(); 
     String text = "message 1 from TopicProducer..."; 
     topicProducer.sendMessage(text, topicSession, topic); 

     System.out.println("---Exiting TopicProducer---"); 
    } 

    public void sendMessage(String text, TopicSession topicSession, Topic topic) throws JMSException { 
     System.out.println("Send Message: " + text + " " + topicSession + " " + topic); 
     TopicPublisher topicPublisher = topicSession.createPublisher(topic); 
     TextMessage textMessage = topicSession.createTextMessage(text); 
     topicPublisher.publish(textMessage); 
     topicPublisher.close(); 
    } 
} 

Antwort

0

Also das Problem ist, dass Sie auf der JMS-Bibliothek setzen Thread zu halten mindestens einen nicht-Daemon zu halten, um Ihre Anwendung, die nach dem Erstellen des Consumers und dem Zuweisen des Nachrichten-Listeners aktiv ist. In der Realität gibt es jedoch keine Garantie, dass dies der Fall ist.

Es ist wahr, dass viele JMS-Provider tatsächlich versuchen, immer einen einzigen Nicht-Daemon-Thread intern laufen zu lassen, aber davon auszugehen, dass dies immer der Fall ist, ist nicht wirklich ratsam. Sie haben anscheinend festgestellt, dass Ihr bestimmter Anbieter dies nicht für Sie erledigt. Wenn Sie also sicherstellen möchten, dass Ihre Anwendung läuft, sollten Sie dies selbst vornehmen.