javax.jms.XAConnectionFactory Java Examples

The following examples show how to use javax.jms.XAConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JmsPoolXAConnectionFactory.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Override
public void setConnectionFactory(Object toUse) {
    if (toUse instanceof XAConnectionFactory) {
        try {
            toUse.getClass().getMethod("createContext", String.class, String.class);
            LOG.info("Provided ConnectionFactory is JMS 2.0+ capable.");
            jmsContextSupported = true;
        } catch (NoSuchMethodException | SecurityException e) {
            LOG.info("Provided ConnectionFactory is not JMS 2.0+ capable.");
        }

        connectionFactory = toUse;
    } else {
        throw new IllegalArgumentException("connectionFactory should implement javax.jms.XAConnectionFactory");
    }
}
 
Example #2
Source File: DtxPrepareNegativeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if preparing a DTX branch after setting fail flag in dtx.end throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class)
public void prepareDtxBranchAfterEndFails()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxPrepareTestCasePrepareDtxBranchAfterEndFails";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    // We are not starting the dtx branch
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    xaResource.end(xid, XAResource.TMFAIL);

    xaResource.prepare(xid);

    xaResource.rollback(xid);

    session.close();
    xaConnection.close();
}
 
Example #3
Source File: NarayanaXAConnectionFactoryWrapper.java    From narayana-spring-boot with Apache License 2.0 5 votes vote down vote up
private XAResourceRecoveryHelper getRecoveryHelper(XAConnectionFactory xaConnectionFactory) {
    if (this.properties.getRecoveryJmsUser() == null && this.properties.getRecoveryJmsPass() == null) {
        return new JmsXAResourceRecoveryHelper(xaConnectionFactory);
    }
    return new JmsXAResourceRecoveryHelper(xaConnectionFactory, this.properties.getRecoveryJmsUser(),
            this.properties.getRecoveryJmsPass());
}
 
Example #4
Source File: JmsPoolXAConnectionFactory.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Override
protected XAConnection createProviderConnection(PooledConnectionKey key) throws JMSException {
    if (connectionFactory instanceof XAConnectionFactory) {
        if (key.getUserName() == null && key.getPassword() == null) {
            return ((XAConnectionFactory) connectionFactory).createXAConnection();
        } else {
            return ((XAConnectionFactory) connectionFactory).createXAConnection(key.getUserName(), key.getPassword());
        }
    } else {
        throw new IllegalStateException("connectionFactory should implement javax.jms.XAConnectionFactory");
    }
}
 
Example #5
Source File: JmsPoolXAConnectionFactory.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Override
protected XAJMSContext createProviderContext(String username, String password, int sessionMode) {
    if (connectionFactory instanceof XAConnectionFactory) {
        if (username == null && password == null) {
            return ((XAConnectionFactory) connectionFactory).createXAContext();
        } else {
            return ((XAConnectionFactory) connectionFactory).createXAContext(username, password);
        }
    } else {
        throw new javax.jms.IllegalStateRuntimeException("connectionFactory should implement javax.jms.XAConnectionFactory");
    }
}
 
Example #6
Source File: JmsTracingTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void connectionFactory_wrapsXaInput() {
  abstract class Both implements XAConnectionFactory, ConnectionFactory {
  }

  assertThat(jmsTracing.connectionFactory(mock(Both.class)))
    .isInstanceOf(XAConnectionFactory.class);
}
 
Example #7
Source File: QueueDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublisherWithRollback() throws NamingException, JMSException, XAException, IOException {
    String queueName = "testPublisherWithRollback";
    String testMessage = "testPublisherWithRollback-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory()
                                                         .withQueue(queueName)
                                                         .build();

    XAConnectionFactory xaConnectionFactory =
            (XAConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);

    XAConnection xaConnection = xaConnectionFactory.createXAConnection();
    XASession xaSession = xaConnection.createXASession();
    XAResource xaResource = xaSession.getXAResource();

    Session session = xaSession.getSession();
    Queue queue = session.createQueue(queueName);
    MessageProducer producer = session.createProducer(queue);
    xaConnection.start();

    XidImpl xid = new XidImpl(0, "branchId_1".getBytes(), "globalId_1".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage(testMessage));
    xaResource.end(xid, XAResource.TMSUCCESS);

    int prepareOK = xaResource.prepare(xid);
    Assert.assertEquals(prepareOK, XAResource.XA_OK, "Prepare phase should return XA_OK");

    xaResource.rollback(xid);

    // Check whether the message is published to queue.
    QueueMetadata queueMetadata = restApiClient.getQueueMetadata(queueName);

    Assert.assertEquals((int) queueMetadata.getSize(), 0, "Queue should be empty");

    session.close();
    xaConnection.close();
}
 
Example #8
Source File: TracingConnectionFactory.java    From brave with Apache License 2.0 5 votes vote down vote up
TracingConnectionFactory(Object delegate, JmsTracing jmsTracing) {
  this.delegate = delegate;
  this.jmsTracing = jmsTracing;
  int types = 0;
  if (delegate instanceof ConnectionFactory) types |= TYPE_CF;
  if (delegate instanceof QueueConnectionFactory) types |= TYPE_QUEUE_CF;
  if (delegate instanceof TopicConnectionFactory) types |= TYPE_TOPIC_CF;
  if (delegate instanceof XAConnectionFactory) types |= TYPE_XA_CF;
  if (delegate instanceof XAQueueConnectionFactory) types |= TYPE_XA_QUEUE_CF;
  if (delegate instanceof XATopicConnectionFactory) types |= TYPE_XA_TOPIC_CF;
  this.types = types;
}
 
Example #9
Source File: JmsTracingConfigurationTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
static void checkXAConnection(AssertableApplicationContext ctx) throws JMSException {
	// Not using try-with-resources as that doesn't exist in JMS 1.1
	XAConnection con = ctx.getBean(XAConnectionFactory.class).createXAConnection();
	try {
		con.setExceptionListener(exception -> {
		});
		assertThat(con.getExceptionListener().getClass().getName())
				.startsWith("brave.jms.TracingExceptionListener");
	}
	finally {
		con.close();
	}
}
 
Example #10
Source File: ConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void assertNTypes(ActiveMQConnectionFactory factory, final int total) {
   StringBuilder text = new StringBuilder();
   text.append(factory + "\n is instance of ");
   int num = 0;
   if (factory instanceof ConnectionFactory) {
      num++;
      text.append("ConnectionFactory ");
   }
   if (factory instanceof XAConnectionFactory) {
      num++;
      text.append("XAConnectionFactory ");
   }
   if (factory instanceof QueueConnectionFactory) {
      num++;
      text.append("QueueConnectionFactory ");
   }
   if (factory instanceof TopicConnectionFactory) {
      num++;
      text.append("TopicConnectionFactory ");
   }
   if (factory instanceof XAQueueConnectionFactory) {
      num++;
      text.append("XAQueueConnectionFactory ");
   }
   if (factory instanceof XATopicConnectionFactory) {
      num++;
      text.append("XATopicConnectionFactory ");
   }
   Assert.assertEquals(text.toString(), total, num);
}
 
Example #11
Source File: DtxCommitNegativeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if committing a DTX branch without starting it throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class,
      expectedExceptionsMessageRegExp = ".*Error while committing dtx session.*")
public void commitDtxBranchWithoutEnding()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxCommitTestCaseCommitDtxBranchWithoutEnding";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    // xaResource.end(xid, XAResource.TMSUCCESS);

    // xaResource.prepare(xid);

    xaResource.commit(xid, false);

    session.close();
    xaConnection.close();
}
 
Example #12
Source File: TracingConnectionFactoryBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
LazyConnectionAndXaConnectionFactory(BeanFactory beanFactory,
		ConnectionFactory connectionFactoryDelegate,
		XAConnectionFactory xaConnectionFactoryDelegate) {
	this.connectionFactoryDelegate = new LazyConnectionFactory(beanFactory,
			connectionFactoryDelegate);
	this.xaConnectionFactoryDelegate = new LazyXAConnectionFactory(beanFactory,
			xaConnectionFactoryDelegate);
}
 
Example #13
Source File: JmsXaTest.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
/**
 * Configure factory for broker here.
 */
private static XAConnectionFactory getXAConnectionFactory() {
    // replace this line with a factory for your broker, for example:
    //    ActiveMQXAConnectionFactory factory = new ActiveMQXAConnectionFactory(BROKER_URL);
    //    return factory;
    return null;
}
 
Example #14
Source File: TracingConnectionFactoryBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
		throws BeansException {
	// Wrap the caching connection factories instead of its target, because it catches
	// callbacks
	// such as ExceptionListener. If we don't wrap, cached callbacks like this won't
	// be traced.
	if (bean instanceof CachingConnectionFactory) {
		return new LazyConnectionFactory(this.beanFactory,
				(CachingConnectionFactory) bean);
	}
	if (bean instanceof JmsMessageEndpointManager) {
		JmsMessageEndpointManager manager = (JmsMessageEndpointManager) bean;
		MessageListener listener = manager.getMessageListener();
		if (listener != null) {
			manager.setMessageListener(
					new LazyMessageListener(this.beanFactory, listener));
		}
		return bean;
	}
	if (bean instanceof XAConnectionFactory && bean instanceof ConnectionFactory) {
		return new LazyConnectionAndXaConnectionFactory(this.beanFactory,
				(ConnectionFactory) bean, (XAConnectionFactory) bean);
	}
	// We check XA first in case the ConnectionFactory also implements
	// XAConnectionFactory
	else if (bean instanceof XAConnectionFactory) {
		return new LazyXAConnectionFactory(this.beanFactory,
				(XAConnectionFactory) bean);
	}
	else if (bean instanceof TopicConnectionFactory) {
		return new LazyTopicConnectionFactory(this.beanFactory,
				(TopicConnectionFactory) bean);
	}
	else if (bean instanceof ConnectionFactory) {
		return new LazyConnectionFactory(this.beanFactory, (ConnectionFactory) bean);
	}
	return bean;
}
 
Example #15
Source File: TracingConnectionFactoryBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
private XAConnectionFactory wrappedDelegate() {
	if (this.wrappedDelegate != null) {
		return this.wrappedDelegate;
	}
	this.wrappedDelegate = jmsTracing().xaConnectionFactory(this.delegate);
	return this.wrappedDelegate;
}
 
Example #16
Source File: TracingXAConnectionFactory.java    From brave with Apache License 2.0 4 votes vote down vote up
@JMS2_0 public XAJMSContext createXAContext(String userName, String password) {
  XAConnectionFactory xacf = (XAConnectionFactory) delegate;
  return TracingXAJMSContext.create(xacf.createXAContext(userName, password), jmsTracing);
}
 
Example #17
Source File: TracingXAConnectionFactory.java    From brave with Apache License 2.0 4 votes vote down vote up
static XAConnectionFactory create(XAConnectionFactory delegate, JmsTracing jmsTracing) {
  if (delegate == null) throw new NullPointerException("xaConnectionFactory == null");
  if (delegate instanceof TracingXAConnectionFactory) return delegate;
  return new TracingXAConnectionFactory(delegate, jmsTracing);
}
 
Example #18
Source File: ActiveMQServerTestCase.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public XAConnectionFactory getXAConnectionFactory() throws Exception {
   return (XAConnectionFactory) getInitialContext().lookup("/CF_XA_TRUE");
}
 
Example #19
Source File: TracingXAConnectionFactory.java    From brave with Apache License 2.0 4 votes vote down vote up
TracingXAConnectionFactory(XAConnectionFactory delegate, JmsTracing jmsTracing) {
  super(delegate, jmsTracing);
}
 
Example #20
Source File: DtxStartPositiveTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if acknowledging a messages works correctly with session joining. Steps are,
 * 1. Publish two messages to two queues using two non-transacted sessions
 * 2. Create two distributed transaction sessions and join one session to other.
 * 3. Receive messages and ack using two sessions.
 * 4. Commit the session
 * 5. Subscribe to the published queue and see if any message is received.
 */
@Test(groups = { "wso2.mb", "dtx" })
public void xaStartJoinMessageAckTestCase()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueNameOne = "DtxStartPositiveTestCaseXaStartJoinMessageAckTestCaseOne";
    String queueNameTwo = "DtxStartPositiveTestCaseXaStartJoinMessageAckTestCaseTwo";

    InitialContext initialContext = JMSClientHelper.createInitialContextBuilder("admin", "admin", "localhost",
            getAMQPPort()).withQueue(queueNameOne).build();

    ConnectionFactory nonXaConnectionFactory = (ConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_CONNECTION_FACTORY);
    Connection nonXaQueueConnection = nonXaConnectionFactory.createConnection();
    nonXaQueueConnection.start();
    Session nonXaQueueSessionOne = nonXaQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination xaTestQueueOne = nonXaQueueSessionOne.createQueue(queueNameOne);
    Destination xaTestQueueTwo = nonXaQueueSessionOne.createQueue(queueNameTwo);

    MessageProducer nonXaQueueSessionProducerOne = nonXaQueueSessionOne.createProducer(xaTestQueueOne);
    MessageProducer nonXaQueueSessionProducerTwo = nonXaQueueSessionOne.createProducer(xaTestQueueTwo);

    nonXaQueueSessionProducerOne.send(nonXaQueueSessionOne.createTextMessage("Message 1"));
    nonXaQueueSessionProducerTwo.send(nonXaQueueSessionOne.createTextMessage("Message 2"));

    nonXaQueueSessionProducerOne.close();
    nonXaQueueSessionProducerTwo.close();

    XAConnectionFactory xaConnectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    // Create XA resource one
    XAConnection xaConnectionOne = xaConnectionFactory.createXAConnection();
    xaConnectionOne.start();
    XASession xaSessionOne = xaConnectionOne.createXASession();

    XAResource xaResourceOne = xaSessionOne.getXAResource();
    Session sessionOne = xaSessionOne.getSession();

    MessageConsumer xaConsumerOne = sessionOne.createConsumer(xaTestQueueOne);

    // Create XA resource two
    XAConnection xaConnectionTwo = xaConnectionFactory.createXAConnection();
    xaConnectionTwo.start();
    XASession xaSessionTwo = xaConnectionTwo.createXASession();

    XAResource xaResourceTwo = xaSessionTwo.getXAResource();
    Session sessionTwo = xaSessionTwo.getSession();

    MessageConsumer xaConsumerTwo = sessionTwo.createConsumer(xaTestQueueTwo);

    Xid xid = JMSClientHelper.getNewXid();

    boolean sameRM = xaResourceOne.isSameRM(xaResourceTwo);

    Assert.assertEquals(sameRM, true, "Resource one and resource two are connected to different resource "
            + "managers");

    xaResourceOne.start(xid, XAResource.TMNOFLAGS);
    xaResourceTwo.start(xid, XAResource.TMJOIN);

    Message receivedMessageForQueueOne = xaConsumerOne.receive(5000);
    Assert.assertNotNull(receivedMessageForQueueOne, "A message was not received for queue " + queueNameOne);
    Message receivedMessageForQueueTwo = xaConsumerTwo.receive(5000);
    Assert.assertNotNull(receivedMessageForQueueTwo, "A message was not received for queue " + queueNameTwo);

    xaResourceOne.end(xid, XAResource.TMSUCCESS);

    xaResourceOne.prepare(xid);
    xaResourceOne.commit(xid, false);

    xaConnectionOne.close();
    xaConnectionTwo.close();

    // subscribe and see if the message is received
    MessageConsumer nonXaConsumerOne = nonXaQueueSessionOne.createConsumer(xaTestQueueOne);

    Session nonXaQueueSessionTwo = nonXaQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer nonXaConsumerTwo = nonXaQueueSessionTwo.createConsumer(xaTestQueueTwo);

    // wait 3 seconds
    receivedMessageForQueueOne = nonXaConsumerOne.receive(3000);
    Assert.assertNull(receivedMessageForQueueOne, "Message received after committing for queue " + queueNameOne);

    receivedMessageForQueueTwo = nonXaConsumerTwo.receive(3000);
    Assert.assertNull(receivedMessageForQueueTwo, "Message received after committing for queue " + queueNameTwo);

    nonXaQueueConnection.close();
}
 
Example #21
Source File: DtxStartPositiveTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if publishing messages works correctly with session joining. Steps are,
 * 1. Create two distributed transaction sessions and join one session to other.
 * 2. Publish messages using two sessions.
 * 3. Subscribe to the published queue and see if any message is received.
 * 4. Commit the session
 * 5. Subscribe to the queue and see if two messages are received
 */
