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上有没有连接

wildfox
2004-01-07 13:41
是不是你的程序先运行了发送程序,然后再运行的接收程序? 如果这样的话,你需要用 Durable Subscriptions,否则,你不能接收到你的接收程序运行以前消息生产者发送的消息。下面是java.sun.com上的一段

Creating Durable Subscriptions

To ensure that a pub/sub application receives all published messages, use PERSISTENT delivery mode for the publishers. In addition, use durable subscriptions for the subscribers.

The Session.createConsumer method creates a nondurable subscriber if a topic is specified as the destination. A nondurable subscriber can receive only messages that are published while it is active.

At the cost of higher overhead, you can use the Session.createDurableSubscriber method to create a durable subscriber. A durable subscription can have only one active subscriber at a time.

A durable subscriber registers a durable subscription with a unique identity that is retained by the JMS provider. Subsequent subscriber objects with the same identity resume the subscription in the state in which it was left by the previous subscriber. If a durable subscription has no active subscriber, the JMS provider retains the subscription's messages until they are received by the subscription or until they expire.

You establish the unique identity of a durable subscriber by setting the following:

A client ID for the connection
A topic and a subscription name for the subscriber
You set the client ID administratively for a client-specific connection factory using the Admin Console.

After using this connection factory to create the connection and the session, you call the createDurableSubscriber method with two arguments--the topic and a string that specifies the name of the subscription:

String subName = "MySub";
MessageConsumer topicSubscriber =
session.createDurableSubscriber(myTopic, subName);

The subscriber becomes active after you start the Connection or TopicConnection. Later on, you might close the subscriber:

topicSubscriber.close();

The JMS provider stores the messages sent or published to the topic, as it would store messages sent to a queue. If the program or another application calls createDurableSubscriber with the same connection factory and its client ID, the same topic, and the same subscription name, the subscription is reactivated, and the JMS provider delivers the messages that were published while the subscriber was inactive.

To delete a durable subscription, first close the subscriber, and then use the unsubscribe method, with the subscription name as the argument:

topicSubscriber.close();
session.unsubscribe("MySub");

The unsubscribe method deletes the state that the provider maintains for the subscriber.



trainking
2004-01-10 10:05
哦,原来是这样.
另外,请问如何在WebLogic的console中看topic中的消息?
谢谢大家!

trainking
2004-01-13 09:56
还有,我先运行消息接受者,再运行消息发送者,消息还是收不到啊

wildfox
2004-01-13 11:37
贴出你的BaseClient的代码,从你现在的代码看不出什么问题来