Search in sources :

Example 1 with LocalTransaction

use of org.apache.qpid.server.txn.LocalTransaction in project qpid-broker-j by apache.

the class AbstractQueue method clearQueue.

// ------ Management functions
@Override
public long clearQueue() {
    QueueEntryIterator queueListIterator = getEntries().iterator();
    long count = 0;
    ServerTransaction txn = new LocalTransaction(getVirtualHost().getMessageStore());
    while (queueListIterator.advance()) {
        final QueueEntry node = queueListIterator.getNode();
        boolean acquired = node.acquireOrSteal(new Runnable() {

            @Override
            public void run() {
                dequeueEntry(node);
            }
        });
        if (acquired) {
            dequeueEntry(node, txn);
        }
    }
    txn.commit();
    return count;
}
Also used : LocalTransaction(org.apache.qpid.server.txn.LocalTransaction) ServerTransaction(org.apache.qpid.server.txn.ServerTransaction)

Example 2 with LocalTransaction

use of org.apache.qpid.server.txn.LocalTransaction in project qpid-broker-j by apache.

the class AMQChannel method receiveTxSelect.

@Override
public void receiveTxSelect() {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV[" + _channelId + "] TxSelect");
    }
    ServerTransaction txn = _transaction;
    if (txn instanceof LocalTransaction) {
        getConnection().unregisterTransactionTickers(_transaction);
    }
    _transaction = _connection.createLocalTransaction();
    long notificationRepeatPeriod = getContextValue(Long.class, TRANSACTION_TIMEOUT_NOTIFICATION_REPEAT_PERIOD);
    getConnection().registerTransactionTickers(_transaction, message -> _connection.sendConnectionCloseAsync(AMQPConnection.CloseReason.TRANSACTION_TIMEOUT, message), notificationRepeatPeriod);
    MethodRegistry methodRegistry = _connection.getMethodRegistry();
    TxSelectOkBody responseBody = methodRegistry.createTxSelectOkBody();
    _connection.writeFrame(responseBody.generateFrame(_channelId));
}
Also used : LocalTransaction(org.apache.qpid.server.txn.LocalTransaction) ServerTransaction(org.apache.qpid.server.txn.ServerTransaction)

Example 3 with LocalTransaction

use of org.apache.qpid.server.txn.LocalTransaction in project qpid-broker-j by apache.

the class AMQChannel method close.

public void close(int cause, String message) {
    if (!_closing.compareAndSet(false, true)) {
        // Channel is already closing
        return;
    }
    try {
        unsubscribeAllConsumers();
        setDefaultQueue(null);
        for (Action<? super AMQChannel> task : _taskList) {
            task.performAction(this);
        }
        if (_transaction instanceof LocalTransaction) {
            if (((LocalTransaction) _transaction).hasOutstandingWork()) {
                _connection.incrementTransactionRollbackCounter();
            }
            _connection.decrementTransactionOpenCounter();
            _connection.unregisterTransactionTickers(_transaction);
        }
        _transaction.rollback();
        requeue();
    } finally {
        dispose();
        LogMessage operationalLogMessage = cause == 0 ? ChannelMessages.CLOSE() : ChannelMessages.CLOSE_FORCED(cause, message);
        messageWithSubject(operationalLogMessage);
    }
}
Also used : LocalTransaction(org.apache.qpid.server.txn.LocalTransaction) LogMessage(org.apache.qpid.server.logging.LogMessage)

Example 4 with LocalTransaction

use of org.apache.qpid.server.txn.LocalTransaction in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method createIdentifiedTransaction.

@Override
public IdentifiedTransaction createIdentifiedTransaction() {
    ServerTransaction[] openTransactions = _openTransactions;
    final int maxOpenTransactions = openTransactions.length;
    int id = 0;
    for (; id < maxOpenTransactions; id++) {
        if (openTransactions[id] == null) {
            break;
        }
    }
    // we need to expand the transaction array;
    if (id == maxOpenTransactions) {
        final int newSize = maxOpenTransactions < 1024 ? 2 * maxOpenTransactions : maxOpenTransactions + 1024;
        _openTransactions = new ServerTransaction[newSize];
        System.arraycopy(openTransactions, 0, _openTransactions, 0, maxOpenTransactions);
    }
    final LocalTransaction serverTransaction = createLocalTransaction();
    _openTransactions[id] = serverTransaction;
    return new IdentifiedTransaction(id, serverTransaction);
}
Also used : LocalTransaction(org.apache.qpid.server.txn.LocalTransaction) ServerTransaction(org.apache.qpid.server.txn.ServerTransaction)

Example 5 with LocalTransaction

use of org.apache.qpid.server.txn.LocalTransaction in project qpid-broker-j by apache.

the class StandardReceivingLinkEndpoint method receiveDelivery.

@Override
protected Error receiveDelivery(Delivery delivery) {
    ReceiverSettleMode transferReceiverSettleMode = delivery.getReceiverSettleMode();
    if (delivery.getResume()) {
        DeliveryState deliveryState = _unsettled.get(delivery.getDeliveryTag());
        if (deliveryState instanceof Outcome) {
            boolean settled = shouldReceiverSettleFirst(transferReceiverSettleMode);
            updateDisposition(delivery.getDeliveryTag(), deliveryState, settled);
            return null;
        } else {
        // TODO: QPID-7845: create message ?
        }
    } else {
        ServerMessage<?> serverMessage;
        UnsignedInteger messageFormat = delivery.getMessageFormat();
        DeliveryState xfrState = delivery.getState();
        MessageFormat format = MessageFormatRegistry.getFormat(messageFormat.intValue());
        if (format != null) {
            try (QpidByteBuffer payload = delivery.getPayload()) {
                serverMessage = format.createMessage(payload, getAddressSpace().getMessageStore(), getSession().getConnection().getReference());
            } catch (AmqpErrorRuntimeException e) {
                return e.getCause().getError();
            }
        } else {
            final Error err = new Error();
            err.setCondition(AmqpError.NOT_IMPLEMENTED);
            err.setDescription("Unknown message format: " + messageFormat);
            return err;
        }
        MessageReference<?> reference = serverMessage.newReference();
        try {
            Binary transactionId = null;
            if (xfrState != null) {
                if (xfrState instanceof TransactionalState) {
                    transactionId = ((TransactionalState) xfrState).getTxnId();
                }
            }
            final ServerTransaction transaction;
            boolean setRollbackOnly = true;
            if (transactionId != null) {
                try {
                    transaction = getSession().getTransaction(transactionId);
                } catch (UnknownTransactionException e) {
                    return new Error(TransactionError.UNKNOWN_ID, String.format("transaction-id '%s' is unknown.", transactionId));
                }
                if (!(transaction instanceof AutoCommitTransaction)) {
                    transaction.addPostTransactionAction(new ServerTransaction.Action() {

                        @Override
                        public void postCommit() {
                            updateDisposition(delivery.getDeliveryTag(), null, true);
                        }

                        @Override
                        public void onRollback() {
                            updateDisposition(delivery.getDeliveryTag(), null, true);
                        }
                    });
                }
            } else {
                transaction = new AsyncAutoCommitTransaction(getAddressSpace().getMessageStore(), this);
            }
            try {
                Session_1_0 session = getSession();
                session.getAMQPConnection().checkAuthorizedMessagePrincipal(serverMessage.getMessageHeader().getUserId());
                Outcome outcome;
                if (serverMessage.isPersistent() && !getAddressSpace().getMessageStore().isPersistent()) {
                    final Error preconditionFailedError = new Error(AmqpError.PRECONDITION_FAILED, "Non-durable message store cannot accept durable message.");
                    if (_rejectedOutcomeSupportedBySource) {
                        final Rejected rejected = new Rejected();
                        rejected.setError(preconditionFailedError);
                        outcome = rejected;
                    } else {
                        // TODO - disposition not updated for the non-transaction case
                        return preconditionFailedError;
                    }
                } else {
                    try {
                        getReceivingDestination().send(serverMessage, transaction, session.getSecurityToken());
                        outcome = ACCEPTED;
                    } catch (UnroutableMessageException e) {
                        final Error error = new Error();
                        error.setCondition(e.getErrorCondition());
                        error.setDescription(e.getMessage());
                        String targetAddress = getTarget().getAddress();
                        if (targetAddress == null || "".equals(targetAddress.trim())) {
                            error.setInfo(Collections.singletonMap(DELIVERY_TAG, delivery.getDeliveryTag()));
                        }
                        if (!_rejectedOutcomeSupportedBySource || (delivery.isSettled() && !(transaction instanceof LocalTransaction))) {
                            return error;
                        } else {
                            if (delivery.isSettled() && transaction instanceof LocalTransaction) {
                                ((LocalTransaction) transaction).setRollbackOnly();
                            }
                            Rejected rejected = new Rejected();
                            rejected.setError(error);
                            outcome = rejected;
                        }
                    }
                }
                Outcome sourceDefaultOutcome = getSource().getDefaultOutcome();
                boolean defaultOutcome = sourceDefaultOutcome != null && sourceDefaultOutcome.getSymbol().equals(outcome.getSymbol());
                DeliveryState resultantState;
                if (transactionId == null) {
                    resultantState = defaultOutcome ? null : outcome;
                } else {
                    TransactionalState transactionalState = new TransactionalState();
                    transactionalState.setOutcome(defaultOutcome ? null : outcome);
                    transactionalState.setTxnId(transactionId);
                    resultantState = transactionalState;
                }
                boolean settled = shouldReceiverSettleFirst(transferReceiverSettleMode);
                if (transaction instanceof AsyncAutoCommitTransaction) {
                    _pendingDispositions.add(new PendingDispositionHolder(delivery.getDeliveryTag(), resultantState, settled));
                } else {
                    getSession().receivedComplete();
                    updateDisposition(delivery.getDeliveryTag(), resultantState, settled);
                }
                getSession().getAMQPConnection().registerMessageReceived(serverMessage.getSize());
                if (transactionId != null) {
                    getSession().getAMQPConnection().registerTransactedMessageReceived();
                }
                setRollbackOnly = false;
            } catch (AccessControlException e) {
                final Error err = new Error();
                err.setCondition(AmqpError.NOT_ALLOWED);
                err.setDescription(e.getMessage());
                return err;
            } finally {
                if (setRollbackOnly && transaction instanceof LocalTransaction) {
                    ((LocalTransaction) transaction).setRollbackOnly();
                }
            }
        } finally {
            reference.release();
        }
    }
    return null;
}
Also used : AsyncAutoCommitTransaction(org.apache.qpid.server.txn.AsyncAutoCommitTransaction) AutoCommitTransaction(org.apache.qpid.server.txn.AutoCommitTransaction) AsyncAutoCommitTransaction(org.apache.qpid.server.txn.AsyncAutoCommitTransaction) LocalTransaction(org.apache.qpid.server.txn.LocalTransaction) Rejected(org.apache.qpid.server.protocol.v1_0.type.messaging.Rejected) ReceiverSettleMode(org.apache.qpid.server.protocol.v1_0.type.transport.ReceiverSettleMode) ServerTransaction(org.apache.qpid.server.txn.ServerTransaction) AmqpErrorRuntimeException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorRuntimeException) MessageFormat(org.apache.qpid.server.plugin.MessageFormat) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) AccessControlException(java.security.AccessControlException) TransactionalState(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionalState) DeliveryState(org.apache.qpid.server.protocol.v1_0.type.DeliveryState) Outcome(org.apache.qpid.server.protocol.v1_0.type.Outcome) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger)

Aggregations

LocalTransaction (org.apache.qpid.server.txn.LocalTransaction)12 ServerTransaction (org.apache.qpid.server.txn.ServerTransaction)10 LogMessage (org.apache.qpid.server.logging.LogMessage)2 UnsignedInteger (org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger)2 TransactionError (org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError)2 AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)2 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)2 AutoCommitTransaction (org.apache.qpid.server.txn.AutoCommitTransaction)2 AccessControlException (java.security.AccessControlException)1 PrivilegedAction (java.security.PrivilegedAction)1 Date (java.util.Date)1 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)1 MessageDestination (org.apache.qpid.server.message.MessageDestination)1 RoutingResult (org.apache.qpid.server.message.RoutingResult)1 ServerMessage (org.apache.qpid.server.message.ServerMessage)1 MessageFormat (org.apache.qpid.server.plugin.MessageFormat)1 AmqpErrorRuntimeException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorRuntimeException)1 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)1 DeliveryState (org.apache.qpid.server.protocol.v1_0.type.DeliveryState)1 Outcome (org.apache.qpid.server.protocol.v1_0.type.Outcome)1