1 22 package org.jboss.cache.invalidation; 23 24 import javax.transaction.Transaction ; 25 26 import org.jboss.logging.Logger; 27 import org.jboss.tm.TransactionLocal; 28 29 import java.util.HashMap ; 30 import javax.transaction.Synchronization ; 31 32 import java.io.Serializable ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 60 public class InvalidationsTxGrouper 61 { 62 63 65 67 69 private static final TransactionLocal synchLocal = new TransactionLocal(); 70 static Logger log = Logger.getLogger(InvalidationsTxGrouper.class); 71 72 74 76 public static void registerInvalidationSynchronization(Transaction tx, InvalidationGroup group, Serializable key) 77 throws Exception 78 { 79 InvalidatorSynchronization synch = (InvalidatorSynchronization) synchLocal.get(tx); 80 if(synch == null) 81 { 82 synch = new InvalidatorSynchronization(tx); 83 synchLocal.set(tx, synch); 84 if( tx != null ) 86 tx.registerSynchronization(synch); 87 } 88 synch.addInvalidation(group, key); 89 if( tx == null ) 91 synch.afterCompletion(javax.transaction.Status.STATUS_NO_TRANSACTION); 92 } 93 } 94 95 class InvalidatorSynchronization 96 implements Synchronization 97 { 98 101 protected Transaction tx; 102 103 106 protected HashMap ids = new HashMap (); 107 108 111 InvalidatorSynchronization(Transaction tx) 112 { 113 this.tx = tx; 114 } 115 116 public void addInvalidation(InvalidationGroup group, Serializable key) 117 { 118 InvalidationManagerMBean im = group.getInvalidationManager(); 119 120 123 Map relatedInvalidationMgr; 124 synchronized(ids) 125 { 126 relatedInvalidationMgr = (HashMap ) ids.get(im); 127 if(relatedInvalidationMgr == null) 128 { 129 relatedInvalidationMgr = new HashMap (); 130 ids.put(im, relatedInvalidationMgr); 131 } 132 } 133 134 Set relatedInvalidations; 135 synchronized(relatedInvalidationMgr) 136 { 137 relatedInvalidations = (HashSet ) relatedInvalidationMgr.get(group); 138 if(relatedInvalidations == null) 139 { 140 relatedInvalidations = new HashSet (); 141 relatedInvalidationMgr.put(group, relatedInvalidations); 142 } 143 } 144 145 relatedInvalidations.add(key); 146 } 147 148 150 public void beforeCompletion() 151 { 152 } 153 154 155 public void afterCompletion(int status) 156 { 157 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 161 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 162 163 try 164 { 165 try 166 { 167 sendBatchInvalidations(); 168 } 169 catch(Exception ex) 170 { 171 InvalidationsTxGrouper.log.warn("Failed sending invalidations messages", ex); 172 } 173 } 174 finally 175 { 176 Thread.currentThread().setContextClassLoader(oldCl); 177 } 178 } 179 180 protected void sendBatchInvalidations() 181 { 182 boolean trace = InvalidationsTxGrouper.log.isTraceEnabled(); 183 if(trace) 184 { 185 InvalidationsTxGrouper.log.trace("Begin sendBatchInvalidations, tx=" + tx); 186 } 187 Iterator imIter = ids.keySet().iterator(); 190 while(imIter.hasNext()) 191 { 192 InvalidationManagerMBean im = (InvalidationManagerMBean) imIter.next(); 193 194 HashMap relatedInvalidationMgr = (HashMap ) ids.get(im); 197 198 BatchInvalidation[] bomb = new BatchInvalidation[relatedInvalidationMgr.size()]; 199 200 Iterator groupsIter = relatedInvalidationMgr.keySet().iterator(); 201 int i = 0; 202 while(groupsIter.hasNext()) 203 { 204 InvalidationGroup group = (InvalidationGroup) groupsIter.next(); 205 HashSet sourceIds = (HashSet ) relatedInvalidationMgr.get(group); 206 String groupName = group.getGroupName(); 207 if(trace) 208 { 209 InvalidationsTxGrouper.log.trace("Adding ids to bomb(" + groupName + "): " + sourceIds); 210 } 211 Serializable [] ids = new Serializable [sourceIds.size()]; 212 sourceIds.toArray(ids); 213 BatchInvalidation batch = new BatchInvalidation(ids, groupName); 214 215 bomb[i] = batch; 216 217 i++; 218 } 219 220 im.batchInvalidate(bomb); 223 } 224 if(trace) 225 { 226 InvalidationsTxGrouper.log.trace("End sendBatchInvalidations, tx=" + tx); 227 } 228 229 this.ids = null; 232 } 233 } 234 | Popular Tags |