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());
}
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();
}
}
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);
}
});
}
}
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();
}
}
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();
}
}
Aggregations