protected Scheduler findScheduler(String schedulerName) throws SchedulerException { if (this.beanFactory instanceof ListableBeanFactory) { ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory; String[] beanNames = lbf.getBeanNamesForType(Scheduler.class); for (String beanName : beanNames) { Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName); if (schedulerName.equals(schedulerBean.getSchedulerName())) { return schedulerBean; } } } Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName); if (schedulerInRepo == null) { throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found"); } return schedulerInRepo; }
/** * Create the Scheduler instance for the given factory and scheduler name. * Called by {@link #afterPropertiesSet}. * <p>The default implementation invokes SchedulerFactory's {@code getScheduler} * method. Can be overridden for custom Scheduler creation. * @param schedulerFactory the factory to create the Scheduler with * @param schedulerName the name of the scheduler to create * @return the Scheduler instance * @throws SchedulerException if thrown by Quartz methods * @see #afterPropertiesSet * @see org.quartz.SchedulerFactory#getScheduler */ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) throws SchedulerException { // Override thread context ClassLoader to work around naive Quartz ClassLoadHelper loading. Thread currentThread = Thread.currentThread(); ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(); boolean overrideClassLoader = (this.resourceLoader != null && !this.resourceLoader.getClassLoader().equals(threadContextClassLoader)); if (overrideClassLoader) { currentThread.setContextClassLoader(this.resourceLoader.getClassLoader()); } try { SchedulerRepository repository = SchedulerRepository.getInstance(); synchronized (repository) { Scheduler existingScheduler = (schedulerName != null ? repository.lookup(schedulerName) : null); Scheduler newScheduler = schedulerFactory.getScheduler(); if (newScheduler == existingScheduler) { throw new IllegalStateException("Active Scheduler of name '" + schedulerName + "' already registered " + "in Quartz SchedulerRepository. Cannot create a new Spring-managed Scheduler of the same name!"); } if (!this.exposeSchedulerInRepository) { // Need to remove it in this case, since Quartz shares the Scheduler instance by default! SchedulerRepository.getInstance().remove(newScheduler.getSchedulerName()); } return newScheduler; } } finally { if (overrideClassLoader) { // Reset original thread context ClassLoader. currentThread.setContextClassLoader(threadContextClassLoader); } } }
public static Scheduler startup(Properties props) throws SchedulerException { String schedname = props.getProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "QuartzScheduler"); SchedulerRepository schedRep = SchedulerRepository.getInstance(); Scheduler scheduler = schedRep.lookup(schedname); if (scheduler == null || scheduler.isShutdown()) { logger.info("Initializing Scheduler..."); StdSchedulerFactory factory = new StdSchedulerFactory(); factory.initialize(props); scheduler = factory.getScheduler(); logger.info("Scheduler has been initialized..."); return scheduler; } else { throw new SchedulerException(MessageFormat.format("Scheduler with name \"{0}\" already exists. Choose a different name.", schedname)); } }
@Override protected void doStop() throws Exception { // shutdown and unregister the scheduler instance scheduler.shutdown(); SchedulerRepository.getInstance().remove(SCHEDULER_NAME); scheduler = null; }
@Test public void testSchedulerRepositoryExposure() throws InterruptedException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/schedulerRepositoryExposure.xml"); assertSame(SchedulerRepository.getInstance().lookup("myScheduler"), ctx.getBean("scheduler")); ctx.close(); }
public static String[] getSchedulers() throws SchedulerException { ArrayList<String> l = new ArrayList<String>(); Collection<Scheduler> schedulers = SchedulerRepository.getInstance().lookupAll(); for (Scheduler s : schedulers) { if (!s.isShutdown()) { l.add(s.getSchedulerName()); } } return l.toArray(new String [l.size()]); }
/** * Create the Scheduler instance for the given factory and scheduler name. * Called by {@link #afterPropertiesSet}. * <p>The default implementation invokes SchedulerFactory's <code>getScheduler</code> * method. Can be overridden for custom Scheduler creation. * @param schedulerFactory the factory to create the Scheduler with * @param schedulerName the name of the scheduler to create * @return the Scheduler instance * @throws SchedulerException if thrown by Quartz methods * @see #afterPropertiesSet * @see org.quartz.SchedulerFactory#getScheduler */ protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) throws SchedulerException { // Override thread shard ClassLoader to work around naive Quartz ClassLoadHelper loading. Thread currentThread = Thread.currentThread(); ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(); boolean overrideClassLoader = (this.resourceLoader != null && !this.resourceLoader.getClassLoader().equals(threadContextClassLoader)); if (overrideClassLoader) { currentThread.setContextClassLoader(this.resourceLoader.getClassLoader()); } try { SchedulerRepository repository = SchedulerRepository.getInstance(); synchronized (repository) { Scheduler existingScheduler = (schedulerName != null ? repository.lookup(schedulerName) : null); Scheduler newScheduler = schedulerFactory.getScheduler(); if (newScheduler == existingScheduler) { throw new IllegalStateException("Active Scheduler of name '" + schedulerName + "' already registered " + "in Quartz SchedulerRepository. Cannot create a new Spring-managed Scheduler of the same name!"); } if (!this.exposeSchedulerInRepository) { // Need to remove it in this case, since Quartz shares the Scheduler instance by default! SchedulerRepository.getInstance().remove(newScheduler.getSchedulerName()); } return newScheduler; } } finally { if (overrideClassLoader) { // Reset original thread shard ClassLoader. currentThread.setContextClassLoader(threadContextClassLoader); } } }
@Test public void schedulerRepositoryExposure() throws Exception { ClassPathXmlApplicationContext ctx = context("schedulerRepositoryExposure.xml"); assertSame(SchedulerRepository.getInstance().lookup("myScheduler"), ctx.getBean("scheduler")); ctx.close(); }
@SuppressWarnings("unchecked") public static List<Scheduler> getAllSchedulers() { return new ArrayList<Scheduler>(SchedulerRepository.getInstance().lookupAll()); }
/** * 删除不够通用 * @param schedName * @return */ public static Scheduler getScheduler(String schedName) { return SchedulerRepository.getInstance().lookup(schedName); }
/** * 获取仓库中所有的Scheduler集合 * @return */ public static Collection<Scheduler> getAllScheduler(){ return SchedulerRepository.getInstance().lookupAll(); }