KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > mybatchfwk > jmx > ManagedBatchService


1 /*
2  * MyBatchFramework - Open-source batch framework.
3  * Copyright (C) 2006 Jérôme Bertèche cyberteche@users.sourceforge.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Jérôme Bertèche
16  * Email: cyberteche@users.sourceforge.net
17  */

18 package net.sf.mybatchfwk.jmx;
19
20 import java.lang.management.ManagementFactory JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import javax.management.InstanceAlreadyExistsException JavaDoc;
24 import javax.management.MBeanRegistrationException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.NotCompliantMBeanException JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationBroadcasterSupport JavaDoc;
30 import javax.management.ObjectName JavaDoc;
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 /**
40  * Default implementation of the MBean interface
41  * @author Jérôme Bertèche (cyberteche@users.sourceforge.net)
42  */

43 public class ManagedBatchService extends NotificationBroadcasterSupport JavaDoc implements ManagedBatchServiceMBean {
44     
45     private static final Log log = LogFactory.getLog(ManagedBatchService.class);
46     
47     protected static final String JavaDoc[] STATES_LABELS = {
48         "initializing", "waiting for launch", "running", "sleeping", "waiting for shutdown", "shutdown"
49     };
50     
51     private BatchService batchService;
52     
53     /**
54      * Register the batch service to the JMX MBean server
55      *
56      * @param batchService
57      * @throws NullPointerException
58      * @throws MalformedObjectNameException
59      * @throws NotCompliantMBeanException
60      * @throws MBeanRegistrationException
61      * @throws InstanceAlreadyExistsException
62      */

63     public ManagedBatchService(BatchService batchService) throws MalformedObjectNameException JavaDoc, NullPointerException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc {
64         this.batchService = batchService;
65         
66         MBeanServer JavaDoc mbs = ManagementFactory.getPlatformMBeanServer();
67         ObjectName JavaDoc name = new ObjectName JavaDoc("net.sf.mybatchfwk:type=BatchService");
68         mbs.registerMBean(this, name);
69     }
70     
71     /* (non-Javadoc)
72      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#pause()
73      */

74     public void pause() throws Exception JavaDoc {
75         batchService.pause();
76     }
77     
78     /* (non-Javadoc)
79      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#resume()
80      */

81     public void resume() throws Exception JavaDoc {
82         batchService.resume();
83     }
84     
85     /* (non-Javadoc)
86      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#shutdown()
87      */

88     public void shutdown() throws Exception JavaDoc {
89         batchService.shutdown(true);
90     }
91     
92     /* (non-Javadoc)
93      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#shutdownNow()
94      */

95     public void shutdownNow() throws Exception JavaDoc {
96         batchService.shutdown(false);
97     }
98     
99     /* (non-Javadoc)
100      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#restart()
101      */

102     public void restart() throws Exception JavaDoc {
103         batchService.restart();
104     }
105     
106     /* (non-Javadoc)
107      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#getState()
108      */

109     public String JavaDoc getState() {
110         return STATES_LABELS[batchService.getState().ordinal()];
111     }
112     
113     /* (non-Javadoc)
114      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#getBeginDate()
115      */

116     public Date JavaDoc getBeginDate() {
117         return batchService.getExecutionReport().getBeginDate();
118     }
119     
120     /* (non-Javadoc)
121      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#getEndDate()
122      */

123     public Date JavaDoc getEndDate() {
124         return batchService.getExecutionReport().getEndDate();
125     }
126     
127     /* (non-Javadoc)
128      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#getNumberOfCompletedTasks()
129      */

130     public long getNumberOfCompletedTasks() {
131         return batchService.getExecutionReport().getNumberOfCompletedTasks();
132     }
133
134     /* (non-Javadoc)
135      * @see net.sf.mybatchfwk.jmx.ManagedBatchServiceMBean#getNumberOfFailedTasks()
136      */

137     public long getNumberOfFailedTasks() {
138         return batchService.getExecutionReport().getNumberOfFailedTasks();
139     }
140     
141     /**
142      * Send a JMX notification that contains the new state of the batch
143      * @param newState
144      */

145     public void notifyBatchState(STATE newState) {
146         Notification JavaDoc notification = new Notification JavaDoc(
147             "ManagedBatchService.batchState", this, -1, System.currentTimeMillis(), STATES_LABELS[newState.ordinal()]
148         );
149         sendNotification(notification);
150     }
151 }
152
Popular Tags