1 2 18 19 22 package org.quartz.ee.jmx.jboss; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.util.Properties ; 28 29 import javax.naming.InitialContext ; 30 import javax.naming.Name ; 31 import javax.naming.NamingException ; 32 33 import org.quartz.Scheduler; 34 import org.quartz.SchedulerConfigException; 35 import org.quartz.SchedulerException; 36 import org.quartz.impl.StdSchedulerFactory; 37 38 import org.jboss.naming.NonSerializableFactory; 39 import org.jboss.system.ServiceMBeanSupport; 40 41 62 public class QuartzService extends ServiceMBeanSupport implements 63 QuartzServiceMBean { 64 65 72 73 private Properties properties; 74 75 private StdSchedulerFactory schedulerFactory; 76 77 private String jndiName; 78 79 private String propertiesFile; 80 81 private boolean error; 82 83 private boolean useProperties; 84 85 private boolean usePropertiesFile; 86 87 91 private boolean startScheduler = true; 92 93 100 101 public QuartzService() { 102 error = false; 104 105 usePropertiesFile = false; 107 propertiesFile = ""; 108 109 useProperties = false; 111 properties = new Properties (); 112 113 jndiName = "Quartz"; 115 } 116 117 124 125 public void setJndiName(String jndiName) throws Exception { 126 String oldName = this.jndiName; 127 this.jndiName = jndiName; 128 129 if (super.getState() == STARTED) { 130 unbind(oldName); 131 132 try { 133 rebind(); 134 } catch (NamingException ne) { 135 log.error("Failed to rebind Scheduler", ne); 136 137 throw new SchedulerConfigException( 138 "Failed to rebind Scheduler - ", ne); 139 } 140 } 141 } 142 143 public String getJndiName() { 144 return jndiName; 145 } 146 147 public String getName() { 148 return "QuartzService(" + jndiName + ")"; 149 } 150 151 public void setProperties(String properties) { 152 if (usePropertiesFile) { 153 log 154 .error("Must specify only one of 'Properties' or 'PropertiesFile'"); 155 156 error = true; 157 158 return; 159 } 160 161 useProperties = true; 162 163 try { 164 ByteArrayInputStream bais = new ByteArrayInputStream (properties 165 .getBytes()); 166 this.properties = new Properties (); 167 this.properties.load(bais); 168 } catch (IOException ioe) { 169 } 171 } 172 173 public String getProperties() { 174 try { 175 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 176 properties.store(baos, ""); 177 178 return new String (baos.toByteArray()); 179 } catch (IOException ioe) { 180 return ""; 182 } 183 } 184 185 public void setPropertiesFile(String propertiesFile) { 186 if (useProperties) { 187 log 188 .error("Must specify only one of 'Properties' or 'PropertiesFile'"); 189 190 error = true; 191 192 return; 193 } 194 195 usePropertiesFile = true; 196 197 this.propertiesFile = propertiesFile; 198 } 199 200 public String getPropertiesFile() { 201 return propertiesFile; 202 } 203 204 public void setStartScheduler(boolean startScheduler) { 205 this.startScheduler = startScheduler; 206 } 207 208 public boolean getStartScheduler() { 209 return startScheduler; 210 } 211 212 public void createService() throws Exception { 213 log.info("Create QuartzService(" + jndiName + ")..."); 214 215 if (error) { 216 log 217 .error("Must specify only one of 'Properties' or 'PropertiesFile'"); 218 219 throw new Exception ( 220 "Must specify only one of 'Properties' or 'PropertiesFile'"); 221 } 222 223 schedulerFactory = new StdSchedulerFactory(); 224 225 try { 226 if (useProperties) { 227 schedulerFactory.initialize(properties); 228 } 229 230 if (usePropertiesFile) { 231 schedulerFactory.initialize(propertiesFile); 232 } 233 } catch (Exception e) { 234 log.error("Failed to initialize Scheduler", e); 235 236 throw new SchedulerConfigException( 237 "Failed to initialize Scheduler - ", e); 238 } 239 240 log.info("QuartzService(" + jndiName + ") created."); 241 } 242 243 public void destroyService() throws Exception { 244 log.info("Destroy QuartzService(" + jndiName + ")..."); 245 246 schedulerFactory = null; 247 248 log.info("QuartzService(" + jndiName + ") destroyed."); 249 } 250 251 public void startService() throws Exception { 252 log.info("Start QuartzService(" + jndiName + ")..."); 253 254 try { 255 rebind(); 256 } catch (NamingException ne) { 257 log.error("Failed to rebind Scheduler", ne); 258 259 throw new SchedulerConfigException("Failed to rebind Scheduler - ", 260 ne); 261 } 262 263 try { 264 Scheduler scheduler = schedulerFactory.getScheduler(); 265 266 if (startScheduler) { 267 scheduler.start(); 268 } else { 269 log.info("Skipping starting the scheduler (will not run jobs)."); 270 } 271 } catch (Exception e) { 272 log.error("Failed to start Scheduler", e); 273 274 throw new SchedulerConfigException("Failed to start Scheduler - ", 275 e); 276 } 277 278 log.info("QuartzService(" + jndiName + ") started."); 279 } 280 281 public void stopService() throws Exception { 282 log.info("Stop QuartzService(" + jndiName + ")..."); 283 284 try { 285 Scheduler scheduler = schedulerFactory.getScheduler(); 286 287 scheduler.shutdown(); 288 } catch (Exception e) { 289 log.error("Failed to shutdown Scheduler", e); 290 291 throw new SchedulerConfigException( 292 "Failed to shutdown Scheduler - ", e); 293 } 294 295 unbind(jndiName); 296 297 log.info("QuartzService(" + jndiName + ") stopped."); 298 } 299 300 private void rebind() throws NamingException , SchedulerException { 301 InitialContext rootCtx = null; 302 try { 303 rootCtx = new InitialContext (); 304 Name fullName = rootCtx.getNameParser("").parse(jndiName); 305 Scheduler scheduler = schedulerFactory.getScheduler(); 306 NonSerializableFactory.rebind(fullName, scheduler, true); 307 } finally { 308 if (rootCtx != null) { 309 try { 310 rootCtx.close(); 311 } catch (NamingException ignore) {} 312 } 313 } 314 } 315 316 private void unbind(String jndiName) { 317 InitialContext rootCtx = null; 318 try { 319 rootCtx = new InitialContext (); 320 rootCtx.unbind(jndiName); 321 NonSerializableFactory.unbind(jndiName); 322 } catch (NamingException e) { 323 log.warn("Failed to unbind scheduler with jndiName: " + jndiName, e); 324 } finally { 325 if (rootCtx != null) { 326 try { 327 rootCtx.close(); 328 } catch (NamingException ignore) {} 329 } 330 } 331 } 332 } 333 | Popular Tags |