package org.infinispan.replication; import java.util.Properties; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import javax.transaction.Status; import javax.transaction.TransactionManager; import org.infinispan.Cache; import org.infinispan.config.Configuration; import org.infinispan.config.GlobalConfiguration; import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.remoting.transport.jgroups.JGroupsTransport; import org.infinispan.test.fwk.JGroupsConfigBuilder; import org.infinispan.test.fwk.TestCacheManagerFactory; import org.infinispan.transaction.lookup.JBossStandaloneJTAManagerLookup; import org.infinispan.transaction.lookup.TransactionManagerLookup; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author yeli */ public class CacheScheduledCounter { Cache cache = null; String key = "locked-counter"; // Collection keys = Collections.singletonList(key); public static void main(String[] args) { CacheScheduledCounter c = new CacheScheduledCounter(); c.start(); } public CacheScheduledCounter() { Configuration c = TestCacheManagerFactory.getDefaultConfiguration(false,Configuration.CacheMode.REPL_SYNC); c.setFetchInMemoryState(true); TransactionManagerLookup txLookup = new JBossStandaloneJTAManagerLookup(); c.setTransactionManagerLookup(txLookup); System.out.println("Create cache manager ..."); GlobalConfiguration gc = GlobalConfiguration.getClusteredDefault(); amendTransport(gc); EmbeddedCacheManager manager = new DefaultCacheManager(gc,c); manager.start(); System.out.println("get cache"); cache = manager.getCache("test-grid"); System.out.println("memebers " + manager.getMembers()); System.out.println("cache size " + cache.size()); } public void start() { ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); timer.scheduleAtFixedRate(new CounterTask(), 0, 5, TimeUnit.SECONDS); } class CounterTask implements Runnable { @Override public void run() { TransactionManager tx = cache.getAdvancedCache().getTransactionManager(); try { tx.begin(); } catch (Exception ex) { ex.printStackTrace(); } try { System.out.println("aquiring lock on cache " + cache.getName() + " key " + key + "..."); cache.getAdvancedCache().lock(key); Integer val = (Integer) cache.get(key); System.out.println("current value : " + val); if (val == null) { val = 0; } else { val++; } cache.put(key, val); try { Thread.sleep(2000); } catch (Exception ex) {} System.out.println("commit..."); tx.commit(); System.out.println("done commit"); } catch (Exception ex) { try { System.out.println("rollback... " + ex.getLocalizedMessage()); tx.rollback(); System.out.println("done rollback"); } catch (Exception rex) { rex.printStackTrace(); } } finally { try { System.out.print("tx status at the end : "); switch (tx.getStatus()) { case Status.STATUS_ACTIVE: System.out.println("active"); break; case Status.STATUS_COMMITTED: System.out.println("committed"); break; case Status.STATUS_COMMITTING: System.out.println("committing"); break; case Status.STATUS_MARKED_ROLLBACK: System.out.println("makerd rollback"); break; case Status.STATUS_NO_TRANSACTION: System.out.println("no transaction"); break; case Status.STATUS_PREPARED: System.out.println("preprared"); break; case Status.STATUS_PREPARING: System.out.println("preparing"); break; case Status.STATUS_ROLLEDBACK: System.out.println("rolledback"); break; case Status.STATUS_ROLLING_BACK: System.out.println("rolling back"); break; case Status.STATUS_UNKNOWN: System.out.println("unknown"); break; default: System.out.println(tx.getStatus()); } } catch (Exception ex) { ex.printStackTrace(); } } } } private static void amendTransport(GlobalConfiguration configuration) { if (configuration.getTransportClass() != null) { //this is local Properties newTransportProps = new Properties(); Properties previousSettings = configuration.getTransportProperties(); if (previousSettings != null) { newTransportProps.putAll(previousSettings); } newTransportProps.put(JGroupsTransport.CONFIGURATION_STRING, JGroupsConfigBuilder.getJGroupsConfig()); configuration.setTransportProperties(newTransportProps); } } }