KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > execution > JmxEtlManager


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.execution;
17
18 import scriptella.core.SystemException;
19 import scriptella.core.ThreadSafe;
20
21 import javax.management.MBeanServer JavaDoc;
22 import javax.management.MalformedObjectNameException JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import java.lang.management.ManagementFactory JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 /**
31  * Implementation of {@link scriptella.execution.JmxEtlManagerMBean}.
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 @ThreadSafe
37 public class JmxEtlManager implements JmxEtlManagerMBean {
38     private static Logger JavaDoc LOG = Logger.getLogger(JmxEtlManager.class.getName());
39     private static final String JavaDoc MBEAN_NAME_PREFIX = "scriptella:type=etl";
40     private static MBeanServer JavaDoc mbeanServer;
41     private MBeanServer JavaDoc server;
42     private EtlContext ctx;
43     private Thread JavaDoc etlThread;
44     private ObjectName JavaDoc name;
45     private Date JavaDoc started;
46
47     public JmxEtlManager(EtlContext ctx) {
48         this.ctx = ctx;
49     }
50
51     public synchronized long getExecutedStatementsCount() {
52         return ctx.getSession().getExecutedStatementsCount();
53     }
54
55     public synchronized Date JavaDoc getStartDate() {
56         return started;
57     }
58
59     public synchronized double getThroughput() {
60         long cnt = getExecutedStatementsCount();
61         if (cnt > 0 && started != null) {
62             double ti = System.currentTimeMillis() - started.getTime();
63             return (1000 * cnt) / ti;
64         }
65         return 0;
66     }
67
68     /**
69      * Sets mbean server to use when registering mbeans.
70      * <p>By default {@link java.lang.management.ManagementFactory#getPlatformMBeanServer()} is used.
71      *
72      * @param mbeanServer mbean server.
73      */

74     public static void setMBeanServer(MBeanServer JavaDoc mbeanServer) {
75         JmxEtlManager.mbeanServer = mbeanServer;
76     }
77
78     /**
79      * Registers this manager as a JMX mbean.
80      */

81     public synchronized void register() {
82         if (name != null) {
83             throw new IllegalStateException JavaDoc("MBean already registered");
84         }
85         server = getMBeanServer();
86         String JavaDoc url = ctx.getScriptFileURL().toString();
87         boolean registered = false;
88         for (int i = 0; i < 1000; i++) {
89             if (name == null || server.isRegistered(name)) {
90                 registered = true;
91                 name = toObjectName(url, i);
92             } else {
93                 registered = false;
94                 break;
95             }
96         }
97         etlThread = Thread.currentThread();
98         if (!registered) {
99             try {
100                 server.registerMBean(this, name);
101                 started = new Date JavaDoc();
102                 LOG.info("Registered JMX mbean: " + name);
103             } catch (Exception JavaDoc e) {
104                 throw new SystemException("Unable to register mbean " + name, e);
105             }
106         } else {
107             throw new SystemException("Unable to register mbean for url " + url + ": too many equal tasks already registered");
108         }
109     }
110
111     private static MBeanServer JavaDoc getMBeanServer() {
112         return mbeanServer == null ? ManagementFactory.getPlatformMBeanServer() : mbeanServer;
113     }
114
115     /**
116      * Cancels all in-progress ETL tasks.
117      *
118      * @return number of cancelled ETL tasks.
119      */

120     public static synchronized int cancelAll() {
121         Set JavaDoc<ObjectName JavaDoc> names = findEtlMBeans();
122         MBeanServer JavaDoc srv = getMBeanServer();
123         int cancelled = 0;
124         for (ObjectName JavaDoc objectName : names) {
125             try {
126                 srv.invoke(objectName, "cancel", null, null);
127                 cancelled++;
128             } catch (Exception JavaDoc e) {
129                 LOG.log(Level.WARNING, "Cannot cancel ETL, MBean " + objectName, e);
130             }
131         }
132         return cancelled;
133     }
134
135     /**
136      * Find ETL mbeans.
137      *
138      * @return set of object names.
139      */

140     public static synchronized Set JavaDoc<ObjectName JavaDoc> findEtlMBeans() {
141         try {
142             return getMBeanServer().queryNames(new ObjectName JavaDoc(MBEAN_NAME_PREFIX + ",*"), null);
143         } catch (MalformedObjectNameException JavaDoc e) {
144             throw new IllegalStateException JavaDoc(e.getMessage(), e);
145         }
146     }
147
148     static ObjectName JavaDoc toObjectName(String JavaDoc url, int n) {
149         try {
150             return new ObjectName JavaDoc(MBEAN_NAME_PREFIX + ",url=" + ObjectName.quote(url) + (n > 0 ? ",n=" + n : ""));
151         } catch (MalformedObjectNameException JavaDoc e) { //Should not happen
152
throw new IllegalStateException JavaDoc("Cannot set MBean name", e);
153         }
154
155     }
156
157     /**
158      * Unregisters this manager from the JMX server.
159      * <p>This method does not throws any exceptions.
160      */

161     public synchronized void unregister() {
162         if (name != null && server != null) {
163             try {
164                 server.unregisterMBean(name);
165             } catch (Exception JavaDoc e) {
166                 LOG.log(Level.WARNING, "Unable to unregister mbean " + name, e);
167             }
168             name = null;
169         }
170     }
171
172     public synchronized void cancel() {
173         if (etlThread != null && etlThread.isAlive() && !etlThread.isInterrupted()) {
174             etlThread.interrupt();
175             etlThread = null;
176         }
177     }
178 }
179
Popular Tags