javax.jms.XATopicConnection Java Examples

The following examples show how to use javax.jms.XATopicConnection. 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: ManagedConnection.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
public static ManagedConnection create(
                                        final Connection connection ) {

    if ( (connection instanceof XAQueueConnection) && (connection instanceof XATopicConnection)) {
        return new ManagedXAQueueTopicConnection(connection);
    } else if (connection instanceof XAQueueConnection) {
        return new ManagedXAQueueConnection((XAQueueConnection) connection);
    } else if (connection instanceof XATopicConnection) {
        return new ManagedXATopicConnection((XATopicConnection) connection);
    } else if ( (connection instanceof QueueConnection) && (connection instanceof TopicConnection)) {
        return new ManagedQueueTopicConnection(connection);
    } else if (connection instanceof QueueConnection) {
        return new ManagedQueueConnection((QueueConnection) connection);
    } else if (connection instanceof TopicConnection) {
        return new ManagedTopicConnection((TopicConnection) connection);
    } else {
        return new ManagedConnection(connection);
    }
}
 
Example #2
Source File: ConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void assertConnectionType(Connection conn, String type) {
   if ("generic".equals(type) || "queue".equals(type) || "topic".equals(type)) {
      //generic
      Assert.assertFalse(conn instanceof XAConnection);
      Assert.assertTrue(conn instanceof QueueConnection);
      Assert.assertFalse(conn instanceof XAQueueConnection);
      Assert.assertTrue(conn instanceof TopicConnection);
      Assert.assertFalse(conn instanceof XATopicConnection);
   } else if ("xa".equals(type) || "xa-queue".equals(type) || "xa-topic".equals(type)) {
      Assert.assertTrue(conn instanceof XAConnection);
      Assert.assertTrue(conn instanceof QueueConnection);
      Assert.assertTrue(conn instanceof XAQueueConnection);
      Assert.assertTrue(conn instanceof TopicConnection);
      Assert.assertTrue(conn instanceof XATopicConnection);
   } else {
      Assert.fail("Unknown connection type: " + type);
   }
}
 
Example #3
Source File: ActiveMQRAConnectionFactoryImpl.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Create a XA topic connection
 *
 * @param userName The user name
 * @param password The password
 * @return The connection
 * @throws JMSException Thrown if the operation fails
 */
@Override
public XATopicConnection createXATopicConnection(final String userName, final String password) throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("createXATopicConnection(" + userName + ", ****)");
   }

   ActiveMQRASessionFactoryImpl s = new ActiveMQRASessionFactoryImpl(mcf, cm, getResourceAdapter().getTM(), ActiveMQRAConnectionFactory.XA_TOPIC_CONNECTION);
   s.setUserName(userName);
   s.setPassword(password);
   validateUser(s);

   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("Created topic connection: " + s);
   }

   return s;
}
 
Example #4
Source File: TopicDtxCommitNegativeTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@AfterMethod
public void tearDown() throws Exception {
    restApiClient.close();
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().build();
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    xaSession = xaTopicConnection.createXATopicSession();

    xaResource = xaSession.getXAResource();
    xaResource.rollback(xid);
    xaSession.close();
    xaTopicConnection.close();
    xaConnection.close();
}
 
Example #5
Source File: JmsTracingTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void topicConnection_wrapsXaInput() {
  abstract class Both implements XATopicConnection, TopicConnection {
  }

  assertThat(jmsTracing.topicConnection(mock(Both.class)))
    .isInstanceOf(XATopicConnection.class);
}
 
Example #6
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublisherWithRollback() throws Exception {

    String subscriptionId = "sub-testPublisherWithRollback";
    String topicName = "testPublisherWithRollback";
    String testMessage = "testPublisherWithRollback-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Setup XA producer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    MessageProducer producer = xaTopicSession.createProducer(topic);

    // Setup non-transactional consumer.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicSubscriber durableSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);
    xaTopicConnection.start();

    // Send message withing a XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(xaTopicSession.createTextMessage(testMessage));
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.rollback(xid);

    durableSubscriber.close();
    xaTopicSession.close();
    xaTopicConnection.close();
    QueueMetadata queueMetadata = restApiClient.getQueueMetadata("carbon:" + subscriptionId);
    Assert.assertEquals((int) queueMetadata.getSize(), 0, "Queue is not empty");
}
 
