Ibm Wmq - Xms .Net - Read and delete message from Queue - ibm-mq

Greetings of the day.
Please help on the below requirement:
Requirement:
We want to delete message from MQ only after it is processed successfully.
Use event based message detection technique and avoid loop
So, to achieve above:
I have created message listener and consumer class below:
{
sessionIn = connectionIn.CreateSession(false, AcknowledgeMode.ClientAcknowledge);
// Create message listener and assign it to consumer
messageListener = new MessageListener(OnMessageCallback);
consumerAsync.MessageListener = messageListener;
Console.WriteLine("Message Listener set. Starting the connection now.");
// Start the connection to receive messages.
connectionWMQ.Start();
}
Reading the message from the call back event and push the message into other system:
OnMessageCallback(Message) {
if (xmsMessage is IBytesMessage)
{
IBytesMessage bytesMessage = (IBytesMessage)xmsMessage;
byte[] arrayMessage = new byte[bytesMessage.BodyLength];
bytesMessage.ReadBytes(arrayMessage);
string message = System.Text.Encoding.Default.GetString(arrayMessage);
}
}
Once the message processed, external system will fire the below over ride method:
Response method override:
protected override Task OnResponse(ReponseMessage message)
{
 //Read the message and get the message id and correlation id.
//Delete the message from the queue.
//I am trying to do like this, but Its not working:
messageConsumerDelete = sessionDelete.CreateConsumer(destinationDelete, query);
if (messageConsumerDelete != null)
{
IMessage m = messageConsumerDelete.Receive(1000);
LogWrite("Receive Message=" + m);
m.Acknowledge();
}
}
Please suggest a best solution for this requirement.
I am trying to find a solution for this since weeks, but no breakthrough. 
Thanks,
Balaji

Related

Differences in total message length between spring JMS and native IBM MQ libraries

I send a simple text message to an MQ Queue (MQ 7.0.1):
"abc"
Using spring JMS the total length of the message is: 291
But putting the same message in the queue using IBM MQ libraries the total length of the message is: 3
How can I get total data length 3 with JMS?
Spring JMS code:
#EnableJms
public class JMSTestController {
...
#Autowired
private JmsTemplate jmsTemplate;
#Autowired
JmsMessagingTemplate jmsMessagingTemplate;
...
public String send() throws JMSException{
jmsTemplate.setReceiveTimeout(10000);
jmsMessagingTemplate.setJmsTemplate(jmsTemplate);
Session session = jmsMessagingTemplate.getConnectionFactory().createConnection()
.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue entryQueue = session.createQueue("hereQueueName");
Queue replyQueue = session.createQueue("hereReplyQueueName");
TextMessage message = session.createTextMessage("abc");
message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
message.setJMSDestination(entryQueue);
message.setIntProperty(WMQConstants.JMS_IBM_CHARACTER_SET, 819);
message.setIntProperty(WMQConstants.JMS_IBM_ENCODING, 273);
jmsMessagingTemplate.convertAndSend(entryQueue, message);
String messageId = message.getJMSMessageID();
...
}
Native code:
MQQueueManager qm = createQueueManager(queueManager, host, port,
channel, username, password, connectionType);
MQQueue m_receiver = null;
MQMessage msg = new MQMessage();
msg.format = MQC.MQFMT_STRING;
msg.expiry = timeout / 1000;
msg.replyToQueueName = qReceiver;
msg.replyToQueueManagerName = queueManager;
msg.write("abc".getBytes());
MQPutMessageOptions pmo = new MQPutMessageOptions();
try {
qm.put(qSender, msg, pmo);
} catch (MQException e) {
MQTalkerException ex = new MQTalkerException(
"An error happened sending a message", e);
logger.error(ex);
throw ex;
}
Solution
Following JoshMc's comment I made the following modification and reached the expected result:
Check out these answers, you want to set targetClient to MQ to remove
those properties. There are many ways to accomplish this, changing
your CreateQueue to use a URI is probably the easiest.
JMS transport v/s MQ transport
That is, modify the creation of the queue using the URI instead of just its name.
Queue entryQueue = session.createQueue("queue:///QUEUE_NAME?targetClient=1");
I reached the solution by following JoshMc's comment. That is, modify the creation of the queue using the URI instead of just its name.
Queue entryQueue = session.createQueue("queue:///QUEUE_NAME?targetClient=1");
This removes the MQRFH2 header (the extra bytes I didn't know where they came from)
and with that the message has a total length of 3 bytes.
Spring is counting the bytes of the message body (aka data)
IBM MQ native is counting the bytes of the message headers plus body
In your screenshot, the field directly above shows '3' bytes.
Longitud dataos = length of body = 3
Longitud total = length of headers + body = 291

why jms Client acknowledge is not working

I am trying to retrieve messages from an ActiveMQ queue. I set the session to Session.CLIENT_ACKNOWLEDGE. When I receive the message from the server it is auto-acknowledged although I have not called acknowledge on the message. Below is sample code of what I am doing:
connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination destination = session.createQueue(queue);
consumer = session.createConsumer(destination);
connection.start();
Message message = consumer.receive(1);
The code snippet above works and I do get the message from the queue. The problem is that messages disappear from the queue although I have not acknowledged the message. Any pointers why this is happening. I have been debugging this issue almost all this afternoon with no luck. This is a Spring boot application.
I got the client_ack to work in jmsTemplate. Below is the code.
public Object retrieve() {
return this.jmsTemplate.execute(session -> {
session.recover();
MessageConsumer consumer = session.createConsumer(
this.jmsTemplate.getDestinationResolver().resolveDestinationName(session, queue, false));
try {
Message received = consumer.receive(1);
if (received != null) {
return this.messageConverter.fromMessage(received);
}
}
catch (Exception e) {
return null;
}
finally {
consumer.close();
}
return null;
}, true);
}
The issue I am now seeing is that my app does not see these messages after it restarts. I tried running session.recover() but it is not making any difference. Thanks for your help.
The working code using jmsTemplate is as below:
public Object retrieve() {
return this.jmsTemplate.execute(session -> {
MessageConsumer consumer = session.createConsumer(
this.jmsTemplate.getDestinationResolver().resolveDestinationName(session, queue, false));
try {
Message received = consumer.receive(1);
session.recover();
if (received != null) {
return this.messageConverter.fromMessage(received);
}
}
catch (Exception e) {
return null;
}
finally {
consumer.close();
}
return null;
}, true);
}
This appears to be a problem with the springframework Constants class. It uses introspection to get the integer value of CLIENT_ACKNOWLEDGE but always return a value of 1, so session is always AUTO_ACKNOWLEDGE
To anybody who might run into this issue, I did the following to resolve the issue:
Set the session to non-transacted. This is the default and only option if you are using jmsTemplate. If you are creating your own consumer, you need to create the session as non-transacted when creating the jms connection
If you are creating your own consumer, you need to set the session to CLIENT_ACKNOWLEDGE.
Check the maximum number of redeliveries to the required value. This will differ depending on the product: ActiveMQ, IBM MQ, etc.
call session.recover() after retrieving the message.
I updated the working code that is using jmsTemplate in the original post.

How to consume message from RabbitMQ dead letter queue one by one

The requirement is like to process the messages from dead letter queue by exposed a REST service API(Spring Boot).
So that once REST service is called, one message will be consumed from the DL queue and will publish in the main queue again for processing.
#RabbitListener(queues = "QUEUE_NAME") consumes the message immediately which is not required as per the scenario. The message only has to be consumed by the REST service API.
Any suggestion or solution?
I do not think RabbitListener will help here.
However you could implement this behaviour manually.
Spring Boot automatically creates RabbitMq connection factory so you could use it. When http call is made just read single message from the queue manually, you could use basic.get to synchronously get just one message:
#Autowire
private ConnectionFactory factory
void readSingleMessage() {
Connection connection = null;
Channel channel = null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
GetResponse response = channel.basicGet(QUEUE_NAME, true);
if (response != null) {
//Do something with the message
}
} finally {
//Check if not null
channel.close();
connection.close();
}
}
If you are using Spring; you can avoid all the boilerplate in the other answer using RabbitTemplate.receive(...).
EDIT
To manually ack/reject the message, use the execute method instead.
template.execute(channel -> {
GetResponse got = channel.basicGet("foo", false);
// ...
channel.basicAck(got.getEnvelope().getDeliveryTag(), false);
return null;
});
It's a bit lower level, but again, most of the boilerplate is taken care of for you.

Receive method in JMS waiting for messages

I want a method to browse all messages from a messsage queue and can send it to another queue using jmstemplate with using Websphere queues(NOT MQ). I have tried using receive and it is able to retrieve all the messages from the queue but it is still waiting for another message. And the messages are being lost. It must be in a transaction
The Code I have Tried:
**String message = (String) jmsTemplate.receiveAndConvert();
System.out.print(message);
while ((message = (String) jmsTemplate.receiveAndConvert()) != null) {
messages.add(message);
}
return messages;
}**
The JMStemplate should be used for only synchronous read or sending message. For asychronous read use one of the listener implementation. Read here

