1 23 24 32 package com.sun.enterprise.transaction.monitor; 33 34 import java.util.Map ; 35 import java.util.Hashtable ; 36 import java.util.ArrayList ; 37 import java.util.List ; 38 import java.util.HashMap ; 39 40 import javax.management.Attribute ; 41 import javax.management.AttributeList ; 42 import javax.management.AttributeNotFoundException ; 43 import javax.management.RuntimeOperationsException ; 44 import javax.management.DynamicMBean ; 45 import javax.management.InvalidAttributeValueException ; 46 import javax.management.MBeanException ; 47 import javax.management.MBeanInfo ; 48 import javax.management.MBeanOperationInfo ; 49 import javax.management.MBeanParameterInfo ; 50 import javax.management.ReflectionException ; 51 52 import javax.transaction.Transaction ; 53 import javax.transaction.SystemException ; 54 55 import com.sun.enterprise.admin.monitor.BaseMonitorMBean; 56 import com.sun.enterprise.admin.monitor.MonitoredObjectType; 57 import com.sun.enterprise.admin.monitor.types.Counter; 58 import com.sun.enterprise.admin.monitor.types.MonitoredAttributeType; 59 import com.sun.enterprise.admin.monitor.types.StringMonitoredAttributeType; 60 61 import com.sun.enterprise.J2EETransactionManager; 62 import com.sun.enterprise.Switch; 63 import com.sun.enterprise.transaction.TransactionAdminBean; 64 import com.sun.enterprise.resource.ResourceInstaller; 65 import java.util.logging.Logger ; 66 import java.util.logging.Level ; 67 import com.sun.logging.LogDomains; 68 69 import com.sun.enterprise.util.i18n.StringManager; 70 71 import com.sun.enterprise.server.ApplicationServer; 73 import com.sun.enterprise.server.ServerContext; 74 import com.sun.enterprise.admin.monitor.registry.*; 75 import com.sun.enterprise.config.ConfigContext; 76 import com.sun.enterprise.config.ConfigBean; 77 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 78 import com.sun.enterprise.config.serverbeans.Config; 79 import com.sun.enterprise.config.ConfigException; 80 import com.sun.enterprise.config.serverbeans.TransactionService; 81 import com.sun.enterprise.config.serverbeans.ElementProperty; 82 83 84 85 88 public class JTSMonitorMBean extends BaseMonitorMBean { 89 90 static final String NUM_TRANSACTIONS_COMPLETED = "total-tx-completed"; 91 static final String NUM_TRANSACTIONS_ROLLEDBACK = "total-tx-rolled-back"; 92 static final String NUM_TRANSACTIONS_INFLIGHT = "total-tx-inflight"; 93 static final String IS_FROZEN = "isFrozen"; 94 static final String INFLIGHT_TRANSACTIONS = "inflight-tx"; 95 static final String ROLLBACK = "rollbackList"; 96 static final String FREEZE = "freeze"; 97 static final int COLUMN_LENGTH = 25; 98 99 public static final String TRANSACTION_ID = "TransactionId"; 100 public static final String STATE = "TransactionState"; 101 public static final String ELAPSED_TIME = "ElapsedTime"; 102 public static final String COMPONENT_NAME = "ComponentName"; 103 public static final String RESOURCE_NAMES = "ResourceNames"; 104 105 106 private static StringManager sm = StringManager.getManager(JTSMonitorMBean.class); 108 109 112 static Logger _logger = LogDomains.getLogger(LogDomains.JTA_LOGGER); 113 114 115 118 private static Object [][] attrNameTypeArray = { 119 {NUM_TRANSACTIONS_COMPLETED, Counter.INTEGER}, 120 {NUM_TRANSACTIONS_ROLLEDBACK, Counter.INTEGER}, 121 {NUM_TRANSACTIONS_INFLIGHT, Counter.INTEGER}, 122 {IS_FROZEN, StringMonitoredAttributeType.DEFAULT}, 123 {INFLIGHT_TRANSACTIONS, StringMonitoredAttributeType.DEFAULT} 124 }; 125 126 private static MBeanOperationInfo [] operationInfoArray = 127 new MBeanOperationInfo [2]; 128 129 132 private J2EETransactionManager txnMgr; 133 private Hashtable txnTable = null; 134 private MonitoredObjectType type = MonitoredObjectType.TXNMGR; 135 private boolean monitorOn = false; 136 private long startTime = 0; 137 138 141 private static Map attrNameTypeMap; 142 143 146 private static MBeanInfo mBeanInfo; 147 148 static { 149 attrNameTypeMap = createAttrNameTypeMap(attrNameTypeArray); 150 operationInfoArray[0] = new MBeanOperationInfo (ROLLBACK, 151 "rollback(String txnId): Marks the transaction for rollback", 152 null, "void", MBeanOperationInfo.ACTION); 153 operationInfoArray[1] = new MBeanOperationInfo (FREEZE, 154 "freeze(): Freezes the transactions", 155 null, "void", MBeanOperationInfo.ACTION); 156 mBeanInfo = createMBeanInfo(attrNameTypeMap, operationInfoArray); 157 } 158 159 162 public JTSMonitorMBean() { 163 txnMgr = Switch.getSwitch().getTransactionManager(); 164 ServerContext sCtx = ApplicationServer.getServerContext(); 165 if (sCtx != null) { 166 try { 167 ConfigContext ctx = sCtx.getConfigContext(); 168 Config cfg = ServerBeansFactory.getConfigBean(ctx); 169 String lvl = cfg.getMonitoringService().getModuleMonitoringLevels().getTransactionService(); 170 MonitoringLevel l = MonitoringLevel.instance(lvl); 171 if (l != MonitoringLevel.OFF) { 172 startMonitoring(); 173 } 174 MonitoringRegistry registry = sCtx.getMonitoringRegistry(); 175 JTAStatsImpl.createInstance(this); 176 JTAStatsImpl statImpl = JTAStatsImpl.getInstance(); 177 registry.registerJTAStats(statImpl, statImpl); 178 _logger.log(Level.FINE,"JTAStats monitoring registration completed"); 179 TransactionService txnService = ServerBeansFactory.getTransactionServiceBean(ctx); 180 ElementProperty[] eprops = txnService.getElementProperty(); 181 for (int index = 0; index < eprops.length; index++) { 182 if ("pending-txn-cleanup-interval".equals(eprops[index].getName())) { 183 int interval = 60; 184 if (eprops[index].getValue() != null) 185 interval = Integer.parseInt(eprops[index].getValue()); 186 new RecoveryHelperThread(interval).start(); 187 if (_logger.isLoggable(Level.FINE)) 188 _logger.log(Level.FINE,"Asynchronous thread for incomplete tx is enabled with interval " + interval); 189 } 190 } 191 192 } catch (MonitoringRegistrationException mex) { 193 _logger.log(Level.WARNING,"transaction.monitor.registration_failed", mex); 194 } catch (ConfigException e) { 195 _logger.log(Level.WARNING,"transaction.monitor.registration_failed", e); 196 } 197 } 198 else { 199 _logger.log(Level.FINE,"JTSMonitorMBean: ServerContext is null: monitoring is not enabled"); 200 } 201 } 202 203 public List <Map <String , String >> listActiveTransactions() { 204 ArrayList aList = txnMgr.getActiveTransactions(); 205 if (aList.isEmpty()) 206 return new ArrayList <Map <String , String >>(0); 207 txnTable = new Hashtable (); 208 List <Map <String , String >> activeTxnList = new ArrayList <Map <String , String >>(); 209 Map <String , String > txnListEntry = null; 210 for (int i=0; i < aList.size(); i++) { 211 TransactionAdminBean txnBean = (TransactionAdminBean)aList.get(i); 212 Transaction j2eeTxn = (Transaction ) txnBean.getIdentifier(); 213 String txnId = txnBean.getId(); 214 txnTable.put(txnId, j2eeTxn); 215 txnListEntry = new HashMap <String , String >(5); 216 txnListEntry.put(TRANSACTION_ID, txnId); 217 txnListEntry.put(ELAPSED_TIME, String.valueOf(txnBean.getElapsedTime())); 218 txnListEntry.put(COMPONENT_NAME, txnBean.getComponentName()); 219 ArrayList <String > resourceList = txnBean.getResourceNames(); 220 StringBuffer strBuf = new StringBuffer (" "); 221 if (resourceList != null) { 222 for (int k = 0; k < resourceList.size(); k++) { 223 strBuf.append(resourceList.get(k)); 224 strBuf.append(","); 225 } 226 } 227 txnListEntry.put(RESOURCE_NAMES, strBuf.toString()); 228 txnListEntry.put(STATE, txnBean.getStatus()); 229 activeTxnList.add(txnListEntry); 230 } 231 return activeTxnList; 232 } 233 234 240 public Object getAttribute(String attribute) throws AttributeNotFoundException { 241 if (attribute == null) { 242 246 throw new RuntimeOperationsException ( 247 new IllegalArgumentException (sm.getString("transaction.monitor.attribute_is_null"))); 248 } 249 if (attribute.equals(NUM_TRANSACTIONS_COMPLETED)) { 251 return new Integer (txnMgr.getNumberOfTransactionsCommitted()); 252 } 253 if (attribute.equals(NUM_TRANSACTIONS_ROLLEDBACK)) { 254 return new Integer (txnMgr.getNumberOfTransactionsRolledBack()); 255 } 256 if (attribute.equals(NUM_TRANSACTIONS_INFLIGHT)) { 257 return new Integer (txnMgr.getNumberOfActiveTransactions()); 258 } 259 if (attribute.equals(IS_FROZEN)) { 260 if (txnMgr.isFrozen()) 261 return "True"; 262 else 263 return "False"; 264 } 265 if (attribute.equals(INFLIGHT_TRANSACTIONS)) { 266 ArrayList aList = txnMgr.getActiveTransactions(); 267 if (aList.isEmpty()) return ""; 269 StringBuffer strBuf = new StringBuffer (1024); 270 txnTable = new Hashtable (); 271 272 if (aList.size() > 0) { 274 String colName = "Transaction Id"; 275 strBuf.append("\n\n"); 276 strBuf.append(colName); 277 for (int i=colName.length(); i<COLUMN_LENGTH+15; i++){ 278 strBuf.append(" "); 279 } 280 colName = "Status"; 281 strBuf.append(colName); 282 for (int i=colName.length(); i<COLUMN_LENGTH; i++){ 283 strBuf.append(" "); 284 } 285 colName = "ElapsedTime(ms)"; 286 strBuf.append(colName); 287 for (int i=colName.length(); i<COLUMN_LENGTH; i++){ 288 strBuf.append(" "); 289 } 290 colName = "ComponentName"; 291 strBuf.append(colName); 292 for (int i=colName.length(); i<COLUMN_LENGTH; i++){ 293 strBuf.append(" "); 294 } 295 strBuf.append("ResourceNames\n"); 296 } 297 298 for (int i=0; i < aList.size(); i++) { 299 TransactionAdminBean txnBean = (TransactionAdminBean)aList.get(i); 300 Transaction j2eeTxn = (Transaction ) txnBean.getIdentifier(); 301 String txnId = txnBean.getId(); 302 txnTable.put(txnId, j2eeTxn); 303 304 strBuf.append("\n"); 305 strBuf.append(txnId); 306 for (int j=txnId.length(); j<COLUMN_LENGTH+15; j++){ 307 strBuf.append(" "); 308 } 309 strBuf.append(txnBean.getStatus()); 310 for (int j=txnBean.getStatus().length(); j<COLUMN_LENGTH; j++){ 311 strBuf.append(" "); 312 } 313 strBuf.append(String.valueOf(txnBean.getElapsedTime())); 314 for (int j=(String.valueOf(txnBean.getElapsedTime()).length()); j<COLUMN_LENGTH; j++){ 315 strBuf.append(" "); 316 } 317 318 strBuf.append(txnBean.getComponentName()); 319 for (int j=txnBean.getComponentName().length(); j<COLUMN_LENGTH; j++){ 320 strBuf.append(" "); 321 } 322 ArrayList <String > resourceList = txnBean.getResourceNames(); 323 if (resourceList != null) { 324 for (int k = 0; k < resourceList.size(); k++) { 325 strBuf.append(resourceList.get(k)); 326 strBuf.append(","); 327 } 328 } 329 } 330 331 return strBuf.toString(); 332 } 333 334 throw(new AttributeNotFoundException (sm.getString("transaction.monitor.attribute_not_found",attribute ) )); 337 } 338 339 344 public AttributeList getAttributes(String [] attributeNames) { 345 if (attributeNames == null) { 347 352 throw new RuntimeOperationsException ( 353 new IllegalArgumentException ( 354 sm.getString("transaction.monitor.attributes_not_null"))); 355 } 356 AttributeList resultList = new AttributeList (); 357 if (attributeNames.length == 0) 359 return resultList; 360 for (int i=0 ; i<attributeNames.length ; i++){ 362 try { 363 Object value = getAttribute((String ) attributeNames[i]); 364 resultList.add(new Attribute (attributeNames[i],value)); 365 } catch (Exception e) { 366 _logger.log(Level.WARNING,"transaction.monitor.error_while_getting_monitor_attr",e); 368 } 369 } 370 return(resultList); 371 } 372 373 public Object invoke(String operationName, Object [] params, String [] signature) throws MBeanException , ReflectionException { 374 if (operationName == null || operationName.equals("")) { 375 379 throw new RuntimeOperationsException ( 380 new IllegalArgumentException (sm.getString("transaction.monitor.operation_name_is_null"))); 381 } 382 383 if (params == null) 384 return null; 385 386 AttributeList resultList = new AttributeList (); 387 if (operationName.equals(ROLLBACK)) { 388 for (int i=0; i<params.length; i++) { 389 String txnId = (String ) params[i]; 390 Object value = setRollback(txnId); 391 resultList.add(new Attribute (txnId, value)); 392 } 393 } else if (operationName.equals(FREEZE)) { 394 if (params[0].equals("true")) { 395 txnMgr.freeze(); 396 resultList.add(new Attribute ("freeze", "Successful")); 397 } else { 398 txnMgr.unfreeze(); 399 resultList.add(new Attribute ("unfreeze", "Successful")); 400 } 401 } else { 402 throw new UnsupportedOperationException (UNSUPPORTED_ERRMSG); 403 } 404 405 return resultList; 406 } 407 408 public void freeze() { 409 txnMgr.freeze(); 410 } 411 public void unfreeze() { 412 txnMgr.unfreeze(); 413 } 414 415 public String [] rollback(String [] txnIds) { 416 if (txnIds == null || txnIds.length == 0) 417 return new String [0]; 418 String result[] = new String [txnIds.length]; 419 for (int i = 0; i < txnIds.length; i++) { 420 result[i] = (String ) setRollback(txnIds[i]); 421 } 422 return result; 423 } 424 425 public Object setRollback(String txnId) throws IllegalStateException { 426 String result = ""; 428 if (txnTable == null || txnTable.get(txnId) == null) { 429 result = sm.getString("transaction.monitor.rollback_invalid_id"); 430 throw new IllegalStateException (result); 431 } 432 else { 433 try { 435 txnMgr.forceRollback((Transaction )txnTable.get(txnId)); 436 result = sm.getString("transaction.monitor.rollback_sucessful"); 437 } catch (IllegalStateException e) { 438 result = sm.getString("transaction.monitor.rollback_unsuccessful_not_associated"); 440 IllegalStateException ex = new IllegalStateException (result); 441 ex.initCause(e); 442 throw ex; 443 444 } catch (SecurityException e) { 445 result = sm.getString("transaction.monitor.rollback_unsuccessful_security_exception"); 447 SecurityException ex = new SecurityException (result); 448 ex.initCause(e); 449 throw ex; 450 } catch (SystemException e) { 451 result = sm.getString("transaction.monitor.rollback_unsuccessful_unexpected_exception"); 453 IllegalStateException ex = new IllegalStateException (result); 454 ex.initCause(e); 455 throw ex; 456 } 457 } 458 459 return result; 460 } 461 462 463 469 public void startMonitoring() { 470 txnMgr.setMonitoringEnabled(true); 471 monitorOn = true; 472 startTime = System.currentTimeMillis(); 473 } 474 475 479 public void stopMonitoring() { 480 txnMgr.setMonitoringEnabled(false); 481 monitorOn = false; 482 startTime = 0; 483 } 484 485 486 public long getStartTime() { 487 return startTime; 488 } 489 490 496 public MBeanInfo getMBeanInfo() { 497 return mBeanInfo; 498 } 499 500 508 public Map getMonitoringMetaData() { 509 return attrNameTypeMap; 510 } 511 512 515 public MonitoredAttributeType getAttributeType(String attrName) { 516 MonitoredAttributeType type = null; 517 if (attrNameTypeMap != null && attrName != null) { 518 type = (MonitoredAttributeType)attrNameTypeMap.get(attrName); 519 } 520 return type; 521 } 522 523 public MonitoredObjectType getMonitoredObjectType() { 524 return type; 525 } 526 527 public static void recover(boolean delegated, String txLogDir) throws Exception { 528 ResourceInstaller resInstaller = Switch.getSwitch().getResourceInstaller(); 529 boolean result = true; 530 if (resInstaller == null) { 531 throw new IllegalStateException (); 532 } 533 if (!delegated) { result = resInstaller.recoverIncompleteTx(false, null); 535 } 536 else { result = resInstaller.recoverIncompleteTx(true, txLogDir); 538 } 539 if (!result) 540 throw new IllegalStateException (); 541 } 542 543 class RecoveryHelperThread extends Thread { 544 private int interval; 545 RecoveryHelperThread(int interval) { 546 setName("Recovery Helper Thread"); 547 setDaemon(true); 548 this.interval = interval; 549 } 550 public void run() { 551 try { 552 while(true) { 553 Thread.sleep(interval*1000); 554 Switch.getSwitch().getResourceInstaller().recoverIncompleteTx(false, null); 555 } 556 } catch (Exception ex) { 557 if (JTSMonitorMBean._logger.isLoggable(Level.FINE)) 558 JTSMonitorMBean._logger.log(Level.FINE, " Exception occurred in recoverInCompleteTx "); 559 } 560 } 561 } 562 } 563 564 | Popular Tags |