Search in sources :

Example 16 with ActiveMQRAConnectionFactory

use of org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory in project activemq-artemis by apache.

the class OutgoingConnectionNoJTATest method testSimpleMessageSendAndReceiveSessionTransacted.

@Test
public void testSimpleMessageSendAndReceiveSessionTransacted() throws Exception {
    setupDLQ(10);
    resourceAdapter = newResourceAdapter();
    MyBootstrapContext ctx = new MyBootstrapContext();
    resourceAdapter.start(ctx);
    ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
    mcf.setAllowLocalTransactions(true);
    mcf.setResourceAdapter(resourceAdapter);
    ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
    QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
    Session s = queueConnection.createSession(true, Session.SESSION_TRANSACTED);
    Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
    MessageProducer mp = s.createProducer(q);
    MessageConsumer consumer = s.createConsumer(q);
    Message message = s.createTextMessage("test");
    mp.send(message);
    s.commit();
    queueConnection.start();
    TextMessage textMessage = (TextMessage) consumer.receive(1000);
    assertNotNull(textMessage);
    assertEquals(textMessage.getText(), "test");
    s.rollback();
    textMessage = (TextMessage) consumer.receive(1000);
    assertNotNull(textMessage);
    assertEquals(textMessage.getText(), "test");
    s.commit();
    textMessage = (TextMessage) consumer.receiveNoWait();
    assertNull(textMessage);
}
Also used : ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) QueueConnection(javax.jms.QueueConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 17 with ActiveMQRAConnectionFactory

use of org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory in project wildfly by wildfly.

the class ExternalJMSTopicService method start.

@Override
public synchronized void start(final StartContext context) throws StartException {
    NamingStore namingStore = namingStoreInjector.getOptionalValue();
    if (namingStore != null) {
        final Queue managementQueue = config.getManagementQueue();
        final NamingContext storeBaseContext = new NamingContext(namingStore, null);
        try {
            ConnectionFactory cf = (ConnectionFactory) storeBaseContext.lookup(pcfInjector.getValue().getBindInfo().getAbsoluteJndiName());
            if (cf instanceof ActiveMQRAConnectionFactory) {
                final ActiveMQRAConnectionFactory raCf = (ActiveMQRAConnectionFactory) cf;
                final ServerLocator locator = raCf.getDefaultFactory().getServerLocator();
                final ClientProtocolManagerFactory protocolManagerFactory = locator.getProtocolManagerFactory();
                sessionFactory = locator.createSessionFactory();
                ClusterTopologyListener listener = new ClusterTopologyListener() {

                    @Override
                    public void nodeUP(TopologyMember member, boolean last) {
                        try (ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(false, member.getLive())) {
                            factory.getServerLocator().setProtocolManagerFactory(protocolManagerFactory);
                            MessagingLogger.ROOT_LOGGER.infof("Creating topic %s on node UP %s - %s", topicName, member.getNodeId(), member.getLive().toJson());
                            config.createTopic(factory, managementQueue, topicName);
                        } catch (JMSException | StartException ex) {
                            MessagingLogger.ROOT_LOGGER.errorf(ex, "Creating topic %s on node UP %s failed", topicName, member.getLive().toJson());
                            throw new RuntimeException(ex);
                        }
                        if (member.getBackup() != null) {
                            try (ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(false, member.getBackup())) {
                                factory.getServerLocator().setProtocolManagerFactory(protocolManagerFactory);
                                MessagingLogger.ROOT_LOGGER.infof("Creating topic %s on backup node UP %s - %s", topicName, member.getNodeId(), member.getBackup().toJson());
                                config.createTopic(factory, managementQueue, topicName);
                            } catch (JMSException | StartException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }

                    @Override
                    public void nodeDown(long eventUID, String nodeID) {
                    }
                };
                locator.addClusterTopologyListener(listener);
                Collection<TopologyMemberImpl> members = locator.getTopology().getMembers();
                if (members == null || members.isEmpty()) {
                    config.createTopic(cf, managementQueue, topicName);
                }
            } else {
                config.createTopic(cf, managementQueue, topicName);
            }
        } catch (Exception ex) {
            MessagingLogger.ROOT_LOGGER.errorf(ex, "Error starting the external queue service %s", ex.getMessage());
            throw new StartException(ex);
        } finally {
            try {
                storeBaseContext.close();
            } catch (NamingException ex) {
                MessagingLogger.ROOT_LOGGER.tracef(ex, "Error closing the naming context %s", ex.getMessage());
            }
        }
    }
    topic = ActiveMQDestination.createTopic(topicName);
}
Also used : NamingStore(org.jboss.as.naming.NamingStore) TopologyMemberImpl(org.apache.activemq.artemis.core.client.impl.TopologyMemberImpl) ClusterTopologyListener(org.apache.activemq.artemis.api.core.client.ClusterTopologyListener) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) JMSException(javax.jms.JMSException) NamingContext(org.jboss.as.naming.NamingContext) NamingException(javax.naming.NamingException) StartException(org.jboss.msc.service.StartException) JMSException(javax.jms.JMSException) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ClientProtocolManagerFactory(org.apache.activemq.artemis.spi.core.remoting.ClientProtocolManagerFactory) StartException(org.jboss.msc.service.StartException) NamingException(javax.naming.NamingException) TopologyMember(org.apache.activemq.artemis.api.core.client.TopologyMember) Queue(javax.jms.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Aggregations

ActiveMQRAConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory)17 ActiveMQRAConnectionFactoryImpl (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl)15 ActiveMQRAManagedConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory)15 QueueConnection (javax.jms.QueueConnection)14 Test (org.junit.Test)14 Session (javax.jms.Session)9 XAQueueConnection (javax.jms.XAQueueConnection)8 Queue (javax.jms.Queue)7 MessageProducer (javax.jms.MessageProducer)6 QueueSession (javax.jms.QueueSession)6 JMSException (javax.jms.JMSException)5 Message (javax.jms.Message)5 MessageConsumer (javax.jms.MessageConsumer)5 TextMessage (javax.jms.TextMessage)5 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)5 ActiveMQRAManagedConnection (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection)5 ActiveMQRASession (org.apache.activemq.artemis.ra.ActiveMQRASession)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 XASession (javax.jms.XASession)3