Ask a Question

Init JMS Topic before start of test

Said
Contributor

Init JMS Topic before start of test

Hi,

I would like to know how to clear a JMS topic before test execution. I have found code below via this link. How do I change the code to only read the JMS topics until there is nothing more on the topic?

Motivation for this: I have created a DataSource Loop test. But sometimes there are already (alot of) messages on the queue that need to be cleared, so that what I put on the topic and my test step to verify the topic are in sync.

 

Thanks in advance!

 

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

def jmsEndpoint = new  JMSEndpoint("jms://activeMQSession::queue_testQ1::queue_testQ1");
def hermes = HermesUtils.getHermes( context.testCase.testSuite.project, jmsEndpoint.sessionName)
def jmsConnectionHolder = new JMSConnectionHolder( jmsEndpoint, hermes, false, null ,null ,null);

Session queueSession = jmsConnectionHolder.getSession();
Queue queueSend = jmsConnectionHolder.getQueue( jmsConnectionHolder.getJmsEndpoint().getSend() );
Queue queueBrowse = jmsConnectionHolder.getQueue( jmsConnectionHolder.getJmsEndpoint().getReceive() );

MessageProducer messageProducer =queueSession.createProducer( queueSend );
TextMessage textMessageSend = queueSession.createTextMessage();
textMessageSend.setText( "jms message from groovy");
messageProducer.send( textMessageSend );
textMessageSend.setText( "another jms message from groovy");
messageProducer.send( textMessageSend );

QueueBrowser qb  = queueSession.createBrowser(queueBrowse);
Enumeration en= qb.getEnumeration();
while(en.hasMoreElements()){
    TextMessage tm = (TextMessage)en.nextElement();
    log.info tm.getText()
    }
jmsConnectionHolder.closeAll()// don't forget to close session and connection

 

1 REPLY 1
Olga_T
SmartBear Alumni (Retired)

Hi @Said,

 

Thanks for creating the topic. 

Community, does anyone want to accept this challenge? Smiley Wink

 


Olga Terentieva
SmartBear Assistant Community Manager

cancel
Showing results for 
Search instead for 
Did you mean: