All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aktivecortex.core.commandhandling.interceptors.afterreceive.TransactionInterceptor Maven / Gradle / Ivy

/*
 * Copyright (C) 2012-2013. Aktive Cortex
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aktivecortex.core.commandhandling.interceptors.afterreceive;

import org.aktivecortex.api.command.Command;
import org.aktivecortex.api.message.Message;
import org.aktivecortex.core.commandhandling.interceptors.AfterReceiveInterceptorChain;
import org.aktivecortex.core.commandhandling.interceptors.AfterReceiveMessageHandlerInterceptor;
import org.axonframework.unitofwork.CurrentUnitOfWork;
import org.axonframework.unitofwork.UnitOfWork;
import org.axonframework.unitofwork.UnitOfWorkListenerAdapter;

public abstract class TransactionInterceptor implements AfterReceiveMessageHandlerInterceptor {
	
	 @Override
	    public Object handle(Message message, UnitOfWork unitOfWork, AfterReceiveInterceptorChain interceptorChain)  {
	        T transaction = startTransaction();
	        CurrentUnitOfWork.get().registerListener(new TransactionalUnitOfWork(transaction));
	        return interceptorChain.proceed();
	    }

	    /**
	     * Start a new transaction for a command execution described by the given context. The given
	     * unitOfWork is the unitOfWork bound to the current thread.
	     *
	     * @return A reference to the current transaction
	     */
	    protected abstract T startTransaction();

	    /**
	     * Commits the transaction for the command execution described by the given context. The given
	     * unitOfWork is the unitOfWork bound to the current thread.
	     *
	     * @param transaction The transaction object returned during during {@link #startTransaction()}
	     */
	    protected abstract void commitTransaction(T transaction);

	    /**
	     * Rolls back a transaction for a command execution described by the given context. The given
	     * unitOfWork is the unitOfWork bound to the current thread.
	     *
	     * @param transaction The transaction object returned during during {@link #startTransaction()}
	     */
	    protected abstract void rollbackTransaction(T transaction);

	    private final class TransactionalUnitOfWork extends UnitOfWorkListenerAdapter {

	        private final T transaction;

	        /**
	         * Creates an instance of the listener, tied to the given transaction.
	         *
	         * @param transaction the transaction assigned to the Unit Of Work.
	         */
	        private TransactionalUnitOfWork(T transaction) {
	            this.transaction = transaction;
	        }

	        /**
	         * This method tries to roll back the transaction assigned to this Unit Of Work.
	         *
	         * @param failureCause The cause of the rollback
	         */
	        @Override
	        public void onRollback(Throwable failureCause) {
	            rollbackTransaction(transaction);
	        }

	        /**
	         * This method commits the transaction assigned to this Unit Of Work.
	         */
	        @Override
	        public void afterCommit() {
	            commitTransaction(transaction);
	        }
	    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy