Search in sources :

Example 11 with XAConnectionFactory

use of javax.jms.XAConnectionFactory in project activemq-artemis by apache.

the class XAReceiveExample method main.

public static void main(final String[] args) throws Exception {
    XAConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the XA Connection Factory
        XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("XAConnectionFactory");
        // Step 4.Create a JMS XAConnection
        connection = cf.createXAConnection();
        // Step 5. Start the connection
        connection.start();
        // Step 6. Create a JMS XASession
        XASession xaSession = connection.createXASession();
        // Step 7. Create a normal session
        Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 8. Create a normal Message Producer
        MessageProducer normalProducer = normalSession.createProducer(queue);
        // Step 9. Get the JMS Session
        Session session = xaSession.getSession();
        // Step 10. Create a message consumer
        MessageConsumer xaConsumer = session.createConsumer(queue);
        // Step 11. Create two Text Messages
        TextMessage helloMessage = session.createTextMessage("hello");
        TextMessage worldMessage = session.createTextMessage("world");
        // Step 12. create a transaction
        Xid xid1 = new DummyXid("xa-example1".getBytes(StandardCharsets.US_ASCII), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
        // Step 13. Get the JMS XAResource
        XAResource xaRes = xaSession.getXAResource();
        // Step 14. Begin the Transaction work
        xaRes.start(xid1, XAResource.TMNOFLAGS);
        // Step 15. Send two messages.
        normalProducer.send(helloMessage);
        normalProducer.send(worldMessage);
        // Step 16. Receive the message
        TextMessage rm1 = (TextMessage) xaConsumer.receive();
        System.out.println("Message received: " + rm1.getText());
        TextMessage rm2 = (TextMessage) xaConsumer.receive();
        System.out.println("Message received: " + rm2.getText());
        // Step 17. Stop the work
        xaRes.end(xid1, XAResource.TMSUCCESS);
        // Step 18. Prepare
        xaRes.prepare(xid1);
        // Step 19. Roll back the transaction
        xaRes.rollback(xid1);
        // Step 20. Create another transaction
        Xid xid2 = new DummyXid("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
        // Step 21. Start the transaction
        xaRes.start(xid2, XAResource.TMNOFLAGS);
        // Step 22. receive those messages again
        rm1 = (TextMessage) xaConsumer.receive();
        System.out.println("Message received again: " + rm1.getText());
        rm2 = (TextMessage) xaConsumer.receive();
        System.out.println("Message received again: " + rm2.getText());
        // Step 23. Stop the work
        xaRes.end(xid2, XAResource.TMSUCCESS);
        // Step 24. Prepare
        xaRes.prepare(xid2);
        // Step 25. Commit!
        xaRes.commit(xid2, false);
        // Step 26. Check no more messages are received.
        TextMessage rm3 = (TextMessage) xaConsumer.receive(2000);
        if (rm3 == null) {
            System.out.println("No message received after commit.");
        } else {
            throw new IllegalStateException();
        }
    } finally {
        // Step 27. Be sure to close our JMS resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) InitialContext(javax.naming.InitialContext) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) XASession(javax.jms.XASession) XAConnectionFactory(javax.jms.XAConnectionFactory) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session)

Example 12 with XAConnectionFactory

use of javax.jms.XAConnectionFactory in project activemq-artemis by apache.

the class JMSBridgeImpl method createConnection.

private Connection createConnection(final String username, final String password, final ConnectionFactoryFactory cff, final String clientID, final boolean isXA, boolean isSource) throws Exception {
    Connection conn = null;
    try {
        Object cf = cff.createConnectionFactory();
        if (cf instanceof ActiveMQConnectionFactory && registry != null) {
            registry.register(XARecoveryConfig.newConfig((ActiveMQConnectionFactory) cf, username, password, null));
        }
        if (qualityOfServiceMode == QualityOfServiceMode.ONCE_AND_ONLY_ONCE && !(cf instanceof XAConnectionFactory)) {
            throw new IllegalArgumentException("Connection factory must be XAConnectionFactory");
        }
        if (username == null) {
            if (isXA) {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
                }
                conn = ((XAConnectionFactory) cf).createXAConnection();
            } else {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
                }
                conn = ((ConnectionFactory) cf).createConnection();
            }
        } else {
            if (isXA) {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
                }
                conn = ((XAConnectionFactory) cf).createXAConnection(username, password);
            } else {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
                }
                conn = ((ConnectionFactory) cf).createConnection(username, password);
            }
        }
        if (clientID != null) {
            conn.setClientID(clientID);
        }
        boolean ha = false;
        BridgeFailoverListener failoverListener = null;
        if (conn instanceof ActiveMQConnection) {
            ActiveMQConnectionFactory activeMQCF = (ActiveMQConnectionFactory) cf;
            ha = activeMQCF.isHA();
            if (ha) {
                ActiveMQConnection activeMQConn = (ActiveMQConnection) conn;
                failoverListener = new BridgeFailoverListener(isSource);
                activeMQConn.setFailoverListener(failoverListener);
            }
        }
        conn.setExceptionListener(new BridgeExceptionListener(ha, failoverListener, isSource));
        return conn;
    } catch (JMSException e) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Throwable ignored) {
        }
        throw e;
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) XAConnectionFactory(javax.jms.XAConnectionFactory) JMSException(javax.jms.JMSException)

Example 13 with XAConnectionFactory

use of javax.jms.XAConnectionFactory in project jbpm by kiegroup.

the class AsyncAuditLogProducerTest method startHornetQServer.

private void startHornetQServer() throws Exception {
    jmsServer = new EmbeddedJMS();
    jmsServer.start();
    logger.debug("Started Embedded JMS Server");
    XAConnectionFactory connectionFactory = (XAConnectionFactory) jmsServer.lookup("ConnectionFactory");
    new InitialContext().rebind("java:comp/UserTransaction", com.arjuna.ats.jta.UserTransaction.userTransaction());
    new InitialContext().rebind("java:comp/TransactionManager", com.arjuna.ats.jta.TransactionManager.transactionManager());
    new InitialContext().rebind("java:comp/TransactionSynchronizationRegistry", new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple());
    factory = new ConnectionFactoryProxy(connectionFactory, new TransactionHelperImpl(com.arjuna.ats.jta.TransactionManager.transactionManager()));
    queue = (Queue) jmsServer.lookup("/queue/exampleQueue");
}
Also used : ConnectionFactoryProxy(org.jboss.narayana.jta.jms.ConnectionFactoryProxy) EmbeddedJMS(org.hornetq.jms.server.embedded.EmbeddedJMS) TransactionHelperImpl(org.jboss.narayana.jta.jms.TransactionHelperImpl) XAConnectionFactory(javax.jms.XAConnectionFactory) InitialContext(javax.naming.InitialContext)

Example 14 with XAConnectionFactory

use of javax.jms.XAConnectionFactory in project jbpm by kiegroup.

the class JmsAvaiableJobExecutorTest method startHornetQServer.

private void startHornetQServer() throws Exception {
    jmsServer = new EmbeddedJMS();
    jmsServer.start();
    logger.debug("Started Embedded JMS Server");
    XAConnectionFactory connectionFactory = (XAConnectionFactory) jmsServer.lookup("ConnectionFactory");
    new InitialContext().rebind("java:comp/UserTransaction", com.arjuna.ats.jta.UserTransaction.userTransaction());
    new InitialContext().rebind("java:comp/TransactionManager", com.arjuna.ats.jta.TransactionManager.transactionManager());
    new InitialContext().rebind("java:comp/TransactionSynchronizationRegistry", new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple());
    factory = new ConnectionFactoryProxy(connectionFactory, new TransactionHelperImpl(com.arjuna.ats.jta.TransactionManager.transactionManager()));
    queue = (Queue) jmsServer.lookup("/queue/exampleQueue");
}
Also used : ConnectionFactoryProxy(org.jboss.narayana.jta.jms.ConnectionFactoryProxy) EmbeddedJMS(org.hornetq.jms.server.embedded.EmbeddedJMS) TransactionHelperImpl(org.jboss.narayana.jta.jms.TransactionHelperImpl) XAConnectionFactory(javax.jms.XAConnectionFactory) InitialContext(javax.naming.InitialContext)

Aggregations

XAConnectionFactory (javax.jms.XAConnectionFactory)14 ConnectionFactory (javax.jms.ConnectionFactory)7 Test (org.junit.Test)6 XAConnection (javax.jms.XAConnection)5 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)5 InitialContext (javax.naming.InitialContext)4 Connection (javax.jms.Connection)3 MessageConsumer (javax.jms.MessageConsumer)3 MessageProducer (javax.jms.MessageProducer)3 Queue (javax.jms.Queue)3 Session (javax.jms.Session)3 TextMessage (javax.jms.TextMessage)3 XASession (javax.jms.XASession)3 ConnectionFactoryProxy (org.jboss.narayana.jta.jms.ConnectionFactoryProxy)3 TransactionHelperImpl (org.jboss.narayana.jta.jms.TransactionHelperImpl)3 QueueConnectionFactory (javax.jms.QueueConnectionFactory)2 TopicConnectionFactory (javax.jms.TopicConnectionFactory)2 XAQueueConnectionFactory (javax.jms.XAQueueConnectionFactory)2 XATopicConnectionFactory (javax.jms.XATopicConnectionFactory)2 XAResource (javax.transaction.xa.XAResource)2