关于Jms

03-10-09 easyinfonet

weblogic.jms.common.InvalidClientIDException: Client id, BibleJMSClient, is in use

BibleJMSClient 是我定义的client id

运行AsyncQueueReceiver 进行接受可以收到,队列中的消息

但是不关闭AsyncQueueReceiver 运行发送程序 BibleQueueSender 出现上面的错误

package bible.jms;

import javax.jms.*;
import javax.naming.*;
import java.util.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class BibleQueueSender {
  public BibleQueueSender() {
  }

public static void main(String args[]) {
    Context ctx= null;
    Hashtable ht= new Hashtable();
    QueueConnectionFactory qfactory=null;
    QueueConnection qconn =null;
    QueueSession qsession=null;
    QueueSender qsender=null;
    Queue q=null;
    TextMessage textMsg=null;
    String msg;
    try {
      ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
      ctx=new InitialContext(ht);
      qfactory=(QueueConnectionFactory)ctx.lookup("BibleJMSFactory");
      qconn =qfactory.createQueueConnection();
      qsession=qconn.createQueueSession(false,javax.jms.QueueSession.AUTO_ACKNOWLEDGE);
      q=(Queue) ctx.lookup("BibleJMSQueue");

      qsender=qsession.createSender(q);
      System.out.println("dddddddd...");
      textMsg=qsession.createTextMessage();

      for (int i=0;i<100;i++) {
        msg="Message # " + i;
        textMsg.clearBody();
        textMsg.setIntProperty("serverity",i);
        textMsg.setText(msg);
        //System.out.println("Sending Message: "+ msg);
        qsender.send(textMsg);
      }

      //textMsg.setText("stop");
      //qsender.send(textMsg);
      System.out.println("finished...");

    }catch (Exception e){
      e.printStackTrace();
    }
    finally {
      try {
      qsender.close();
      qsession.close();
      qconn.close();
      }catch (Exception e){
        e.printStackTrace();
      }
    }
}

}

package bible.jms;

import javax.jms.*;
import javax.naming.*;
import java.util.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class AsyncQueueReceiver implements MessageListener {

  private boolean finished =false;
  private  Context ctx= null;
  private Hashtable ht= new Hashtable();
  private QueueConnectionFactory qfactory=null;
  private QueueConnection qconn =null;
  private QueueSession qsession=null;
  private QueueReceiver qreceiver=null;
  private Queue q=null;


public void init() {
    try {
    ht= new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
      ctx=new InitialContext(ht);
      qfactory=(QueueConnectionFactory)ctx.lookup("BibleJMSFactory");
      qconn =qfactory.createQueueConnection();
      qsession=qconn.createQueueSession(false,javax.jms.QueueSession.AUTO_ACKNOWLEDGE);
      q=(Queue) ctx.lookup("BibleJMSQueue");

      qreceiver= qsession.createReceiver(q);
      qreceiver.setMessageListener(this);
      qconn.start();
    } catch (Exception e){
      e.printStackTrace();
    }
  }


public void close() throws JMSException
  {
    try {
      qreceiver.close();
      qsession.close();
      qconn.close();
    }catch (Exception  e){
      e.printStackTrace();
    }

  }


  public AsyncQueueReceiver() {
  }
  public void onMessage(Message message) {
    if (message instanceof TextMessage){
      try {
        TextMessage textMessage=(TextMessage)message;
        String msg= textMessage.getText();
        System.out.println("Received Message :"+ msg);


        if (msg.equals("stop")) {
          synchronized (this) {
            finished=true;
            this.notifyAll();
          }
        }
      }catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

public static void main(String args[]) {
    try {
      AsyncQueueReceiver qr= new AsyncQueueReceiver();
      qr.init();
      System.out.println("aaaaaaaaa...");

     synchronized (qr) {
       while(!qr.finished) {
         try {
           qr.wait();
         }
         catch (InterruptedException ie){}
       }
       qr.close();
     }
    }catch (Exception e){
      e.printStackTrace();
    }
  }
}

1
weelee
2003-10-11 03:37

clientid 只是针对订阅/发布方式的,在一般队列方式不可用.

在weblogic中将队列设置中的clientid 去掉即可.