Author |
Message
|
drajani |
Posted: Fri Jul 07, 2017 2:42 am Post subject: Transaction handling |
|
|
Newbie
Joined: 21 Dec 2016 Posts: 9
|
Hi, For C#.NET based application using MQ Appliance, I have below flow to be addressed.
- Read a message (Msg1) from an MQ on QueueManager MyQMgr
- Parse the content and do some logic
- Create and publish message (Msg2) with topic (Topic1) to MyQMgr for downstream systems.
- Do some more logic.
- Create and publish message (Msg3) with topic (Topic2) to MyQMgr for other downstream systems.
- Finally, commit the message Msg1.
Transaction behavior Requirement:
The end to end processing to be atomic. For eg, if Msg3 publish fails, Msg2 publish should fail and Msg1 should be rolled back.
Questions:
1. Is handling above scenario possible using IBM.XMS libraries with async model i.e., in OnMessage() event ?
2. If not, is this possible using IBM.WMQ libraries.
Regards |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Jul 07, 2017 3:45 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Yes, it's possible. Use an MQ Transaction.
Yes, it's better to use something other than the C++ API, as that is stabilized and won't be updated ever again. Which means it could stop working with newer C++ runtimes and other exciting things. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
drajani |
Posted: Mon Jul 10, 2017 1:24 am Post subject: |
|
|
Newbie
Joined: 21 Dec 2016 Posts: 9
|
Thank you. With below code the messages still come back to the queue after closing the app. Can you help with what is wrong here.
Code: |
private IConnection mqConnection = null;
private ISession session = null;
private void GetConnection()
{
XMSFactoryFactory factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
IConnectionFactory connectionFactory = factoryFactory.CreateConnectionFactory();
connectionFactory.SetStringProperty(XMSC.WMQ_CONNECTION_NAME_LIST, _connectionNameList);
connectionFactory.SetStringProperty(XMSC.WMQ_CHANNEL, _channel);
connectionFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, _queueManager);
connectionFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
connectionFactory.SetIntProperty(XMSC.WMQ_CLIENT_RECONNECT_OPTIONS, XMSC.WMQ_CLIENT_RECONNECT);
connectionFactory.SetIntProperty(XMSC.WMQ_SHARE_CONV_ALLOWED, 1);
mqConnection = connectionFactory.CreateConnection();
mqConnection.ExceptionListener = new ExceptionListener(OnException);
// Create session for request queue
session = mqConnection.CreateSession(true, AcknowledgeMode.AutoAcknowledge);
IDestination queue = session.CreateQueue(ReqMQName);
// Create the consumer and register an async message listener
IMessageConsumer consumer = session.CreateConsumer(queue);
consumer.MessageListener = new MessageListener(OnMessage);
mqConnection.Start();
}
private void OnMessage(IMessage msg)
{
IBytesMessage bytesmsg;
using (CommittableTransaction transScope = new CommittableTransaction())
{
try
{
CommittableTransaction.Current = transScope;
bytesmsg = (IBytesMessage)msg;
//Logic here
transScope.Commit();
}
catch (TransactionException tex)
{
transScope.Rollback();
//Error handling
}
catch (Exception ex)
{
transScope.Rollback();
//Error handling
}
CommittableTransaction.Current = null;
}
}
private void OnException(Exception ex)
{
//Display error
}
|
|
|
Back to top |
|
 |
zpat |
Posted: Mon Jul 10, 2017 1:35 am Post subject: |
|
|
 Jedi Council
Joined: 19 May 2001 Posts: 5866 Location: UK
|
Remember to close the queues/topics handles and disconnect from the queue manager in an orderly fashion.
Don't just allow the application to terminate - as MQ will assume your application has failed and it will rollback the transaction.
Or you can explicitly issue a commit and then close the handles. _________________ Well, I don't think there is any question about it. It can only be attributable to human error. This sort of thing has cropped up before, and it has always been due to human error. |
|
Back to top |
|
 |
drajani |
Posted: Mon Jul 10, 2017 10:21 pm Post subject: |
|
|
Newbie
Joined: 21 Dec 2016 Posts: 9
|
Thanks zpat. I just kept the sample code flow. All key aspects you mentioned are taken care of.
Still looking for help with what else is missing in xms transaction handling. |
|
Back to top |
|
 |
fjb_saper |
Posted: Tue Jul 11, 2017 1:17 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
drajani wrote: |
Thanks zpat. I just kept the sample code flow. All key aspects you mentioned are taken care of.
Still looking for help with what else is missing in xms transaction handling. |
If you're running XMS you should be using JMS style transaction handling:
Make your XMS Session transacted by creating it with (true, AUTO_ACKNOWLEDGE), or createJMSContext(JMSContext.SessionTransacted);
Remember to commit the session at the end of each transaction:
Code: |
isesssion.commit(); //JMS 1.X
// or
jmscontext.commit(); //JMS 2.0 |
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
drajani |
Posted: Tue Jul 11, 2017 3:32 am Post subject: |
|
|
Newbie
Joined: 21 Dec 2016 Posts: 9
|
|
Back to top |
|
 |
|