Search in sources :

Example 11 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class QueueImpl method move.

private void move(final Transaction originalTX, final SimpleString address, final Binding binding, final MessageReference ref, final boolean rejectDuplicate, final AckReason reason) throws Exception {
    Transaction tx;
    if (originalTX != null) {
        tx = originalTX;
    } else {
        // if no TX we create a new one to commit at the end
        tx = new TransactionImpl(storageManager);
    }
    Message copyMessage = makeCopy(ref, reason == AckReason.EXPIRED);
    copyMessage.setAddress(address);
    postOffice.route(copyMessage, tx, false, rejectDuplicate, binding);
    acknowledge(tx, ref, reason);
    if (originalTX == null) {
        tx.commit();
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) Message(org.apache.activemq.artemis.api.core.Message) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) BindingsTransactionImpl(org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)

Example 12 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class ScaleDownHandler method scaleDownSNF.

private long scaleDownSNF(final SimpleString address, final Set<Queue> queues, final ClientProducer producer) throws Exception {
    long messageCount = 0;
    final String propertyEnd;
    // If this SNF is towards our targetNodeId
    boolean queueOnTarget = address.toString().endsWith(targetNodeId);
    if (queueOnTarget) {
        propertyEnd = targetNodeId;
    } else {
        propertyEnd = address.toString().substring(address.toString().lastIndexOf("."));
    }
    Transaction tx = new TransactionImpl(storageManager);
    for (Queue queue : queues) {
        // using auto-closeable
        try (LinkedListIterator<MessageReference> messagesIterator = queue.browserIterator()) {
            // loop through every message of this queue
            while (messagesIterator.hasNext()) {
                MessageReference messageRef = messagesIterator.next();
                Message message = messageRef.getMessage().copy();
                /* Here we are taking messages out of a store-and-forward queue and sending them to the corresponding
                * address on the scale-down target server.  However, we have to take the existing _AMQ_ROUTE_TOsf.*
                * property and put its value into the _AMQ_ROUTE_TO property so the message is routed properly.
                */
                byte[] oldRouteToIDs = null;
                List<SimpleString> propertiesToRemove = new ArrayList<>();
                message.removeProperty(Message.HDR_ROUTE_TO_IDS.toString());
                for (SimpleString propName : message.getPropertyNames()) {
                    if (propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
                        if (propName.toString().endsWith(propertyEnd)) {
                            oldRouteToIDs = message.getBytesProperty(propName.toString());
                        }
                        propertiesToRemove.add(propName);
                    }
                }
                for (SimpleString propertyToRemove : propertiesToRemove) {
                    message.removeProperty(propertyToRemove.toString());
                }
                if (queueOnTarget) {
                    message.putBytesProperty(Message.HDR_ROUTE_TO_IDS.toString(), oldRouteToIDs);
                } else {
                    message.putBytesProperty(Message.HDR_SCALEDOWN_TO_IDS.toString(), oldRouteToIDs);
                }
                logger.debug("Scaling down message " + message + " from " + address + " to " + message.getAddress() + " on node " + targetNodeId);
                producer.send(message.getAddress(), message);
                messageCount++;
                messagesIterator.remove();
                ackMessageOnQueue(tx, queue, messageRef);
            }
        } catch (NoSuchElementException ignored) {
        // this could happen through paging browsing
        }
    }
    tx.commit();
    return messageCount;
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) Queue(org.apache.activemq.artemis.core.server.Queue) NoSuchElementException(java.util.NoSuchElementException)

Example 13 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class BridgeTest method testWithDuplicates.

@Test
public void testWithDuplicates() throws Exception {
    Map<String, Object> server0Params = new HashMap<>();
    server0 = createClusteredServerWithParams(isNetty(), 0, true, server0Params);
    Map<String, Object> server1Params = new HashMap<>();
    addTargetParameters(server1Params);
    server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
    final String testAddress = "testAddress";
    final String queueName0 = "queue0";
    final String forwardAddress = "forwardAddress";
    final String queueName1 = "forwardQueue";
    Map<String, TransportConfiguration> connectors = new HashMap<>();
    TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
    TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
    connectors.put(server1tc.getName(), server1tc);
    server0.getConfiguration().setConnectorConfigurations(connectors);
    ArrayList<String> staticConnectors = new ArrayList<>();
    staticConnectors.add(server1tc.getName());
    BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName("bridge1").setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(100).setReconnectAttemptsOnSameNode(-1).setConfirmationWindowSize(0).setStaticConnectors(staticConnectors);
    List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
    bridgeConfigs.add(bridgeConfiguration);
    server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
    CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
    List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
    queueConfigs0.add(queueConfig0);
    server0.getConfiguration().setQueueConfigurations(queueConfigs0);
    server0.start();
    locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc, server1tc));
    ClientSessionFactory sf0 = locator.createSessionFactory(server0tc);
    ClientSession session0 = sf0.createSession(false, true, true);
    ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));
    final int numMessages = 1000;
    final SimpleString propKey = new SimpleString("testkey");
    final SimpleString selectorKey = new SimpleString("animal");
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[1024]);
        message.putIntProperty(propKey, i);
        message.putStringProperty(selectorKey, new SimpleString("monkey" + i));
        producer0.send(message);
    }
    server1.start();
    // Inserting the duplicateIDs so the bridge will fail in a few
    {
        long[] ids = new long[100];
        Queue queue = server0.locateQueue(new SimpleString(queueName0));
        LinkedListIterator<MessageReference> iterator = queue.iterator();
        for (int i = 0; i < 100; i++) {
            iterator.hasNext();
            ids[i] = iterator.next().getMessage().getMessageID();
        }
        iterator.close();
        DuplicateIDCache duplicateTargetCache = server1.getPostOffice().getDuplicateIDCache(PostOfficeImpl.BRIDGE_CACHE_STR.concat(forwardAddress));
        TransactionImpl tx = new TransactionImpl(server1.getStorageManager());
        for (long id : ids) {
            byte[] duplicateArray = BridgeImpl.getDuplicateBytes(server0.getNodeManager().getUUID(), id);
            duplicateTargetCache.addToCache(duplicateArray, tx);
        }
        tx.commit();
    }
    Thread.sleep(1000);
    ClientSessionFactory sf1 = locator.createSessionFactory(server1tc);
    ClientSession session1 = sf1.createSession(false, true, true);
    try {
        session1.createQueue(forwardAddress, RoutingType.ANYCAST, queueName1);
    } catch (Throwable ignored) {
        ignored.printStackTrace();
    }
    ClientConsumer consumer1 = session1.createConsumer(queueName1);
    session1.start();
    for (int i = 100; i < numMessages; i++) {
        ClientMessage message = consumer1.receive(5000);
        assertNotNull(message);
        assertEquals(i, message.getIntProperty(propKey).intValue());
        message.acknowledge();
    }
    session1.commit();
    Assert.assertNull(consumer1.receiveImmediate());
    consumer1.close();
    session1.deleteQueue(queueName1);
    session1.close();
    sf1.close();
    server1.stop();
    session0.close();
    sf0.close();
    closeFields();
    assertEquals(0, loadQueues(server0).size());
}
Also used : DuplicateIDCache(org.apache.activemq.artemis.core.postoffice.DuplicateIDCache) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) LinkedListIterator(org.apache.activemq.artemis.utils.collections.LinkedListIterator) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) Test(org.junit.Test)

Example 14 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class PagingCounterTest method testPrepareCounter.

@Test
public void testPrepareCounter() throws Exception {
    Xid xid = newXID();
    Queue queue = server.createQueue(new SimpleString("A1"), RoutingType.ANYCAST, new SimpleString("A1"), null, true, false);
    PageSubscriptionCounter counter = locateCounter(queue);
    StorageManager storage = server.getStorageManager();
    Transaction tx = new TransactionImpl(xid, server.getStorageManager(), 300);
    for (int i = 0; i < 2000; i++) {
        counter.increment(tx, 1, 1000);
    }
    assertEquals(0, counter.getValue());
    tx.prepare();
    storage.waitOnOperations();
    assertEquals(0, counter.getValue());
    server.stop();
    server = newActiveMQServer();
    server.start();
    storage = server.getStorageManager();
    queue = server.locateQueue(new SimpleString("A1"));
    assertNotNull(queue);
    counter = locateCounter(queue);
    tx = server.getResourceManager().removeTransaction(xid);
    assertNotNull(tx);
    assertEquals(0, counter.getValue());
    tx.commit(false);
    storage.waitOnOperations();
    assertEquals(2000, counter.getValue());
}
Also used : Xid(javax.transaction.xa.Xid) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PageSubscriptionCounter(org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) Queue(org.apache.activemq.artemis.core.server.Queue) Test(org.junit.Test)

Example 15 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class PagingCounterTest method testCounter.

@Test
public void testCounter() throws Exception {
    ClientSessionFactory sf = createSessionFactory(sl);
    ClientSession session = sf.createSession();
    try {
        server.addAddressInfo(new AddressInfo(new SimpleString("A1"), RoutingType.ANYCAST));
        Queue queue = server.createQueue(new SimpleString("A1"), RoutingType.ANYCAST, new SimpleString("A1"), null, true, false);
        PageSubscriptionCounter counter = locateCounter(queue);
        StorageManager storage = server.getStorageManager();
        Transaction tx = new TransactionImpl(server.getStorageManager());
        counter.increment(tx, 1, 1000);
        assertEquals(0, counter.getValue());
        assertEquals(0, counter.getPersistentSize());
        tx.commit();
        storage.waitOnOperations();
        assertEquals(1, counter.getValue());
        assertEquals(1000, counter.getPersistentSize());
    } finally {
        sf.close();
        session.close();
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PageSubscriptionCounter(org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) Queue(org.apache.activemq.artemis.core.server.Queue) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Aggregations

TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)31 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)24 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)11 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)11 Test (org.junit.Test)10 Queue (org.apache.activemq.artemis.core.server.Queue)9 Message (org.apache.activemq.artemis.api.core.Message)8 PageSubscriptionCounter (org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter)7 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)5 PageSubscription (org.apache.activemq.artemis.core.paging.cursor.PageSubscription)5 DuplicateIDCache (org.apache.activemq.artemis.core.postoffice.DuplicateIDCache)5 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)5 Map (java.util.Map)4 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)4 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)4 LinkedList (java.util.LinkedList)3 NoSuchElementException (java.util.NoSuchElementException)3