    001package com.hammurapi.common.concurrent.transactions;
    002
    003import java.util.concurrent.ExecutorService;
    004import java.util.concurrent.ThreadFactory;
    005
    006import javax.transaction.NotSupportedException;
    007import javax.transaction.SystemException;
    008import javax.transaction.Transaction;
    009import javax.transaction.xa.XAResource;
    010
    011import bitronix.tm.BitronixTransactionManager;
    012import bitronix.tm.TransactionManagerServices;
    013import bitronix.tm.resource.ehcache.EhCacheXAResourceProducer;
    014
    015import com.hammurapi.common.Context;
    016import com.hammurapi.common.concurrent.LocalStringPropertySet;
    017import com.hammurapi.common.concurrent.NonQueueingThreadPoolExecutorService;
    018import com.hammurapi.common.concurrent.PropertySet;
    019import com.hammurapi.convert.ConvertingService;
    020
    021/**
    022 * Transactional processor which uses NonQueueingThreadPoolExecutor, String PropertySet, and Bitronix transaction manager.
    023 * @author Pavel Vlasov
    024 *
    025 */
    026public class StandaloneTransactionalProcessor extends AbstractTransactionalProcessor<String> {
    027
    028        private ExecutorService nqtpes;
    029
    030        private BitronixTransactionManager tm;
    031
    032        private static ThreadLocal<ExecutorService> tmp = new ThreadLocal<ExecutorService>();
    033
    034        private static ExecutorService setExecutorService(ExecutorService es) {
    035                tmp.set(es);
    036                return es;
    037        }
    038
    039        public StandaloneTransactionalProcessor(ThreadFactory threadFactory, int threadPoolSize) {
    040                super(setExecutorService(new NonQueueingThreadPoolExecutorService(threadFactory, threadPoolSize)));
    041                nqtpes = tmp.get();
    042                tmp.set(null);
    043                tm = TransactionManagerServices.getTransactionManager();
    044        }
    045
    046        @Override
    047        protected PropertySet<String> createPropertySet(ExecutorService executorService, Context context, PropertySet<String>... chain) {
    048                return new LocalStringPropertySet(executorService, ConvertingService.CONVERTER, context, Thread.currentThread().getContextClassLoader(), chain);
    049        }
    050
    051        @Override
    052        protected Transaction beginTransaction() throws NotSupportedException,  SystemException {
    053                tm.begin();
    054                return tm.getTransaction();
    055        }
    056
    057        public void shutdown() {
    058                if (nqtpes!=null) {
    059                        nqtpes.shutdown();
    060                }
    061                if (tm!=null) {
    062                        tm.shutdown();
    063                }
    064        }
    065
    066        public void setTransactionTimeout(int seconds) throws SystemException {
    067                tm.setTransactionTimeout(seconds);
    068        }
    069
    070        @Override
    071        protected void registerXAResource(String uniqueId, XAResource xaResource) {
    072                EhCacheXAResourceProducer.registerXAResource(uniqueId, xaResource);
    073        }
    074
    075        @Override
    076        protected void unregisterXAResource(String uniqueId, XAResource xaResource) {
    077                EhCacheXAResourceProducer.unregisterXAResource(uniqueId, xaResource);
    078        }
    079
    080}