@Test(groups = { "wso2.mb", "dtx" })
public void xaMultiSessionPublishTestCase()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxStartPositiveTestCaseXaMultiSessionPublishTestCase";

    InitialContext initialContext = JMSClientHelper.createInitialContextBuilder("admin", "admin", "localhost",
                                                                                getAMQPPort()).withQueue(queueName).build();

    XAConnectionFactory xaConnectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    // Create XA resource one
    XAConnection xaConnectionOne = xaConnectionFactory.createXAConnection();
    xaConnectionOne.start();
    XASession xaSessionOne = xaConnectionOne.createXASession();

    XAResource xaResourceOne = xaSessionOne.getXAResource();
    Session sessionOne = xaSessionOne.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    sessionOne.createQueue(queueName);
    MessageProducer producerOne = sessionOne.createProducer(xaTestQueue);

    // Create XA resource two
    XASession xaSessionTwo = xaConnectionOne.createXASession();

    XAResource xaResourceTwo = xaSessionTwo.getXAResource();
    Session sessionTwo = xaSessionTwo.getSession();

    MessageProducer producerTwo = sessionTwo.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    boolean sameRM = xaResourceOne.isSameRM(xaResourceTwo);

    Assert.assertEquals(sameRM, true, "Resource one and resource two are connected to different resource "
            + "managers");

    xaResourceOne.start(xid, XAResource.TMNOFLAGS);
    xaResourceTwo.start(xid, XAResource.TMJOIN);

    producerOne.send(sessionOne.createTextMessage("Test 1"));
    producerTwo.send(sessionTwo.createTextMessage("Test 2"));

    xaResourceOne.end(xid, XAResource.TMSUCCESS);

    // subscribe and see if the message is received
    ConnectionFactory nonXaConnectionFactory = (ConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_CONNECTION_FACTORY);
    Connection nonXaQueueConnection = nonXaConnectionFactory.createConnection();
    nonXaQueueConnection.start();
    Session nonXaQueueSession = nonXaQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer messageConsumer = nonXaQueueSession.createConsumer(xaTestQueue);

    // wait 5 seconds
    Message receive = messageConsumer.receive(5000);
    Assert.assertNull(receive, "Message received before committing");

    xaResourceOne.prepare(xid);
    xaResourceOne.commit(xid, false);

    xaConnectionOne.close();

    //This is only added to find out the reason for the intermittent failure of this test method. Should be removed
    // once the issue is identified.
    try {
        // Logging in
        LoginLogoutClient loginLogoutClientForAdmin = new LoginLogoutClient(super.automationContext);
        String sessionCookie = loginLogoutClientForAdmin.login();
        AndesAdminClient admin = new AndesAdminClient(super.backendURL, sessionCookie);

        //Check message count in queue
        org.wso2.carbon.andes.stub.admin.types.Message[] queueOneMessages
                = admin.browseQueue(queueName, 0, 10);
        Assert.assertEquals(queueOneMessages.length, 2, "Message not published to queue " + queueName);

        //Logging out
        loginLogoutClientForAdmin.logout();

    } catch (RemoteException | AutomationUtilException | AndesAdminServiceBrokerManagerAdminException
            | LogoutAuthenticationExceptionException e) {
        e.printStackTrace();
    }

    receive = messageConsumer.receive(5000);
    Assert.assertNotNull(receive, "Message not received");

    receive = messageConsumer.receive(5000);
    Assert.assertNotNull(receive, "Message not received");

    nonXaQueueConnection.close();
}
 
Example #22
Source File: DtxStartPositiveTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if message acknowledgement works correctly with session suspend and resume. Steps are,
 *    1. Publish a message to a queue
 *    2. Using a distributed transacted session receive the message and suspend the session
 *    3. Publish a message to the queue
 *    4. Resume the session again, ack and commit
 *    5. Subscribe again using a normal session and see if any message is received
 */
