Forum Discussion

stephanusts's avatar
stephanusts
Occasional Contributor
7 years ago

how to groovy script with jms websphere

Hi there,

do any of you guys have a groovy script that works with jms and Websphere MQ?

 

I'm struggling with the concept..

 

Regards

SP

  • Hi  Stephanus,

     

    Please see my comments:

    >> If I open JMS (Configure JMS) I press green arrow on the configs, I get no message so I think its working.

    You should have the message that the Connection successfully tested. 

     

     

    >> Do you have a groovy script that I can use with HermesJMS?

    The script from the Working With JMS From Groovy Scripts article works for the WebSphere provider too, you just need to change the jmsEndpoint value.

     

    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.*
    
    
    //Establishing the JMS connection
    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);
    
    // Obtaining the session and queue
    Session queueSession = jmsConnectionHolder.getSession();
    Queue queueSend = jmsConnectionHolder.getQueue( jmsConnectionHolder.getJmsEndpoint().getSend() );
    Queue queueBrowse = jmsConnectionHolder.getQueue( jmsConnectionHolder.getJmsEndpoint().getReceive() );
    
    // Performing some actions
    MessageProducer messageProducer = queueSession.createProducer( queueSend );
    TextMessage textMessageSend = queueSession.createTextMessage();
    textMessageSend.setText( "A JMS message sent from ReadyAPI.");
    messageProducer.send( textMessageSend );
    textMessageSend.setText( "Another JMS message sent from ReadyAPI.");
    messageProducer.send( textMessageSend );
    
    // Logging elements
    QueueBrowser qb = queueSession.createBrowser(queueBrowse);
    Enumeration en = qb.getEnumeration();
    while(en.hasMoreElements())
    {
        TextMessage tm = (TextMessage)en.nextElement();
        log.info tm.getText()
    }
    jmsConnectionHolder.closeAll() // Make sure to close the session and connection

     

    >> If I want to as JMS Destination to project I 'm getting an error. Check the Error attached.
    This error occurs when the session isn't properly configured in HermesJMS.

     

    >> I also added the Hermes JMS screen to show that it works with the config file.

    Can you see the queues content in HermesJMS for the configured session: https://www.screencast.com/t/xuQK0QtFlDC

     

    You can also open a new support case here: https://support.smartbear.com/message/?prod=ReadyAPI

  • Nastya_Khovrina's avatar
    Nastya_Khovrina
    SmartBear Alumni (Retired)

    Hi Stephanus,

     

    Thank you for your post! Please find the sample script which sends a message:

    import javax.jms.*
    import java.util.Properties
    import javax.naming.Context
    import javax.naming.InitialContext
    
    Properties props = new Properties();
    props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    props.setProperty(Context.PROVIDER_URL, "file:/D:/Practice/IBM WebSphere MQ/"); 
    props.setProperty("DestName", "DestName") // Destination name
    Context ctx = new InitialContext(props);
    QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx.lookup("Factory");
    Connection connection = connectionFactory.createQueueConnection("Username", "Password");
    Session queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queueSend = (Queue) ctx.lookup("DestName");
    MessageProducer messageProducer = queueSession.createProducer(queueSend);
    TextMessage textMessageSend = queueSession.createTextMessage();
    connection.start();
    			
    textMessageSend.setText("Your message text");
    messageProducer.send(textMessageSend);
    
    messageProducer.close();
    queueSession.close();
    connection.close();

     

    Please find the sample script which sends a message to one queue and receives a message from another queue(without using JNDI names):

    import com.ibm.msg.client.wmq.*;
    import com.ibm.mq.jms.*;
    import javax.jms.*;
    
    MQQueueConnectionFactory qcf = new MQQueueConnectionFactory();
    
    // Host and port settings have their usual meanings
    qcf.setHostName ("localhost");
    qcf.setPort (1414);
    
    // Queue manager and channel — the W-MQ administrator should
    //  supply these
    qcf.setQueueManager ("TestQueueManager");
    qcf.setChannel ("SYSTEM.DEF.SVRCONN");
    
    // Although there are many possible values of transport type,
    //  only 'client' and 'bindings' work in a Java client. Bindings
    //  is a kind of in-memory transport and only works when the client
    //  and the queue manager are on the same physical host. In most
    //  cases we need 'client'. 
    qcf.setTransportType (WMQConstants.WMQ_CM_BINDINGS);
    
    QueueConnection qc = qcf.createQueueConnection ();
    // QueueConnection qc = qcf.createQueueConnection ("Username","password\$");  //set credentials
    qc.start();
    
    // Create a queue and a session
        Queue q = new MQQueue ("QueueName");
        Queue q1 = new MQQueue ("QueueName1");
        QueueSession s = qc.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);
    
       // Create and send a TextMessage
        QueueSender qs = s.createSender (q);
        Message m = s.createTextMessage ("Hello, World!");
        qs.send (m);
    
      // Receive a TextMessage
        QueueReceiver qr = s.createReceiver(q1);
        qr.receive();

     

    Don't forget to copy MQ client libraries to the <ReadyAPI_Install>/bin/ext folder and change values in bold to the applicable values. Please refer to the Requirements section in the JMS Manual Configuration article for details. 

    • stephanusts's avatar
      stephanusts
      Occasional Contributor

      Hi Natsya,

      thanks for the response.

      But can you please assist with the following:

       

      I'm now getting the following error:

      java.lang.LinkageError: loader constraint violation: loader (instance of sun/misc/Launcher$AppClassLoader) previously initiated loading for a different type with name "javax/jms/Destination"

      I've changed the "Destination" a few times... still no luck

       

      I've changed the script with the following:

       

      props.setProperty(Context.PROVIDER_URL, "file:/C:/.hermes/cfg/context/");
      props.setProperty("AIX.ESB.CRTNEWLEAD.IBMI.REQUEST", "AIX.ESB.CRTNEWLEAD.IBMI.REQUEST") // Destination name
      Context ctx = new InitialContext(props);
      QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx.lookup("Factory");
      Connection connection = connectionFactory.createQueueConnection();
      Session queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      Queue queueSend = (Queue) ctx.lookup("AIX.ESB.CRTNEWLEAD.IBMI.REQUEST");

       

      The "JMS Administered Objects" file contains the following:

       

      #This file is used by the JNDI FSContext.
      #Thu Jan 04 06:29:49 CAT 2018
      TTSUAENODE01CF/RefAddr/3/Encoding=String
      TTSUAENODE01CF/RefAddr/38/Encoding=String
      TTSUAENODE02CF/RefAddr/24/Encoding=String
      TTSUAENODE02CF/RefAddr/70/Encoding=String
      TTSUAENODE01CF/RefAddr/110/Type=XMSC_WMQ_SSL_KEY_RESETCOUNT
      TTSUAENODE02CF/RefAddr/70/Content=1
      TTSUAENODE02CF/RefAddr/110/Type=XMSC_WMQ_SSL_KEY_RESETCOUNT
      TTSUAENODE01CF/RefAddr/21/Encoding=String
      AIX.ESB.CRTNEWLEAD.IBMI.REQUEST/RefAddr/8/Content=1
      ...

      • stephanusts's avatar
        stephanusts
        Occasional Contributor

        This error appears with both script samples..

         

    • Sanjana's avatar
      Sanjana
      New Contributor

      Nastya_KhovrinastephanustsOlga_T

      I have used the second code to connect to IBM MQ. But i am getting error as mentioned below when i run this through soapui.

      My code:

      import com.ibm.msg.client.wmq.*;
      import com.ibm.mq.jms.*;
      import javax.jms.*;

      //public void testMQConnectionMode() throws JMSException {
      // MQConnectionFactory qcf = new MQConnectionFactory();
      // assertThat(cf.getIntProperty(CommonConstants.WMQ_CONNECTION_MODE), is(equalTo(CommonConstants.WMQ_CM_BINDINGS)));
      // cf.setIntProperty(CommonConstants.WMQ_CONNECTION_MODE, CommonConstants.WMQ_CM_CLIENT);
      // assertThat(cf.getIntProperty(CommonConstants.WMQ_CONNECTION_MODE), is(equalTo(CommonConstants.WMQ_CM_CLIENT)));
      //}
      MQQueueConnectionFactory qcf = new MQQueueConnectionFactory();

      // Host and port settings have their usual meanings
      qcf.setHostName ("hostname");
      qcf.setPort (portno);

      // Queue manager and channel — the W-MQ administrator should
      // supply these
      qcf.setQueueManager ("Queuemanager");
      qcf.setChannel ("channelname");

      // Although there are many possible values of transport type,
      // only 'client' and 'bindings' work in a Java client. Bindings
      // is a kind of in-memory transport and only works when the client
      // and the queue manager are on the same physical host. In most
      // cases we need 'client'.
      qcf.setTransportType (WMQConstants.WMQ_CM_BINDINGS);

      QueueConnection qc = qcf.createQueueConnection ();
      // QueueConnection qc = qcf.createQueueConnection ("Username","password\$"); //set credentials
      qc.start();

      // Create a queue and a session
      Queue q = new MQQueue ("MQ");
      Queue q1 = new MQQueue ("MQ1");
      QueueSession s = qc.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);

      // Create and send a TextMessage
      QueueSender qs = s.createSender (q);
      Message m = s.createTextMessage ("Hello, World!");
      qs.send (m);

      // Receive a TextMessage
      QueueReceiver qr = s.createReceiver(q1);
      qr.receive();

       

      Error:

      Tue Oct 16 16:36:13 IST 2018:ERROR:com.ibm.msg.client.jms.DetailedJMSException: JMSFMQ6312: An exception occurred in the Java(tm) MQI. The Java(tm) MQI has thrown an exception describing the problem. See the linked exception for further information.
      com.ibm.msg.client.jms.DetailedJMSException: JMSFMQ6312: An exception occurred in the Java(tm) MQI. The Java(tm) MQI has thrown an exception describing the problem. See the linked exception for further information.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.ibm.msg.client.commonservices.j2se.NLSServices.createException(NLSServices.java:313)
      at com.ibm.msg.client.commonservices.nls.NLSServices.createException(NLSServices.java:388)
      at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createV7ProviderConnection(WMQConnectionFactory.java:7178)
      at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createProviderConnection(WMQConnectionFactory.java:6583)
      at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl.createConnection(JmsConnectionFactoryImpl.java:295)
      at com.ibm.mq.jms.MQConnectionFactory.createCommonConnection(MQConnectionFactory.java:6232)
      at com.ibm.mq.jms.MQQueueConnectionFactory.createQueueConnection(MQQueueConnectionFactory.java:115)
      at javax.jms.QueueConnectionFactory$createQueueConnection.call(Unknown Source)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
      at Script1.run(Script1.groovy:29)
      at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:90)
      at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141)
      at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
      Caused by: com.ibm.mq.jmqi.local.LocalMQ$3: CC=2;RC=2495;AMQ8598:
      -----------------------------------------------------------------------
      | Failed to load the WebSphere MQ native JNI library: 'mqjbnd'.
      |
      | The JVM attempted to load the platform native library 'mqjbnd',
      | which was mapped to the filename: 'mqjbnd.dll'.
      |
      | When attempting to load the library, the JVM reported the error
      | message:
      | 'no mqjbnd in java.library.path'
      |
      | The JVM's bit-size is: '64'
      |
      | The library path which was used to locate this library was:
      | '*** Configured java.library.path **********************************
      | "C:\Program Files\SmartBear\SoapUI-5.3.0/bin"
      | ********************************************************************'
      |
      | Check that the bit size of the JVM matches the bit size of the first
      | native library file located within this java.library.path directory
      | list.
      |
      | The native library 'mqjbnd' is used by the WebSphere MQ classes for
      | Java and WebSphere MQ classes for JMS when creating a connection to
      | the queue manager using a 'bindings' mode connection. A bindings
      | mode connection is a connection which uses the system's memory to
      | communicate with the queue manager, as opposed to a 'client' mode
      | connection which uses a TCP/IP socket.
      |
      | In order to communicate with a queue manager using a bindings mode
      | connection, the queue manager must be located on the same system as
      | the WebSphere MQ classes for Java/JMS. If this is not the case in
      | your environment, consider reconfiguring the application to utilise
      | client mode connections.
      -----------------------------------------------------------------------
      at com.ibm.mq.jmqi.local.LocalMQ.loadLib(LocalMQ.java:1125)
      at com.ibm.mq.jmqi.local.LocalMQ.access$300(LocalMQ.java:166)
      at com.ibm.mq.jmqi.local.LocalMQ$1.run(LocalMQ.java:299)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.ibm.mq.jmqi.local.LocalMQ.initialise_inner(LocalMQ.java:285)
      at com.ibm.mq.jmqi.local.LocalMQ.initialise(LocalMQ.java:250)
      at com.ibm.mq.jmqi.local.LocalMQ.<init>(LocalMQ.java:1187)
      at com.ibm.mq.jmqi.local.LocalServer.<init>(LocalServer.java:188)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.ibm.mq.jmqi.JmqiEnvironment.getInstance(JmqiEnvironment.java:707)
      at com.ibm.mq.jmqi.JmqiEnvironment.getMQI(JmqiEnvironment.java:639)
      at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createV7ProviderConnection(WMQConnectionFactory.java:7170)
      ... 15 more
      Caused by: java.lang.UnsatisfiedLinkError: no mqjbnd in java.library.path
      at java.lang.ClassLoader.loadLibrary(Unknown Source)
      at java.lang.Runtime.loadLibrary0(Unknown Source)
      at java.lang.System.loadLibrary(Unknown Source)
      at com.ibm.mq.jmqi.local.LocalMQ.loadLib(LocalMQ.java:1087)
      ... 29 more

       

      Any answer why this error is coming?