Example #7
Source File: TracingConnection.java    From brave with Apache License 2.0 5 votes vote down vote up
TracingConnection(Connection delegate, JmsTracing jmsTracing) {
  this.delegate = delegate;
  this.jmsTracing = jmsTracing;
  int types = 0;
  if (delegate instanceof QueueConnection) types |= TYPE_QUEUE;
  if (delegate instanceof TopicConnection) types |= TYPE_TOPIC;
  if (delegate instanceof XAConnection) types |= TYPE_XA;
  if (delegate instanceof XAQueueConnection) types |= TYPE_XA_QUEUE;
  if (delegate instanceof XATopicConnection) types |= TYPE_XA_TOPIC;
  this.types = types;
}
 
Example #8
Source File: TracingXAConnectionFactory.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public XATopicConnection createXATopicConnection(String userName, String password)
  throws JMSException {
  checkTopicConnectionFactory();
  XATopicConnectionFactory xaqcf = (XATopicConnectionFactory) delegate;
  return TracingXAConnection.create(xaqcf.createXATopicConnection(userName, password),
    jmsTracing);
}
 
Example #9
Source File: ActiveMQRAConnectionFactoryImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Create a XA topic connection
 *
 * @return The connection
 * @throws JMSException Thrown if the operation fails
 */
@Override
public XATopicConnection createXATopicConnection() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("createXATopicConnection()");
   }

   ActiveMQRASessionFactoryImpl s = new ActiveMQRASessionFactoryImpl(mcf, cm, getResourceAdapter().getTM(), ActiveMQRAConnectionFactory.XA_TOPIC_CONNECTION);

   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("Created topic connection: " + s);
   }

   return s;
}
 
Example #10
Source File: JmsTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
public TopicConnection topicConnection(TopicConnection connection) {
  // It is common to implement both interfaces
  if (connection instanceof XATopicConnection) {
    return xaTopicConnection((XATopicConnection) connection);
  }
  return TracingConnection.create(connection, this);
}
 
Example #11
Source File: TracingXAConnection.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public XATopicSession createXATopicSession() throws JMSException {
  if ((types & TYPE_XA_TOPIC) != TYPE_XA_TOPIC) {
    throw new IllegalStateException(delegate + " is not an XATopicConnection");
  }
  XATopicSession xats = ((XATopicConnection) delegate).createXATopicSession();
  return TracingXASession.create(xats, jmsTracing);
}
 
Example #12
Source File: JmsTracingTest.java    From brave with Apache License 2.0 4 votes vote down vote up
@Test public void xaTopicConnection_doesntDoubleWrap() {
  XATopicConnection wrapped = jmsTracing.xaTopicConnection(mock(XATopicConnection.class));
  assertThat(jmsTracing.xaTopicConnection(wrapped))
    .isSameAs(wrapped);
}
 
Example #13
Source File: JmsTracingTest.java    From brave with Apache License 2.0 4 votes vote down vote up
@Test public void xaTopicConnection_wrapsInput() {
  assertThat(jmsTracing.xaTopicConnection(mock(XATopicConnection.class)))
    .isInstanceOf(TracingXAConnection.class);
}
 
Example #14
Source File: TracingXAConnectionFactory.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override public XATopicConnection createXATopicConnection() throws JMSException {
  checkTopicConnectionFactory();
  XATopicConnectionFactory xaqcf = (XATopicConnectionFactory) delegate;
  return TracingXAConnection.create(xaqcf.createXATopicConnection(), jmsTracing);
}
 
