JMS pub/sub运行正常但消息没有

03-12-29 trainking

我运行一个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窗口中,编译程序通过,执行消息制造者程序也没有什么错误,执行消息消费者也没有错误,但是却收不到任何消息。

请问大家,这是说明原因。谢谢!

trainking
2003-12-30 09:36

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

在DOS窗口中,编译程序通过,执行消息制造者程序也没有什么错误,执行消息消费者也没有错误,但是却收不到任何消息。

请问大家,这是说明原因。谢谢!

xfzhu2003
2003-12-30 12:01

要把MDB中的事务描述删掉

trainking
2004-01-07 10:25

我这里没有MDB,也没有事务啊?

oldma
2004-01-07 11:54

用console看看topic上有没有收到消息,server上有没有连接

2Go 1 2 下一页