Search in sources :

Example 1 with JmsResourceHolder

use of org.springframework.jms.connection.JmsResourceHolder in project iaf by ibissource.

the class JtaUtil method displayTransactionStatus.

// /**
// * returns a meaningful string describing the transaction status.
// */
// public static String displayTransactionStatus(int status) {
// switch (status) {
// case 	Status.STATUS_ACTIVE 			 : return status+"=STATUS_ACTIVE:"+ 	    " A transaction is associated with the target object and it is in the active state.";
// case 	Status.STATUS_COMMITTED 		 : return status+"=STATUS_COMMITTED:"+ 	    " A transaction is associated with the target object and it has been committed.";
// case 	Status.STATUS_COMMITTING 		 : return status+"=STATUS_COMMITTING:"+ 	" A transaction is associated with the target object and it is in the process of committing.";
// case 	Status.STATUS_MARKED_ROLLBACK 	 : return status+"=STATUS_MARKED_ROLLBACK:"+" A transaction is associated with the target object and it has been marked for rollback, perhaps as a result of a setRollbackOnly operation.";
// case 	Status.STATUS_NO_TRANSACTION 	 : return status+"=STATUS_NO_TRANSACTION:"+ " No transaction is currently associated with the target object.";
// case 	Status.STATUS_PREPARED 			 : return status+"=STATUS_PREPARED:"+ 	    " A transaction is associated with the target object and it has been prepared.";
// case 	Status.STATUS_PREPARING 		 : return status+"=STATUS_PREPARING:"+ 	    " A transaction is associated with the target object and it is in the process of preparing.";
// case 	Status.STATUS_ROLLEDBACK 		 : return status+"=STATUS_ROLLEDBACK:"+ 	" A transaction is associated with the target object and the outcome has been determined to be rollback.";
// case 	Status.STATUS_ROLLING_BACK 		 : return status+"=STATUS_ROLLING_BACK:"+ 	" A transaction is associated with the target object and it is in the process of rolling back.";
// case 	Status.STATUS_UNKNOWN 	 		 : return status+"=STATUS_UNKNOWN:"+ 	    " A transaction is associated with the target object but its current status cannot be determined.";
// default : return "unknown transaction status";
// }
// }
public static String displayTransactionStatus(TransactionStatus txStatus) {
    String result;
    result = "txName [" + TransactionSynchronizationManager.getCurrentTransactionName() + "]";
    if (txStatus != null) {
        result += " status new [" + txStatus.isNewTransaction() + "]";
        result += " status completeted [" + txStatus.isCompleted() + "]";
        result += " status rollbackOnly [" + txStatus.isRollbackOnly() + "]";
        result += " status hasSavepoint [" + txStatus.hasSavepoint() + "]";
    } else {
        result += " currently not in a transaction";
    }
    result += " isolation [" + TransactionSynchronizationManager.getCurrentTransactionIsolationLevel() + "]";
    result += " active [" + TransactionSynchronizationManager.isActualTransactionActive() + "]";
    boolean syncActive = TransactionSynchronizationManager.isSynchronizationActive();
    result += " synchronization active [" + syncActive + "]";
    result += "\n";
    Map<Object, Object> resources = TransactionSynchronizationManager.getResourceMap();
    result += "resources:\n";
    if (resources == null) {
        result += "  map is null\n";
    } else {
        for (Iterator<Object> it = resources.keySet().iterator(); it.hasNext(); ) {
            Object key = it.next();
            Object resource = resources.get(key);
            result += ClassUtils.nameOf(key) + "(" + key + "): " + ClassUtils.nameOf(resource) + "(" + resource + ")\n";
            if (resource instanceof JmsResourceHolder) {
                JmsResourceHolder jrh = (JmsResourceHolder) resource;
                result += "  connection: " + jrh.getConnection() + ", session: " + jrh.getSession() + "\n";
            }
        }
    }
    if (syncActive) {
        List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
        result += "synchronizations:\n";
        for (int i = 0; i < synchronizations.size(); i++) {
            TransactionSynchronization synchronization = synchronizations.get(i);
            result += ClassUtils.nameOf(synchronization) + "(" + synchronization + ")\n";
        }
    }
    return result;
}
Also used : JmsResourceHolder(org.springframework.jms.connection.JmsResourceHolder) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization)

Example 2 with JmsResourceHolder

use of org.springframework.jms.connection.JmsResourceHolder in project spring-framework by spring-projects.

the class JmsTemplate method doReceive.

/**
 * Actually receive a JMS message.
 * @param session the JMS Session to operate on
 * @param consumer the JMS MessageConsumer to receive with
 * @return the JMS Message received, or {@code null} if none
 * @throws JMSException if thrown by JMS API methods
 */
@Nullable
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
    try {
        // Use transaction timeout (if available).
        long timeout = getReceiveTimeout();
        ConnectionFactory connectionFactory = getConnectionFactory();
        JmsResourceHolder resourceHolder = null;
        if (connectionFactory != null) {
            resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager.getResource(connectionFactory);
        }
        if (resourceHolder != null && resourceHolder.hasTimeout()) {
            timeout = Math.min(timeout, resourceHolder.getTimeToLiveInMillis());
        }
        Message message = receiveFromConsumer(consumer, timeout);
        if (session.getTransacted()) {
            // Commit necessary - but avoid commit call within a JTA transaction.
            if (isSessionLocallyTransacted(session)) {
                // Transacted session created by this template -> commit.
                JmsUtils.commitIfNecessary(session);
            }
        } else if (isClientAcknowledge(session)) {
            // Manually acknowledge message, if any.
            if (message != null) {
                message.acknowledge();
            }
        }
        return message;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) JmsResourceHolder(org.springframework.jms.connection.JmsResourceHolder) Message(jakarta.jms.Message) Nullable(org.springframework.lang.Nullable)

Aggregations

JmsResourceHolder (org.springframework.jms.connection.JmsResourceHolder)2 ConnectionFactory (jakarta.jms.ConnectionFactory)1 Message (jakarta.jms.Message)1 Nullable (org.springframework.lang.Nullable)1 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)1