Finding XMIT Queue Depth using WebSphere MQ Classes for .Net

I want to get the Queue Depth for a Transmission Queue (XMIT queue) using WebSphere MQ Classes for .Net , can someone kindly help me giving a specific link/Pseudocode or .Net Classes/API to identify the XMIT queue depth. I have gone through the .Net API but didn't find any info on XMIT queue.
You can use the MQ .NET PCF interface to query queue attributes. Below is the sample code snippet.
Note: MQ .NET PCF interface is undocumented interface and may not be supported. You will need to consult IBM.
public static void InquireQueue()
{
PCFMessageAgent messageAgent = null;
try
{
// Create connection to queue manager
messageAgent = new PCFMessageAgent("QM3");
// Build Inquire command to query attributes a queue
PCFMessage pcfMsg = new PCFMessage(MQC.MQCMD_INQUIRE_Q);
pcfMsg.AddParameter(MQC.MQCA_Q_NAME, "TO.QM2");
// Send request and receive response
PCFMessage[] pcfResponse = messageAgent.Send(pcfMsg);
// Process and print response.
int pcfResponseLen = pcfResponse.Length;
for (int pcfResponseIdx = 0; pcfResponseIdx < pcfResponseLen; pcfResponseIdx++)
{
PCFParameter[] parameters = pcfResponse[pcfResponseIdx].GetParameters();
foreach (PCFParameter pm in parameters)
{
// We just want to print current queue depth only
if (pm.Parameter == MQC.MQIA_CURRENT_Q_DEPTH)
Console.WriteLine("Queue Depth" + " - " + pm.GetValue());
}
}
}
catch (PCFException pcfEx)
{
Console.Write(pcfEx);
}
catch (MQException ex)
{
Console.Write(ex);
}
finally
{
if (messageAgent != null)
messageAgent.Disconnect();
}
}

Resources