0

I am consuming Tibco JMS (EMS) messages from a queue... I want to clear the queue each time the application runs. I can think of the below logic... I thought their might be a better way

public void clearMessages() throws JMSException{

        Message msg = (Message) queueReceiver.receiveNoWait();
        while(msg != null)
        {
            clearMessages();
        }

        return;
    }
Achilles
  • 711
  • 2
  • 13
  • 35
  • 1
    Don't use recursivity... in the case you have thousands of messages in the queue... the delay could be worse and the stability of the system could suffer. (Worst case, your application could crash). Put the receiveNoWait in the while instead... – GhislainCote Jun 03 '14 at 15:25

1 Answers1

1

Option 1: you acknowledge each message individually; this approach, however, may take some time, if you have (many) thousands of messages enqueued:

public void clearMessages() throws JMSException{
    Message message = null;
    do {
        message = consumer.receiveNoWait();
     if (message != null) message.acknowledge();
    }
    while (message != null);
}

Option 2: using the TibjmsAdmin Object purging a JMS destination is done like this (click TIBCO EMS Admin Java API for JavaDoc):

public void clearMessages(String queueName) throws TibjmsAdminException, TibjmsAdminInvalidNameException{
    TibjmsAdmin jmsAdmin = new TibjmsAdmin("tcp://localhost:7222", "admin", "admin");
    jmsAdmin.purgeQueue(queueName);
    // alternatively purge all queues:
    // jmsAdmin.purgeQueues(">");
}

HTH,

Hendrik

hsiegeln
  • 116
  • 4
  • I am new to tibco EMS and know only using tibco jsm activities like jms queue sender,jms topic publisher etc.I have seen many of your answers and you give answers java codes.Can you please tell me how will I use java codes in tibco EMS.For example where will i use the above(this answers) java ems code? – SpringLearner Jun 04 '14 at 03:47
  • I think you are referring to how to use Java inside TIBCO BusinessWorks. Please take a look at the Java Code Activity of TIBCO BW (https://docs.tibco.com/pub/activematrix_businessworks/5.12.0/doc/html/tib_bw_palette_reference/wwhelp/wwhimpl/common/html/wwhelp.htm#href=palref.5.079.htm&single=true) – hsiegeln Jun 04 '14 at 08:17
  • Thanks for replying,This is my stacoverflow chat room http://chat.stackoverflow.com/rooms/44929/java-spring-hibernate-php-jquery-javascript-beginners can you come there to discuss about it – SpringLearner Jun 04 '14 at 08:19