@Test(groups = { "wso2.mb", "dtx" })
public void suspendResumeMessageAckTestCase()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxStartPositiveTestCaseSuspendResumeMessageAckTestCase";

    InitialContext initialContext = JMSClientHelper.createInitialContextBuilder("admin", "admin", "localhost",
            getAMQPPort()).withQueue(queueName).build();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);

    // Publish message to queue
    ConnectionFactory queueConnectionFactory = (ConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_CONNECTION_FACTORY);
    Connection queueConnection = queueConnectionFactory.createConnection();
    queueConnection.start();
    Session queueSession = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    queueSession.createQueue(queueName);
    MessageProducer messageProducer = queueSession.createProducer(xaTestQueue);

    messageProducer.send(queueSession.createTextMessage("Test message 1"));

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    MessageConsumer xaConsumer = session.createConsumer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    xaResource.start(xid, XAResource.TMNOFLAGS);
    Message receivedMessage = xaConsumer.receive(5000);
    xaResource.end(xid, XAResource.TMSUSPEND);

    Assert.assertNotNull(receivedMessage, "No message received");

    messageProducer.send(queueSession.createTextMessage("Test message 2"));

    messageProducer.close();

    xaResource.start(xid, XAResource.TMRESUME);
    receivedMessage = xaConsumer.receive(5000);
    xaResource.end(xid, XAResource.TMSUCCESS);

    Assert.assertNotNull(receivedMessage, "No message received");

    int ret = xaResource.prepare(xid);
    Assert.assertEquals(ret, XAResource.XA_OK, "Dtx.prepare was not successful.");

    xaResource.commit(xid, false);

    session.close();
    xaConnection.close();

    // subscribe and see if the message is received
    MessageConsumer messageConsumer = queueSession.createConsumer(xaTestQueue);

    // wait 5 seconds
    Message receivedMessageFromNormalConnection = messageConsumer.receive(5000);
    Assert.assertNull(receivedMessageFromNormalConnection, "Message received. Commit might have failed");

    queueConnection.close();
}
 
Example #23
Source File: DtxStartPositiveTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if publishing messages works correctly with session suspending and resuming.Steps are,
 * 1. Using a distributed transaction a message is published to a queue and session is suspended
 * 2. Subscribe to the published queue and see if any message is received.
 * 3. Resume the suspended session and publish another message and commit
 * 4. Subscribe to the queue and see if two messages are received
 */
@Test(groups = { "wso2.mb", "dtx" })
public void suspendResumeQueuePublishTestCase()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxStartPositiveTestCaseSuspendResumeQueuePublishTestCase";

    InitialContext initialContext = JMSClientHelper.createInitialContextBuilder("admin", "admin", "localhost",
            getAMQPPort()).withQueue(queueName).build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    xaResource.end(xid, XAResource.TMSUSPEND);

    // subscribe and see if the message is received
    ConnectionFactory queueConnectionFactory = (ConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_CONNECTION_FACTORY);
    Connection queueConnection = queueConnectionFactory.createConnection();
    queueConnection.start();
    Session queueSession = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer messageConsumer = queueSession.createConsumer(xaTestQueue);

    // wait 5 seconds
    Message receive = messageConsumer.receive(5000);
    Assert.assertNull(receive, "Message received. Message was not rolled back");


    xaResource.start(xid, XAResource.TMRESUME);
    producer.send(session.createTextMessage("Test 2"));
    xaResource.end(xid, XAResource.TMSUCCESS);

    xaResource.prepare(xid);
    xaResource.commit(xid, false);

    session.close();
    xaConnection.close();

    receive = messageConsumer.receive(5000);
    Assert.assertNotNull(receive, "Message not received");

    receive = messageConsumer.receive(5000);
    Assert.assertNotNull(receive, "Message not received");

    queueConnection.close();
}
 
Example #24
Source File: DtxEndNegativeTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if ending a dtx branch started in a different session throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class,
      expectedExceptionsMessageRegExp = ".*Error while ending dtx session.*")
public void endDtxBranchBelongToADifferentSession()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxEndTestCaseEndDtxBranchBelongToADifferentSession";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

     // We are not starting the dtx branch
     xaResource.start(xid, XAResource.TMNOFLAGS);

    XAConnection secondXaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession secondXaSession = secondXaConnection.createXASession();

    XAResource secondXaResource = secondXaSession.getXAResource();


    producer.send(session.createTextMessage("Test 1"));
    secondXaResource.end(xid, XAResource.TMSUCCESS);

    xaResource.prepare(xid);

    xaResource.rollback(xid);

    session.close();
    xaConnection.close();
}
 
Example #25
Source File: JmsTracingTest.java    From brave with Apache License 2.0 4 votes vote down vote up
@Test public void xaConnectionFactory_wrapsInput() {
  assertThat(jmsTracing.xaConnectionFactory(mock(XAConnectionFactory.class)))
    .isInstanceOf(TracingXAConnectionFactory.class);
}
 
