1 18 package net.sf.mybatchfwk.jmx; 19 20 import java.lang.management.ManagementFactory ; 21 import java.util.Date ; 22 23 import javax.management.InstanceAlreadyExistsException ; 24 import javax.management.MBeanRegistrationException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.NotCompliantMBeanException ; 28 import javax.management.Notification ; 29 import javax.management.NotificationBroadcasterSupport ; 30 import javax.management.ObjectName ; 31 32 import net.sf.mybatchfwk.BatchService; 33 import net.sf.mybatchfwk.BatchService.STATE; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 39 43 public class ManagedBatchService extends NotificationBroadcasterSupport implements ManagedBatchServiceMBean { 44 45 private static final Log log = LogFactory.getLog(ManagedBatchService.class); 46 47 protected static final String [] STATES_LABELS = { 48 "initializing", "waiting for launch", "running", "sleeping", "waiting for shutdown", "shutdown" 49 }; 50 51 private BatchService batchService; 52 53 63 public ManagedBatchService(BatchService batchService) throws MalformedObjectNameException , NullPointerException , InstanceAlreadyExistsException , MBeanRegistrationException , NotCompliantMBeanException { 64 this.batchService = batchService; 65 66 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 67 ObjectName name = new ObjectName ("net.sf.mybatchfwk:type=BatchService"); 68 mbs.registerMBean(this, name); 69 } 70 71 74 public void pause() throws Exception { 75 batchService.pause(); 76 } 77 78 81 public void resume() throws Exception { 82 batchService.resume(); 83 } 84 85 88 public void shutdown() throws Exception { 89 batchService.shutdown(true); 90 } 91 92 95 public void shutdownNow() throws Exception { 96 batchService.shutdown(false); 97 } 98 99 102 public void restart() throws Exception { 103 batchService.restart(); 104 } 105 106 109 public String getState() { 110 return STATES_LABELS[batchService.getState().ordinal()]; 111 } 112 113 116 public Date getBeginDate() { 117 return batchService.getExecutionReport().getBeginDate(); 118 } 119 120 123 public Date getEndDate() { 124 return batchService.getExecutionReport().getEndDate(); 125 } 126 127 130 public long getNumberOfCompletedTasks() { 131 return batchService.getExecutionReport().getNumberOfCompletedTasks(); 132 } 133 134 137 public long getNumberOfFailedTasks() { 138 return batchService.getExecutionReport().getNumberOfFailedTasks(); 139 } 140 141 145 public void notifyBatchState(STATE newState) { 146 Notification notification = new Notification ( 147 "ManagedBatchService.batchState", this, -1, System.currentTimeMillis(), STATES_LABELS[newState.ordinal()] 148 ); 149 sendNotification(notification); 150 } 151 } 152 | Popular Tags |