SOA / Web Services / Java

A Technology Blog

Posts Tagged ‘groovy’

Purge messages from queue using Groovy/ Java

Posted by Vivek on July 8, 2014

Purging messages is one of the common operations when working with queues.

MQI (Message Queue Interface) allows browsing/ reading the message by providing various calls (MQGET, MQOPEN etc) and options alongwith. Below is a sample code to traverse the messages in a queue and perform destructive read.

 

// Set the Get Message Options

int getOpenOpts1 = MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED

MQGetMessageOptions gmo1 = new MQGetMessageOptions();

gmo1.options = MQC.MQGMO_BROWSE_FIRST + MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_ACCEPT_TRUNCATED_MSG;

boolean msgPresent = true

while(msgPresent){

try{  

                def getMsg1 = new MQMessage();                     

                getQ1.get(getMsg1, gmo1);

                gmo1.options = MQC.MQGMO_MSG_UNDER_CURSOR    

                getQ1.get(getMsg1, gmo1);

gmo1.options = MQC.MQGMO_BROWSE_NEXT + MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_ACCEPT_TRUNCATED_MSG;                        

   }

   catch(MQException e){

if (e.completionCode == 1 && e.reasonCode == MQException.MQRC_TRUNCATED_MSG_ACCEPTED)

             {

                 // Same as expected

             }

             else

             {                 

               msgPresent = false;

             }

   }          

  }

Posted in Groovy, IBM Websphere MQ, Java/J2EE | Tagged: , , | 1 Comment »

Using SOAPUi to put/get message in/from IBM MQ

Posted by Vivek on July 8, 2014

We can use HermesJMS which provides out of the box feature to communicate with any of the messaging system. It comes with both soapUI open source and pro version. However, the drawback is message will contain JMS header and the responsibility of converting JMS to native header format lies with the developer/ tester. I have used IBM MQ as messaging system and in order to avoid any transformation from JMS to MQMD, I have used groovy to put and read messages.

Add a groovy test step to get hold of the XML message to be sent

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = groovyUtils.getXmlHolder(“AddDiscussionBoardItem#Request”)

log.info holder.getXml()

def xml = holder.getXml()

//Connect to the queue manager

// Queue used for putting message

def propIQueue = testRunner.testCase.getProperty(“inputqueue”)

//Queue used for reading message

def propOQueue = testRunner.testCase.getProperty(“outputqueue”)

 //setting hostname

def propHostname = testRunner.testCase.getProperty(“hostname”) 

MQEnvironment.@hostname = propHostname.getValue()

 //setting port

def propPort = testRunner.testCase.getProperty(“port”)

MQEnvironment.@port = Integer.parseInt(propPort.getValue())                       

//setting Channel

def propChannel = testRunner.testCase.getProperty(“channel”)

MQEnvironment.@channel = propChannel.getValue()        

//setting Queue Manager

def propQM = testRunner.testCase.getProperty(“queuemanager”)

def queueManager = new MQQueueManager(propQM.getValue())

//Put message in queue

 def putMsg = new MQMessage();

int putOpenOpts = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

def putQ = queueManager1.accessQueue(propIQueue.getValue(), putOpenOpts);

putMsg.writeString(xml);

def pmo = new MQPutMessageOptions();

putQ.put(putMsg, pmo);

putQ.close()

//Read message from queue

def getMsg = new MQMessage();

int getOpenOpts = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST;

MQGetMessageOptions gmo=new MQGetMessageOptions();

def getQ = queueManager1.accessQueue(propOQueue.getValue(), getOpenOpts);

getQ.get(getMsg, gmo);

def response = getMsg.readString(getMsg.getMessageLength())

getQ.close()

Posted in Groovy, IBM Websphere Message Broker, IBM Websphere MQ | Tagged: , , | 1 Comment »

SOAMOA – New kid in the block

Posted by Vivek on May 15, 2010

This is just an extension to my previous post (SOA Testing). Most of the java developers look to automate web service testing. If you are using SOAPUI, this is possible by writing junit test cases and then invoking SOAPUi projects from junit for a particular test case. It might be the case that you do not want to rely on any tool while testing and end up creating test cases that creates and invokes request for you. With the same intention, I was looking for a free tool and found this one – SOAMOA.

Still in its infancy stage, this tool may not be used for all your testing needs but some features look promising. The GUI is not too great. It doesn’t offer you to set preferences and does not support web services standards. For all this, you already have a free tool SOAPUI (not the pro version). The only striking feature is the ability to generate junit test cases and groovy script based upon your request. You just need to import your WSDL and create SOAP Request as you generally do in SOAPUI. 

If you have a request, for eg.

<SOAP-ENV:Envelope xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/“>
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body xmlns:web=”http://webservices.service.employee.com/“>
        <web:getEmployeeDetail>
            <arg0>
                <employeeId>2000</employeeId>
                <employeeName>Monster</employeeName>
                <company>hell techologies</company>
            </arg0>
        </web:getEmployeeDetail>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Creating Groovy Script for the request will produce the following:

/* Generated GroovyWS Script by SoaMoa, :vivek at Sat May 15 10:53:13 GMT+05:30 2010 */

import groovyx.net.ws.WSClient

def proxy = new WSClient(“file:/C:/…../WEB-INF/wsdl/EmployeeService.wsdl”, this.class.classLoader)
proxy.create()
def arg0 = proxy.create(“com.employee.service.webservices.GetEmployeeDetailRequest”)
arg0.employeeId = 2000
arg0.employeeName = ‘Monster’
arg0.company = ‘hell technologies’
def getEmployeeDetailResponse = proxy.getEmployeeDetail(arg0)

A junit class will look like:

/* Generated Testcase by SoaMoa, :vivek at Sat May 15 10:59:54 GMT+05:30 2010 */

import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPBinding;

import java.util.Iterator;

import junit.framework.TestCase;

public class GetEmployeeDetailTest extends TestCase {
public void testGetEmployeeDetail() throws Exception {
String endpointURL = “
http://myhost:myport/MyApp/services/EmployeeService“;
QName serviceName = new QName(“
http://webservices.service.Employee.com/“, “EmployeeService”);
QName portName = new QName(“
http://webservices.service.Employee.com/“, “EmployeeServiceWSPort”);
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointURL);
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
MessageFactory messagefactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage request = messagefactory.createMessage();
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPBody body = env.getBody();

body.addNamespaceDeclaration(“web”, “http://webservices.service.Employee.com/“);
    SOAPElement getEmployeeDetail = body.addChildElement(“getEmployeeDetail”, “web”);
        SOAPElement arg0 = getEmployeeDetail.addChildElement(“arg0”);
            SOAPElement employeeId = arg0.addChildElement(“employeeId”);
            employeeId.addTextNode(“2000”);
            SOAPElement employeeName = arg0.addChildElement(“employeeName”);
            appId.addTextNode(“Monster”);
            SOAPElement company = arg0.addChildElement(“company”);
            EmployeeId.addTextNode(“Hell Technologies”);           
request.saveChanges();

SOAPMessage response = dispatch.invoke(request);
SOAPBody replyBody = response.getSOAPBody();
}
}

 

Posted in SOA Testing | Tagged: , , , | Leave a Comment »