Example #26
Source File: DtxEndNegativeTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if ending DTX branch without starting it throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class,
      expectedExceptionsMessageRegExp = ".*Error while ending dtx session.*")
public void endDtxBranchWithoutStarting()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxEndTestCaseEndDtxBranchWithoutStarting";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

     Xid xid = JMSClientHelper.getNewXid();

    // We are not starting the dtx branch
    // xaResource.start(xid, XAResource.TMJOIN);

    producer.send(session.createTextMessage("Test 1"));
    xaResource.end(xid, XAResource.TMSUCCESS);

    xaResource.prepare(xid);

    xaResource.rollback(xid);

    session.close();
    xaConnection.close();
}
 
Example #27
Source File: DtxPrepareNegativeTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if preparing a DTX branch with publishing permission issues throws an error
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class)
public void prepareDtxBranchWithNoRoutesIssue()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxPrepareTestCasePrepareDtxBranchWithNoRoutesIssues";

    User adminUser = getSuperTenantAdminUser();
    InitialContext adminInitialContext
            = JMSClientHelper.createInitialContextBuilder(adminUser.getUserNameWithoutDomain(),
                                                          adminUser.getPassword(),
                                                          getBrokerHost(),
                                                          getAMQPPort()).withQueue(queueName).build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory
            = (XAConnectionFactory) adminInitialContext.lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination testQueue = (Destination) adminInitialContext.lookup(queueName);
    MessageProducer producer = session.createProducer(testQueue);

    Xid xid = JMSClientHelper.getNewXid();

    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    xaResource.end(xid, XAResource.TMSUCCESS);

    // Test should fail at prepare stage due to no route issue
    int prepareResponseCode = xaResource.prepare(xid);

    Assert.assertNotEquals(prepareResponseCode, XAResource.XA_OK, "Prepare should fail due to no route issue");

    xaResource.commit(xid, false);

    session.close();
    xaConnection.close();
}
 
Example #28
Source File: DtxPrepareNegativeTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if preparing a DTX branch without starting it throws an exception
 */
@Test(groups = { "wso2.mb", "dtx" }, expectedExceptions = XAException.class,
      expectedExceptionsMessageRegExp = ".*Error while preparing dtx session.*")
public void prepareDtxBranchWithoutEnding()
        throws NamingException, JMSException, XAException, XPathExpressionException {
    String queueName = "DtxPrepareTestCasePrepareDtxBranchWithoutEnding";

    InitialContext initialContext = JMSClientHelper
            .createInitialContextBuilder("admin", "admin", "localhost", getAMQPPort())
            .withQueue(queueName)
            .build();

    // Publish to queue and rollback
    XAConnectionFactory connectionFactory = (XAConnectionFactory) initialContext
            .lookup(JMSClientHelper.QUEUE_XA_CONNECTION_FACTORY);

    XAConnection xaConnection = connectionFactory.createXAConnection();
    xaConnection.start();
    XASession xaSession = xaConnection.createXASession();

    XAResource xaResource = xaSession.getXAResource();
    Session session = xaSession.getSession();

    Destination xaTestQueue = (Destination) initialContext.lookup(queueName);
    session.createQueue(queueName);
    MessageProducer producer = session.createProducer(xaTestQueue);

    Xid xid = JMSClientHelper.getNewXid();

    // We are not starting the dtx branch
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(session.createTextMessage("Test 1"));
    // xaResource.end(xid, XAResource.TMFAIL);

    xaResource.prepare(xid);

    xaResource.rollback(xid);

    session.close();
    xaConnection.close();
}
 
Example #29
Source File: TracingXAConnectionFactory.java    From brave with Apache License 2.0 4 votes vote down vote up
@JMS2_0 public XAJMSContext createXAContext() {
  XAConnectionFactory xacf = (XAConnectionFactory) delegate;
  return TracingXAJMSContext.create(xacf.createXAContext(), jmsTracing);
}
 
Example #30
Source File: JmsTracingTest.java    From brave with Apache License 2.0 4 votes vote down vote up
@Test public void xaConnectionFactory_doesntDoubleWrap() {
  XAConnectionFactory wrapped = jmsTracing.xaConnectionFactory(mock(XAConnectionFactory.class));
  assertThat(jmsTracing.xaConnectionFactory(wrapped))
    .isSameAs(wrapped);
}