Forum Discussion

MatthiasBuck's avatar
MatthiasBuck
Occasional Contributor
12 years ago

[Resolved] Groovy Script - Clear JMS Messages from Queue

Hi all,

I'm trying to create a script to clear all messages from one or more queues.

I found a code example online http://josies.blogspot.com/2012/03/delete-messages-from-queues-in-groovy.html but it contains a bunch of issues. This script however, serves as my basis since it's better than starting from scratch.

The purpose of clearing messages from a queue is to run it as a setup of a test case that works with JMS messages. Having old JMS messages on a queue will create assertion failures since its likely not what the test case expects. Clearing messages will solve this problem.

The problem with below code is that I'm still unclear as to how I get a handle on certain objects. Unfortunately the Java Doc API http://www.soapui.org/apidocs/ doesn't contain quite enough information to work with.

Currently I'm stuck with following line of code:
def hermes = HermesUtils.getHermes(testSuite.getProject(), "sessionName")
http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/submit/transports/jms/util/HermesUtils.html

I don't know what sessionName should be. Once that's clear, I'm sure there are going to be additional challenges with the script.

Does anybody have better documentation on how to use the Classes and feed the methods?
Or some additional ideas to get this working?

Here is my current version of the script:

import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSConnectionHolder
import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSEndpoint
import com.eviware.soapui.impl.wsdl.submit.transports.jms.util.HermesUtils
import hermes.Hermes
import javax.jms.*

// ${endpoint} is set in global properties to session created in Hermes ie Tibco-INT
def endpoint = context.expand('${endpoint}')
List queues = ["MYQUEUE"];

queues.each{
def queueName = "${it}"
def jmsEndPoint = new JMSEndpoint("jms://" + endpoint + "::-::queue_" + queueName);

def hermes = HermesUtils.getHermes(testSuite.getProject(), "sessionName")
def jmsConnectionHolder = new JMSConnectionHolder(jmsEndPoint,hermes,false,null,null,null);

log.info("endpoint:" + endpoint);
log.info("queueName:" + queueName);
//log.info("jmsEndPoint:" + "jms://" + endpoint + "::-::queue_" + queueName");

Session session = jmsConnectionHolder.getSession();
Queue queue = jmsConnectionHolder.getQueue(jmsConnectionHolder.getJMSEndpoint().getReceive());

MessageConsumer consumer = session.createConsumer(queue);
Message message = null;
while((message = consumer.receiveNoWait()) != null){
//do nothing
}
jmsConnectionHolder.closeAll()

*/
}


Thanks much for your help!
  • MatthiasBuck's avatar
    MatthiasBuck
    Occasional Contributor
    Ok, I played around with this a bit more and got it functioning (without a very detailed java docs api)

    Here is my working example:


    import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSConnectionHolder
    import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSEndpoint
    import com.eviware.soapui.impl.wsdl.submit.transports.jms.util.HermesUtils
    import hermes.Hermes
    import javax.jms.*

    // This is the name of the session from your hermes-config.xml. Search for 'session'. It also shows in the URL and in my case it's the Queue Manager name.
    def hermesSssionName = "MySessionName";

    List queues = ["MyQueueName1, MyQueueName2"];

    queues.each{
    def queueName = "${it}"
    def jmsEndPoint = new JMSEndpoint("jms://" + hermesSssionName + "::-::queue_" + queueName);
    def hermes = HermesUtils.getHermes(testSuite.getProject(), jmsEndPoint.getSessionName())
    def jmsConnectionHolder = new JMSConnectionHolder(jmsEndPoint,hermes,false,null,null,null);

    log.info("hermesSssionName:" + hermesSssionName);
    log.info("queueName:" + queueName);

    Session session = jmsConnectionHolder.getSession();
    Queue queue = jmsConnectionHolder.getQueue(jmsConnectionHolder.getJmsEndpoint().getReceive());

    MessageConsumer consumer = session.createConsumer(queue);
    Message message = null;
    while((message = consumer.receiveNoWait()) != null){
    log.info(message);
    //do nothing
    }
    jmsConnectionHolder.closeAll()

    }
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hey,

    I'm glad to hear you managed to resolve your issue. Thanks for providing a working example!

    Regards,

    Arian
    SmartBear Sweden
  • larryx's avatar
    larryx
    Frequent Contributor
    Hello
    I use Matthias' groovy sample code to clear JMS Message from queue (SoapUI Pro-HermesJMS). However, I got "groovy.lang.MissingPropertyException" error (attached) which complain the line "queues.each{". Could anyone have idea? Please Help Me!! Thanks.
    Note: I used groovy to send messages to the Queue already. Below is my code:

    import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSConnectionHolder
    import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSEndpoint
    import com.eviware.soapui.impl.wsdl.submit.transports.jms.util.HermesUtils
    import hermes.Hermes
    import javax.jms.*

    // This is the name of the session from your hermes-config.xml. Search for 'session'. It also shows in the URL and in my case it's the Queue Manager name.
    def hermesSssionName = "activeMQSession";

    //List queues = ["MyQueueName1, MyQueueName2"];
    List queues = ["testQ1, testQ1"];

    queues.each{
    def queueName = "${it}"
    def jmsEndPoint = new JMSEndpoint("jms://" + hermesSssionName + "::queue_" + queueName + "::queue_" + queueName);
    def hermes = HermesUtils.getHermes(testSuite.getProject(), jmsEndPoint.getSessionName())
    def jmsConnectionHolder = new JMSConnectionHolder(jmsEndPoint,hermes,false,null,null,null);

    log.info("hermesSssionName:" + hermesSssionName);
    log.info("queueName:" + queueName);

    Session session = jmsConnectionHolder.getSession();
    Queue queue = jmsConnectionHolder.getQueue(jmsConnectionHolder.getJmsEndpoint().getReceive());

    MessageConsumer consumer = session.createConsumer(queue);
    Message message = null;
    while((message = consumer.receiveNoWait()) != null){
    log.info(message);
    //do nothing
    }
    jmsConnectionHolder.closeAll()
    }