Search in sources :

Example 6 with Transaction

use of org.apache.qpid.server.store.Transaction in project qpid-broker-j by apache.

the class AutoCommitTransaction method enqueue.

@Override
public void enqueue(Collection<? extends BaseQueue> queues, EnqueueableMessage message, EnqueueAction postTransactionAction) {
    Transaction txn = null;
    try {
        MessageEnqueueRecord[] enqueueRecords = new MessageEnqueueRecord[queues.size()];
        int i = 0;
        for (BaseQueue queue : queues) {
            if (queue.getMessageDurability().persist(message.isPersistent())) {
                LOGGER.debug("Enqueue of message number {} to transaction log. Queue : {}", message.getMessageNumber(), queue.getName());
                if (txn == null) {
                    txn = _messageStore.newTransaction();
                }
                enqueueRecords[i] = txn.enqueueMessage(queue, message);
            }
            i++;
        }
        if (txn != null) {
            txn.commitTran();
            txn = null;
        }
        if (postTransactionAction != null) {
            postTransactionAction.postCommit(enqueueRecords);
        }
        postTransactionAction = null;
    } finally {
        final EnqueueAction underlying = postTransactionAction;
        rollbackIfNecessary(new Action() {

            @Override
            public void postCommit() {
            }

            @Override
            public void onRollback() {
                if (underlying != null) {
                    underlying.onRollback();
                }
            }
        }, txn);
    }
}
Also used : Transaction(org.apache.qpid.server.store.Transaction) BaseQueue(org.apache.qpid.server.queue.BaseQueue) MessageEnqueueRecord(org.apache.qpid.server.store.MessageEnqueueRecord)

Example 7 with Transaction

use of org.apache.qpid.server.store.Transaction in project qpid-broker-j by apache.

the class DtxBranch method prepare.

public void prepare() throws StoreException {
    LOGGER.debug("Performing prepare for DtxBranch {}", _xid);
    Transaction txn = _dtxRegistry.getMessageStore().newTransaction();
    _storedXidRecord = txn.recordXid(_xid.getFormat(), _xid.getGlobalId(), _xid.getBranchId(), _enqueueRecords.toArray(new EnqueueRecord[_enqueueRecords.size()]), _dequeueRecords.toArray(new DequeueRecord[_dequeueRecords.size()]));
    txn.commitTran();
    prePrepareTransaction();
}
Also used : Transaction(org.apache.qpid.server.store.Transaction)

Example 8 with Transaction

use of org.apache.qpid.server.store.Transaction in project qpid-broker-j by apache.

the class SynchronousMessageStoreRecovererTest method testRecoveryOfMessageInstanceForNonExistingQueue.

public void testRecoveryOfMessageInstanceForNonExistingQueue() {
    final UUID queueId = UUID.randomUUID();
    final Transaction transaction = mock(Transaction.class);
    final long messageId = 1;
    final StoredMessage<StorableMessageMetaData> storedMessage = createMockStoredMessage(messageId);
    MessageStore store = new NullMessageStore() {

        @Override
        public void visitMessages(MessageHandler handler) throws StoreException {
            handler.handle(storedMessage);
        }

        @Override
        public void visitMessageInstances(MessageInstanceHandler handler) throws StoreException {
            handler.handle(new TestMessageEnqueueRecord(queueId, messageId));
        }

        @Override
        public Transaction newTransaction() {
            return transaction;
        }
    };
    when(_virtualHost.getMessageStore()).thenReturn(store);
    SynchronousMessageStoreRecoverer recoverer = new SynchronousMessageStoreRecoverer();
    recoverer.recover(_virtualHost);
    verify(transaction).dequeueMessage(argThat(new MessageEnqueueRecordMatcher(queueId, messageId)));
    verify(transaction, times(1)).commitTranAsync((Void) null);
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) Transaction(org.apache.qpid.server.store.Transaction) MessageHandler(org.apache.qpid.server.store.handler.MessageHandler) MessageInstanceHandler(org.apache.qpid.server.store.handler.MessageInstanceHandler) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) UUID(java.util.UUID) StorableMessageMetaData(org.apache.qpid.server.store.StorableMessageMetaData)

Example 9 with Transaction

use of org.apache.qpid.server.store.Transaction in project qpid-broker-j by apache.

the class SynchronousMessageStoreRecovererTest method testRecoveryOfSingleDequeueWithDistributedTransaction.