Example #15
Source File: JmsTracing.java    From brave with Apache License 2.0 4 votes vote down vote up
public XATopicConnection xaTopicConnection(XATopicConnection connection) {
  return TracingXAConnection.create(connection, this);
}
 
Example #16
Source File: ActiveMQConnectionFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public XATopicConnection createXATopicConnection(final String username, final String password) throws JMSException {
   return (XATopicConnection) createConnectionInternal(username, password, true, ActiveMQConnection.TYPE_TOPIC_CONNECTION);
}
 
Example #17
Source File: ActiveMQConnectionFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public XATopicConnection createXATopicConnection() throws JMSException {
   return createXATopicConnection(user, password);
}
 
Example #18
Source File: ConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testConnectionTypes() throws Exception {
   deployConnectionFactory(0, JMSFactoryType.CF, "ConnectionFactory", "/ConnectionFactory");
   deployConnectionFactory(0, JMSFactoryType.QUEUE_XA_CF, "CF_QUEUE_XA_TRUE", "/CF_QUEUE_XA_TRUE");
   deployConnectionFactory(0, JMSFactoryType.XA_CF, "CF_XA_TRUE", "/CF_XA_TRUE");
   deployConnectionFactory(0, JMSFactoryType.QUEUE_CF, "CF_QUEUE", "/CF_QUEUE");
   deployConnectionFactory(0, JMSFactoryType.TOPIC_CF, "CF_TOPIC", "/CF_TOPIC");
   deployConnectionFactory(0, JMSFactoryType.TOPIC_XA_CF, "CF_TOPIC_XA_TRUE", "/CF_TOPIC_XA_TRUE");

   Connection genericConnection = null;
   XAConnection xaConnection = null;
   QueueConnection queueConnection = null;
   TopicConnection topicConnection = null;
   XAQueueConnection xaQueueConnection = null;
   XATopicConnection xaTopicConnection = null;

   ConnectionFactory genericFactory = (ConnectionFactory) ic.lookup("/ConnectionFactory");
   genericConnection = genericFactory.createConnection();
   assertConnectionType(genericConnection, "generic");

   XAConnectionFactory xaFactory = (XAConnectionFactory) ic.lookup("/CF_XA_TRUE");
   xaConnection = xaFactory.createXAConnection();
   assertConnectionType(xaConnection, "xa");

   QueueConnectionFactory queueCF = (QueueConnectionFactory) ic.lookup("/CF_QUEUE");
   queueConnection = queueCF.createQueueConnection();
   assertConnectionType(queueConnection, "queue");

   TopicConnectionFactory topicCF = (TopicConnectionFactory) ic.lookup("/CF_TOPIC");
   topicConnection = topicCF.createTopicConnection();
   assertConnectionType(topicConnection, "topic");

   XAQueueConnectionFactory xaQueueCF = (XAQueueConnectionFactory) ic.lookup("/CF_QUEUE_XA_TRUE");
   xaQueueConnection = xaQueueCF.createXAQueueConnection();
   assertConnectionType(xaQueueConnection, "xa-queue");

   XATopicConnectionFactory xaTopicCF = (XATopicConnectionFactory) ic.lookup("/CF_TOPIC_XA_TRUE");
   xaTopicConnection = xaTopicCF.createXATopicConnection();
   assertConnectionType(xaTopicConnection, "xa-topic");

   genericConnection.close();
   xaConnection.close();
   queueConnection.close();
   topicConnection.close();
   xaQueueConnection.close();
   xaTopicConnection.close();

   undeployConnectionFactory("ConnectionFactory");
   undeployConnectionFactory("CF_QUEUE_XA_TRUE");
   undeployConnectionFactory("CF_XA_TRUE");
   undeployConnectionFactory("CF_QUEUE");
   undeployConnectionFactory("CF_TOPIC");
   undeployConnectionFactory("CF_TOPIC_XA_TRUE");
}
 
Example #19
Source File: ActiveMQXAConnectionFactoryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void assertXAConnection(Connection connection) {
   assertTrue("Should be an XAConnection", connection instanceof XAConnection);
   assertTrue("Should be an XATopicConnection", connection instanceof XATopicConnection);
   assertTrue("Should be an XAQueueConnection", connection instanceof XAQueueConnection);
}
 
Example #20
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Test
public void testSubscriberWithRollback() throws Exception {

    String subscriptionId = "sub-testSubscriberWithRollback";
    String topicName = "testSubscriberWithCommit";
    String testMessage = "testSubscriberWithCommit-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Setup XA consumer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    TopicSubscriber durableSubscriber = xaTopicSession.createDurableSubscriber(topic, subscriptionId);

    // Setup non-transactional message publisher.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    MessageProducer producer = topicSession.createProducer(topic);

    xaTopicConnection.start();

    producer.send(xaTopicSession.createTextMessage(testMessage));

    // Consume messages within a XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    TextMessage message = (TextMessage) durableSubscriber.receive(2000);
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.rollback(xid);

    Assert.assertNotNull(message, "Didn't receive a message");
    Assert.assertEquals(message.getText(), testMessage, "Received message content didn't match sent message.");

    topicSession.close();
    xaTopicSession.close();
    xaTopicConnection.close();

    QueueMetadata queueMetadata = restApiClient.getQueueMetadata("carbon:" + subscriptionId);
    Assert.assertEquals((int) queueMetadata.getSize(), 1, "Queue shouldn't be empty.");
}
 
Example #21
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Test
public void testSubscriberWithCommit() throws Exception {

    String subscriptionId = "sub-testSubscriberWithCommit";
    String topicName = "testSubscriberWithCommit";
    String testMessage = "testSubscriberWithCommit-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Create XA consumer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    TopicSubscriber durableSubscriber = xaTopicSession.createDurableSubscriber(topic, subscriptionId);

    // Create non transactional producer.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    MessageProducer producer = topicSession.createProducer(topic);

    xaTopicConnection.start();

    producer.send(xaTopicSession.createTextMessage(testMessage));

    // Consume message within a XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    TextMessage message = (TextMessage) durableSubscriber.receive(2000);
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.commit(xid, false);

    Assert.assertNotNull(message, "Didn't receive a message");
    Assert.assertEquals(message.getText(), testMessage, "Received message content didn't match sent message.");

    topicSession.close();
    xaTopicSession.close();
    xaTopicConnection.close();

    QueueMetadata queueMetadata = restApiClient.getQueueMetadata("carbon:" + subscriptionId);
    Assert.assertEquals((int) queueMetadata.getSize(), 0, "Queue should be empty.");
}
 
Example #22
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Test
public void testPublisherWithCommit() throws Exception {

    String subscriptionId = "sub-testPublisherWithCommit";
    String topicName = "testPublisherWithCommit";
    String testMessage = "testPublisherWithCommit-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Setup XA producer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    MessageProducer producer = xaTopicSession.createProducer(topic);

    // Setup non-transactional consumer.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicSubscriber durableSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);

    xaTopicConnection.start();

    // Send message within XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(xaTopicSession.createTextMessage(testMessage));
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.commit(xid, false);

    TextMessage message = (TextMessage) durableSubscriber.receive(2000);
    Assert.assertNotNull(message, "Didn't receive a message");
    Assert.assertEquals(message.getText(), testMessage, "Received message content didn't match sent message.");

    topicSession.close();
    xaTopicSession.close();
    xaTopicConnection.close();
}
 
Example #23
Source File: ManagedXAQueueTopicConnection.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
@Override
public XATopicSession createXATopicSession() throws JMSException {

    return addSession( ((XATopicConnection) connection).createXATopicSession());
}
 
Example #24
Source File: ManagedXATopicConnection.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
public ManagedXATopicConnection( XATopicConnection connection ) {

        super(connection);
        this.xaTopicConnection = connection;
    }