public void testDeliveryCountUpdatedOnCloseXA() throws Exception
{
XAConnection xaConn = null;
Connection conn = null;
TransactionManager mgr = new TransactionManagerImple();
Transaction toResume = null;
Transaction tx = null;
try
{
toResume = mgr.suspend();
conn = getConnectionFactory().createConnection();
// Send a message
Session producerSess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = producerSess.createProducer(HornetQServerTestCase.queue1);
TextMessage tm = producerSess.createTextMessage("message1");
producer.send(tm);
xaConn = ((XAConnectionFactory)getXAConnectionFactory()).createXAConnection();
XASession consumerSess = xaConn.createXASession();
MessageConsumer consumer = consumerSess.createConsumer(HornetQServerTestCase.queue1);
xaConn.start();
DummyXAResource res = new DummyXAResource();
mgr.begin();
tx = mgr.getTransaction();
tx.enlistResource(res);
tx.enlistResource(consumerSess.getXAResource());
TextMessage rm = (TextMessage)consumer.receive(1000);
ProxyAssertSupport.assertNotNull(rm);
ProxyAssertSupport.assertEquals(tm.getText(), rm.getText());
ProxyAssertSupport.assertEquals(1, rm.getIntProperty("JMSXDeliveryCount"));
ProxyAssertSupport.assertFalse(rm.getJMSRedelivered());
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
mgr.rollback();
mgr.begin();
tx = mgr.getTransaction();
tx.enlistResource(res);
tx.enlistResource(consumerSess.getXAResource());
rm = (TextMessage)consumer.receive(1000);
ProxyAssertSupport.assertNotNull(rm);
ProxyAssertSupport.assertEquals(tm.getText(), rm.getText());
ProxyAssertSupport.assertEquals(2, rm.getIntProperty("JMSXDeliveryCount"));
ProxyAssertSupport.assertTrue(rm.getJMSRedelivered());
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
mgr.rollback();
mgr.begin();
tx = mgr.getTransaction();
tx.enlistResource(res);
tx.enlistResource(consumerSess.getXAResource());
rm = (TextMessage)consumer.receive(1000);
ProxyAssertSupport.assertNotNull(rm);
ProxyAssertSupport.assertEquals(tm.getText(), rm.getText());
ProxyAssertSupport.assertEquals(3, rm.getIntProperty("JMSXDeliveryCount"));
ProxyAssertSupport.assertTrue(rm.getJMSRedelivered());
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
mgr.rollback();
// Must close consumer first
consumer.close();
consumerSess.close();
consumerSess = xaConn.createXASession();
consumer = consumerSess.createConsumer(HornetQServerTestCase.queue1);
mgr.begin();
tx = mgr.getTransaction();
tx.enlistResource(res);
tx.enlistResource(consumerSess.getXAResource());
rm = (TextMessage)consumer.receive(1000);
ProxyAssertSupport.assertNotNull(rm);
ProxyAssertSupport.assertEquals(tm.getText(), rm.getText());
ProxyAssertSupport.assertEquals(4, rm.getIntProperty("JMSXDeliveryCount"));
ProxyAssertSupport.assertTrue(rm.getJMSRedelivered());
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
}
finally
{
if (conn != null)
{
conn.close();
}
if (tx != null)
{
try
{
mgr.commit();
}
catch (Exception ignore)
{
}
}
if (xaConn != null)
{
xaConn.close();
}
if (toResume != null)
{
try
{
mgr.resume(toResume);
}
catch (Exception ignore)
{
}
}