Ask a Question

how to groovy script with jms websphere

SOLVED
stephanusts
Occasional Contributor

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

19 REPLIES 19
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. 


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

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
Occasional Contributor

This error appears with both script samples..

 

Nastya_Khovrina
SmartBear Alumni (Retired)

Hi Stephanus,

 

Sorry, the script may be not clear enough. Please see my configuration of WebSphere MQ attached.

 

Also, note that we have sample scripts for Working With JMS From Groovy Scripts in our documentation. But, this example is for ActiveMQ provider.


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

thanks

for prompt response 

but still the same error:

 

I've changed the script to Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
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("TTSAIXNODE01CF");
Connection connection = connectionFactory.createQueueConnection();
Session queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 

 

see my web_sphere_conf.

 

 

Nastya_Khovrina
SmartBear Alumni (Retired)

Hi Stephanus,

 

You attached the wrong file.


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

oops

 

Sorry:

Nastya_Khovrina
SmartBear Alumni (Retired)

Stephanus,

 

Thank you for the screenshot. The issue may be due to class loading issue or multiple jar having such class. What version of ReadyAPI do you use? What files do you have in the  <ReadyAPI_Install>/bin/ext folder (you can share a screenshot)?


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

Ready!API 2.2

 

I've copied all file I could get in to bin/ext with:

 

IBM WebSphere MQ Explorer

Version: 8.0.0.5

 

See attached:

cancel
Showing results for 
Search instead for 
Did you mean: