Search in sources :

Example 31 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction 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 32 with Transaction

use of org.apache.activemq.artemis.core.transaction.Transaction 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)

Example 33 with Transaction

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

the class PostOfficeImpl method processRoute.

@Override
public void processRoute(final Message message, final RoutingContext context, final boolean direct) throws Exception {
    final List<MessageReference> refs = new ArrayList<>();
    Transaction tx = context.getTransaction();
    Long deliveryTime = message.getScheduledDeliveryTime();
    for (Map.Entry<SimpleString, RouteContextList> entry : context.getContexListing().entrySet()) {
        PagingStore store = pagingManager.getPageStore(entry.getKey());
        if (storageManager.addToPage(store, message, context.getTransaction(), entry.getValue())) {
            if (message.isLargeMessage()) {
                confirmLargeMessageSend(tx, message);
            }
            // We need to kick delivery so the Queues may check for the cursors case they are empty
            schedulePageDelivery(tx, entry);
            continue;
        }
        for (Queue queue : entry.getValue().getNonDurableQueues()) {
            MessageReference reference = MessageReference.Factory.createReference(message, queue);
            if (deliveryTime != null) {
                reference.setScheduledDeliveryTime(deliveryTime);
            }
            refs.add(reference);
            message.incrementRefCount();
        }
        Iterator<Queue> iter = entry.getValue().getDurableQueues().iterator();
        while (iter.hasNext()) {
            Queue queue = iter.next();
            MessageReference reference = MessageReference.Factory.createReference(message, queue);
            if (context.isAlreadyAcked(context.getAddress(message), queue)) {
                reference.setAlreadyAcked();
                if (tx != null) {
                    queue.acknowledge(tx, reference);
                }
            }
            if (deliveryTime != null) {
                reference.setScheduledDeliveryTime(deliveryTime);
            }
            refs.add(reference);
            if (message.isDurable()) {
                int durableRefCount = message.incrementDurableRefCount();
                if (durableRefCount == 1) {
                    if (tx != null) {
                        storageManager.storeMessageTransactional(tx.getID(), message);
                    } else {
                        storageManager.storeMessage(message);
                    }
                    if (message.isLargeMessage()) {
                        confirmLargeMessageSend(tx, message);
                    }
                }
                if (tx != null) {
                    storageManager.storeReferenceTransactional(tx.getID(), queue.getID(), message.getMessageID());
                    tx.setContainsPersistent();
                } else {
                    storageManager.storeReference(queue.getID(), message.getMessageID(), !iter.hasNext());
                }
                if (deliveryTime > 0) {
                    if (tx != null) {
                        storageManager.updateScheduledDeliveryTimeTransactional(tx.getID(), reference);
                    } else {
                        storageManager.updateScheduledDeliveryTime(reference);
                    }
                }
            }
            message.incrementRefCount();
        }
    }
    if (tx != null) {
        tx.addOperation(new AddOperation(refs));
    } else {
        // This will use the same thread if there are no pending operations
        // avoiding a context switch on this case
        storageManager.afterCompleteOperations(new IOCallback() {

            @Override
            public void onError(final int errorCode, final String errorMessage) {
                ActiveMQServerLogger.LOGGER.ioErrorAddingReferences(errorCode, errorMessage);
            }

            @Override
            public void done() {
                addReferences(refs, direct);
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) RouteContextList(org.apache.activemq.artemis.core.server.RouteContextList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore) Queue(org.apache.activemq.artemis.core.server.Queue)

Example 34 with Transaction

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

the class ActiveMQServerControlImpl method listPreparedTransactionDetailsAsJSON.

public String listPreparedTransactionDetailsAsJSON(TransactionDetailFactory factory) throws Exception {
    checkStarted();
    clearIO();
    try {
        Map<Xid, Long> xids = resourceManager.getPreparedTransactionsWithCreationTime();
        if (xids == null || xids.size() == 0) {
            return "";
        }
        ArrayList<Entry<Xid, Long>> xidsSortedByCreationTime = new ArrayList<>(xids.entrySet());
        Collections.sort(xidsSortedByCreationTime, new Comparator<Entry<Xid, Long>>() {

            @Override
            public int compare(final Entry<Xid, Long> entry1, final Entry<Xid, Long> entry2) {
                // sort by creation time, oldest first
                return (int) (entry1.getValue() - entry2.getValue());
            }
        });
        JsonArrayBuilder txDetailListJson = JsonLoader.createArrayBuilder();
        for (Map.Entry<Xid, Long> entry : xidsSortedByCreationTime) {
            Xid xid = entry.getKey();
            Transaction tx = resourceManager.getTransaction(xid);
            if (tx == null) {
                continue;
            }
            TransactionDetail detail = factory.createTransactionDetail(xid, tx, entry.getValue());
            txDetailListJson.add(detail.toJSON());
        }
        return txDetailListJson.build().toString();
    } finally {
        blockOnIO();
    }
}
Also used : ArrayList(java.util.ArrayList) Xid(javax.transaction.xa.Xid) Entry(java.util.Map.Entry) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) AtomicLong(java.util.concurrent.atomic.AtomicLong) CoreTransactionDetail(org.apache.activemq.artemis.core.transaction.impl.CoreTransactionDetail) TransactionDetail(org.apache.activemq.artemis.core.transaction.TransactionDetail) JsonArrayBuilder(javax.json.JsonArrayBuilder) Map(java.util.Map)

Example 35 with Transaction

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

the class ActiveMQServerControlImpl method listPreparedTransactionDetailsAsHTML.

public String listPreparedTransactionDetailsAsHTML(TransactionDetailFactory factory) throws Exception {
    checkStarted();
    clearIO();
    try {
        Map<Xid, Long> xids = resourceManager.getPreparedTransactionsWithCreationTime();
        if (xids == null || xids.size() == 0) {
            return "<h3>*** Prepared Transaction Details ***</h3><p>No entry.</p>";
        }
        ArrayList<Entry<Xid, Long>> xidsSortedByCreationTime = new ArrayList<>(xids.entrySet());
        Collections.sort(xidsSortedByCreationTime, new Comparator<Entry<Xid, Long>>() {

            @Override
            public int compare(final Entry<Xid, Long> entry1, final Entry<Xid, Long> entry2) {
                // sort by creation time, oldest first
                return (int) (entry1.getValue() - entry2.getValue());
            }
        });
        StringBuilder html = new StringBuilder();
        html.append("<h3>*** Prepared Transaction Details ***</h3>");
        for (Map.Entry<Xid, Long> entry : xidsSortedByCreationTime) {
            Xid xid = entry.getKey();
            Transaction tx = resourceManager.getTransaction(xid);
            if (tx == null) {
                continue;
            }
            TransactionDetail detail = factory.createTransactionDetail(xid, tx, entry.getValue());
            JsonObject txJson = detail.toJSON();
            html.append("<table border=\"1\">");
            html.append("<tr><th>creation_time</th>");
            html.append("<td>" + txJson.get(TransactionDetail.KEY_CREATION_TIME) + "</td>");
            html.append("<th>xid_as_base_64</th>");
            html.append("<td colspan=\"3\">" + txJson.get(TransactionDetail.KEY_XID_AS_BASE64) + "</td></tr>");
            html.append("<tr><th>xid_format_id</th>");
            html.append("<td>" + txJson.get(TransactionDetail.KEY_XID_FORMAT_ID) + "</td>");
            html.append("<th>xid_global_txid</th>");
            html.append("<td>" + txJson.get(TransactionDetail.KEY_XID_GLOBAL_TXID) + "</td>");
            html.append("<th>xid_branch_qual</th>");
            html.append("<td>" + txJson.get(TransactionDetail.KEY_XID_BRANCH_QUAL) + "</td></tr>");
            html.append("<tr><th colspan=\"6\">Message List</th></tr>");
            html.append("<tr><td colspan=\"6\">");
            html.append("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">");
            JsonArray msgs = txJson.getJsonArray(TransactionDetail.KEY_TX_RELATED_MESSAGES);
            for (int i = 0; i < msgs.size(); i++) {
                JsonObject msgJson = msgs.getJsonObject(i);
                JsonObject props = msgJson.getJsonObject(TransactionDetail.KEY_MSG_PROPERTIES);
                StringBuilder propstr = new StringBuilder();
                Set<String> keys = props.keySet();
                for (String key : keys) {
                    propstr.append(key);
                    propstr.append("=");
                    propstr.append(props.get(key));
                    propstr.append(", ");
                }
                html.append("<th>operation_type</th>");
                html.append("<td>" + msgJson.get(TransactionDetail.KEY_MSG_OP_TYPE) + "</th>");
                html.append("<th>message_type</th>");
                html.append("<td>" + msgJson.get(TransactionDetail.KEY_MSG_TYPE) + "</td></tr>");
                html.append("<tr><th>properties</th>");
                html.append("<td colspan=\"3\">" + propstr.toString() + "</td></tr>");
            }
            html.append("</table></td></tr>");
            html.append("</table><br>");
        }
        return html.toString();
    } finally {
        blockOnIO();
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JsonArray(javax.json.JsonArray) Xid(javax.transaction.xa.Xid) Entry(java.util.Map.Entry) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) AtomicLong(java.util.concurrent.atomic.AtomicLong) CoreTransactionDetail(org.apache.activemq.artemis.core.transaction.impl.CoreTransactionDetail) TransactionDetail(org.apache.activemq.artemis.core.transaction.TransactionDetail) Map(java.util.Map)

Aggregations

Transaction (org.apache.activemq.artemis.core.transaction.Transaction)52 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)24 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)14 Test (org.junit.Test)14 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)13 Xid (javax.transaction.xa.Xid)12 Queue (org.apache.activemq.artemis.core.server.Queue)11 ArrayList (java.util.ArrayList)10 Message (org.apache.activemq.artemis.api.core.Message)10 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)9 Map (java.util.Map)8 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)8 HashMap (java.util.HashMap)7 PageSubscriptionCounter (org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter)7 PageSubscription (org.apache.activemq.artemis.core.paging.cursor.PageSubscription)6 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4