JMS pub/sub執行正常但訊息沒有

trainking發表於2003-12-29
我執行一個JMS pub/sub的例子
訊息製造者程式如下:

package myjms;
import javax.jms.*;
import javax.naming.*;
public final class MessageProducer extends BaseClient {
  private TopicPublisher publisher;
  private TextMessage msg;
  public MessageProducer(String [] argv)
    throws NamingException, JMSException
  {
    super(argv);
    Context ctx = getInitialContext();
    TopicConnectionFactory tConFactory = (TopicConnectionFactory)
      ctx.lookup("weblogic.jms.ConnectionFactory"); 
    Topic messageTopic = (Topic) ctx.lookup("MessageTopic");
    TopicConnection tCon = tConFactory.createTopicConnection();
    TopicSession session = tCon.createTopicSession(
      false, Session.AUTO_ACKNOWLEDGE 
    );

    publisher = session.createPublisher(messageTopic);
    msg = session.createTextMessage();

  }

  public void runClient() 
    throws JMSException
  {
    msg.setText("Hello");
    publisher.publish(msg);
    
    msg.setText("Welcome to JMS");
    publisher.publish(msg);
    
    msg.setText("This is my first JMS");
    publisher.publish(msg);

  }

  public static void main(String [] argv) 
    throws Exception
  {
    try{
    	MessageProducer mp = new MessageProducer(argv);
	System.out.println("OK");
    	mp.runClient();
    	System.out.println("OK2");
    }
    catch (NamingException ne) {
      System.err.println("");
      System.err.println("** Please ensure that you have setup the"+
        " JMS Server properly.  The JMS Server and the appropriate "+
        "JMS Destinations must be configured before running the "+
        "examples.");
      System.err.println("");
      throw ne;
    }
  }
}

非同步訊息消費者程式如下:

package myjms;
import javax.jms.*;
import javax.naming.*;
public final class AsyncMessageConsumer 
  extends BaseClient 
  implements MessageListener
{ private int EXPECTED_MESSAGE_COUNT = 2;
  private int messageCount = 0;

  private TopicSubscriber subscriber;
  private TextMessage msg;

  public AsyncMessageConsumer(String [] argv) 
    throws JMSException, NamingException
  {
    super(argv);
    Context ctx = getInitialContext();
    TopicConnectionFactory tConFactory = (TopicConnectionFactory)
      ctx.lookup("weblogic.jms.ConnectionFactory");       
    Topic messageTopic = (Topic) ctx.lookup ("MessageTopic");      
    TopicConnection tCon = tConFactory.createTopicConnection();
    TopicSession session = tCon.createTopicSession(
      false, Session.AUTO_ACKNOWLEDGE );    
    subscriber = session.createSubscriber(messageTopic);
    subscriber.setMessageListener(this);    
    tCon.start();
    
  }

  public boolean expectMoreMessages() { 
    return messageCount < EXPECTED_MESSAGE_COUNT;
  }

  public void onMessage(Message m) {
  	
   try {
      TextMessage msg = (TextMessage) m;      
      System.err.println("Received: "+msg.getText());

    } catch (JMSException e) {
      e.printStackTrace();
    } 

    messageCount++;
  }



  public static void main(String [] argv) 
    throws Exception
  {
    int MAX_TRIES = 10;
    int tryCount = 0;
	
    AsyncMessageConsumer consumer = new AsyncMessageConsumer(argv);

    while (consumer.expectMoreMessages() && (tryCount < MAX_TRIES)) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ignore) {}

      tryCount++;
    }
    
  }

}


WebLogic中配置stores和server,其中JMS Destination的JNDI Name為:MessageTopic

在DOS視窗中,編譯程式透過,執行訊息製造者程式也沒有什麼錯誤,執行訊息消費者也沒有錯誤,但是卻收不到任何訊息。

請問大家,這是說明原因。謝謝!

相關文章