public void testRecoveryOfSingleDequeueWithDistributedTransaction() {
    final UUID queueId = UUID.randomUUID();
    final Queue<?> queue = createRegisteredMockQueue(queueId);
    final Transaction transaction = mock(Transaction.class);
    final StoredMessage<StorableMessageMetaData> storedMessage = createMockStoredMessage(1);
    final long messageId = storedMessage.getMessageNumber();
    Transaction.DequeueRecord dequeueRecord = createMockDequeueRecord(queueId, messageId);
    QueueEntry queueEntry = mock(QueueEntry.class);
    when(queueEntry.acquire()).thenReturn(true);
    when(queue.getMessageOnTheQueue(messageId)).thenReturn(queueEntry);
    final long format = 1;
    final byte[] globalId = new byte[] { 0 };
    final byte[] branchId = new byte[] { 0 };
    final EnqueueRecord[] enqueues = {};
    final Transaction.DequeueRecord[] dequeues = { dequeueRecord };
    MessageStore store = new NullMessageStore() {

        @Override
        public void visitMessages(MessageHandler handler) throws StoreException {
            handler.handle(storedMessage);
        }

        @Override
        public void visitMessageInstances(MessageInstanceHandler handler) throws StoreException {
            // We need the message to be enqueued onto the queue so that later the distributed transaction
            // can dequeue it.
            handler.handle(new TestMessageEnqueueRecord(queue.getId(), messageId));
        }

        @Override
        public void visitDistributedTransactions(DistributedTransactionHandler handler) throws StoreException {
            handler.handle(new Transaction.StoredXidRecord() {

                @Override
                public long getFormat() {
                    return format;
                }

                @Override
                public byte[] getGlobalId() {
                    return globalId;
                }

                @Override
                public byte[] getBranchId() {
                    return branchId;
                }
            }, enqueues, dequeues);
        }

        @Override
        public Transaction newTransaction() {
            return transaction;
        }
    };
    DtxRegistry dtxRegistry = new DtxRegistry(_virtualHost);
    when(_virtualHost.getMessageStore()).thenReturn(store);
    when(_virtualHost.getDtxRegistry()).thenReturn(dtxRegistry);
    SynchronousMessageStoreRecoverer recoverer = new SynchronousMessageStoreRecoverer();
    recoverer.recover(_virtualHost);
    DtxBranch branch = dtxRegistry.getBranch(new Xid(format, globalId, branchId));
    assertNotNull("Expected dtx branch to be created", branch);
    branch.commit();
    verify(queueEntry, times(1)).delete();
    verify(transaction).commitTran();
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) MessageHandler(org.apache.qpid.server.store.handler.MessageHandler) EnqueueRecord(org.apache.qpid.server.store.Transaction.EnqueueRecord) MessageEnqueueRecord(org.apache.qpid.server.store.MessageEnqueueRecord) UUID(java.util.UUID) StorableMessageMetaData(org.apache.qpid.server.store.StorableMessageMetaData) DistributedTransactionHandler(org.apache.qpid.server.store.handler.DistributedTransactionHandler) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) QueueEntry(org.apache.qpid.server.queue.QueueEntry) Xid(org.apache.qpid.server.txn.Xid) Transaction(org.apache.qpid.server.store.Transaction) DtxRegistry(org.apache.qpid.server.txn.DtxRegistry) MessageInstanceHandler(org.apache.qpid.server.store.handler.MessageInstanceHandler) DtxBranch(org.apache.qpid.server.txn.DtxBranch)

Example 10 with Transaction

use of org.apache.qpid.server.store.Transaction in project qpid-broker-j by apache.

the class JDBCMessageStoreTest method testEnqueueTransactionCommitAsync.

public void testEnqueueTransactionCommitAsync() throws Exception {
    final String queueName = getTestName();
    final UUID transactionalLogId = UUID.randomUUID();
    final MessageStore store = getStore();
    final TransactionLogResource transactionalLog = mockTransactionLogResource(transactionalLogId, queueName);
    final InternalMessage message = addTestMessage(store, queueName, "test");
    final Transaction transaction = store.newTransaction();
    final MessageEnqueueRecord record = transaction.enqueueMessage(transactionalLog, message);
    assertNotNull("Message enqueue record should not be null", record);
    assertEquals("Unexpected queue id", transactionalLogId, record.getQueueId());
    assertEquals("Unexpected message number", message.getMessageNumber(), record.getMessageNumber());
    final ListenableFuture<Void> future = transaction.commitTranAsync(null);
    future.get(1000, TimeUnit.MILLISECONDS);
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) InternalMessage(org.apache.qpid.server.message.internal.InternalMessage) Transaction(org.apache.qpid.server.store.Transaction) UUID(java.util.UUID) MessageEnqueueRecord(org.apache.qpid.server.store.MessageEnqueueRecord) TransactionLogResource(org.apache.qpid.server.store.TransactionLogResource)

Aggregations

Transaction (org.apache.qpid.server.store.Transaction)20 MessageEnqueueRecord (org.apache.qpid.server.store.MessageEnqueueRecord)12 MessageStore (org.apache.qpid.server.store.MessageStore)6 UUID (java.util.UUID)5 NullMessageStore (org.apache.qpid.server.store.NullMessageStore)4 MessageHandler (org.apache.qpid.server.store.handler.MessageHandler)4 MessageInstanceHandler (org.apache.qpid.server.store.handler.MessageInstanceHandler)4 StorableMessageMetaData (org.apache.qpid.server.store.StorableMessageMetaData)3 TransactionLogResource (org.apache.qpid.server.store.TransactionLogResource)3 EnqueueableMessage (org.apache.qpid.server.message.EnqueueableMessage)2 MessageInstance (org.apache.qpid.server.message.MessageInstance)2 MessageReference (org.apache.qpid.server.message.MessageReference)2 InternalMessage (org.apache.qpid.server.message.internal.InternalMessage)2 ContentHeaderBody (org.apache.qpid.server.protocol.v0_8.transport.ContentHeaderBody)2 MessagePublishInfo (org.apache.qpid.server.protocol.v0_8.transport.MessagePublishInfo)2 BaseQueue (org.apache.qpid.server.queue.BaseQueue)2 StoredMessage (org.apache.qpid.server.store.StoredMessage)2 EnqueueRecord (org.apache.qpid.server.store.Transaction.EnqueueRecord)2 DistributedTransactionHandler (org.apache.qpid.server.store.handler.DistributedTransactionHandler)2 DtxBranch (org.apache.qpid.server.txn.DtxBranch)2