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

com.aol.micro.server.transactions.TransactionFlow Maven / Gradle / Ivy

There is a newer version: 0.91.11
Show newest version
package com.aol.micro.server.transactions;

import java.util.function.Consumer;
import java.util.function.Function;

import lombok.AllArgsConstructor;
import lombok.experimental.Wither;

import org.springframework.transaction.support.TransactionTemplate;

import com.aol.cyclops.control.Try;

/**
 * Stream-like monadic transaction processing
 * 
 * @author johnmcclean
 *
 * @param  Data input type for transactional process
 * @param  Data result type for transactional process
 */
@AllArgsConstructor
@Wither
public class TransactionFlow {
	private final TransactionTemplate transactionTemplate;
	private final Function transaction;
	
	
	/**
	 * Map from input -> output within the context of a transaction.
	 * An exception in the mapping process will cause Rollback
	 * Transaction will be completed once the entire flow has completed
	 * 
	 * {@code
	 * 	Integer result = TransactionFlow.of(transactionTemplate)
										.map(this::load)
										.map(this::save)
										.execute(10)
										.get();
	 * }
	 * 
* * @param fn Map from input type to output type within a transaction * @return TransactionFlow */ public TransactionFlow map(Function fn){ return of(transactionTemplate,transaction.andThen(fn)); } /** * Consume current data within the context of a transaction. * An exception in the consumption process will cause Rollback * Transaction will be completed once the entire flow has completed *
	 * {code
	 * 	Integer result = TransactionFlow.of(transactionTemplate, this::load)
										.consume(this::save)
										.execute(10)
										.get();
	 * }
	 * 
* * @param c Consumer to accept current data * @return TransactionFlow */ public TransactionFlow peek(Consumer c){ return map (in-> { c.accept(in); return in;}); } /** * Transform data in the context of a Transaction and optionally propgate to / join with a new TransactionalFlow * *
	 * {code
	 * 	String result = TransactionFlow.of(transactionTemplate, this::load)
										.flatMap(this::newTransaction)
										.execute(10)
										.get();
	 * }
	 * 
* * * @param fn flatMap function to be applied * @return Next stage in the transactional flow */ public TransactionFlow flatMap(Function> fn){ return of(transactionTemplate,a -> fn.apply(transaction.apply(a)).transaction.apply(a)); } /** * Execute the transactional flow - catch all exceptions * * @param input Initial data input * @return Try that represents either success (with result) or failure (with errors) */ public Try execute(T input){ return Try.withCatch( ()->transactionTemplate.execute(status-> transaction.apply(input))); } /** * Execute the transactional flow - catch only specified exceptions * * @param input Initial data input * @param classes Exception types to catch * @return Try that represents either success (with result) or failure (with errors) */ public Try execute(T input,Class classes){ return Try.withCatch( ()->transactionTemplate.execute(status-> transaction.apply(input)),classes); } /** * Create a new Transactional Flow * * @param transactionManager Spring TransactionTemplate to manage the transactions * @param fn Initial function to be applied * @return Create a new Transactional Flow */ public static TransactionFlow of(TransactionTemplate transactionManager,Function fn){ return new TransactionFlow<>(transactionManager,fn); } /** * Create a new Transactional Flow * * @param transactionManager Spring TransactionTemplate to manage the transactions * @return Create a new Transactional Flow */ public static TransactionFlow of(TransactionTemplate transactionManager){ return new TransactionFlow<>(transactionManager,Function.identity()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy