⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcmsscheduler.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 3));

        if (TestScheduledJob.m_runCount == 3) {
            System.out.println("Test job was correctly run 3 times in OpenCms scheduler.");
        } else {
            fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
        }
        
        jobInfo = scheduler.getJob(jobInfo.getId());
        assertEquals("My job", jobInfo.getJobName());
        
        CmsScheduledJobInfo newInfo = (CmsScheduledJobInfo)jobInfo.clone();
        newInfo.setJobName("My CHANGED name");
        newInfo.setActive(true);
        assertEquals(1, scheduler.getJobs().size());
        
        // re-schedule the job with a different name
        scheduler.scheduleJob(null, newInfo);
        // size must still be the same
        assertEquals(jobInfo.getId(), newInfo.getId());
        assertEquals(1, scheduler.getJobs().size());        
        jobInfo = scheduler.getJob(newInfo.getId());
        assertEquals("My CHANGED name", jobInfo.getJobName());
        
        // change cron expression to something invalid
        newInfo = (CmsScheduledJobInfo)jobInfo.clone();
        newInfo.setActive(true);
        newInfo.setCronExpression("* * * * * *");
        assertEquals(1, scheduler.getJobs().size());
        
        CmsSchedulerException error = null;
        try {
            // re-schedule the job with a different name
            scheduler.scheduleJob(null, newInfo); 
        } catch (CmsSchedulerException e) {
            error = e;
        }
        assertNotNull(error);
        // ensure the job is still scheduled
        assertEquals(1, scheduler.getJobs().size());        
        
        // shutdown the scheduler
        scheduler.shutDown();        
    }

    /**
     * Tests execution of jobs using CmsSchedulerThreadPool.<p>
     * 
     * @throws Exception if something goes wrong
     */
    public void testBasicJobExecution() throws Exception {

        System.out.println("Testing the OpenCms tread pool.");
        Scheduler scheduler = initOpenCmsScheduler();        
        
        JobDetail[] jobDetail = new JobDetail[THREADS_TO_RUN];
        SimpleTrigger[] trigger = new SimpleTrigger[THREADS_TO_RUN];

        for (int i = 0; i < jobDetail.length; i++) {
            jobDetail[i] = new JobDetail(
                "myJob" + i, 
                Scheduler.DEFAULT_GROUP, 
                TestCmsJob.class);

            trigger[i] = new SimpleTrigger(
                "myTrigger" + i, 
                Scheduler.DEFAULT_GROUP, 
                new Date(), 
                null, 
                0,
                0L);
        }

        for (int i = 0; i < THREADS_TO_RUN; i++) {
            scheduler.scheduleJob(jobDetail[i], trigger[i]);
        }

        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestCmsJob.m_running > 0));


        if (TestCmsJob.m_running <= 0) {
            System.out.println("Success: All threads are finished.");
        } else {
            fail("Some threads in the pool are still running after " + SECONDS_TO_WAIT + " seconds.");
        }
        
        scheduler.shutdown();        
    }
    
    /**
     * Tests launching of an OpenCms job.<p>
     *  
     * @throws Exception if something goes wrong
     */
    public void testCmsJobLaunch() throws Exception {

        System.out.println("Trying to run an OpenCms job 5x.");
        TestScheduledJob.m_runCount = 0;
        
        Scheduler scheduler = initOpenCmsScheduler();
        
        JobDetail jobDetail = new JobDetail(
            "cmsLaunch",
            Scheduler.DEFAULT_GROUP, 
            CmsScheduleManager.class);
        
        CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
        CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin());
        jobInfo.setContextInfo(contextInfo);
        jobInfo.setClassName(TestScheduledJob.class.getName());  
        
        JobDataMap jobData = new JobDataMap();      
        jobData.put(CmsScheduleManager.SCHEDULER_JOB_INFO, jobInfo);
        
        jobDetail.setJobDataMap(jobData);
        
        CronTrigger trigger = new CronTrigger("cmsLaunchTrigger", Scheduler.DEFAULT_GROUP);
        
        trigger.setCronExpression("0/2 * * * * ?");
        
        scheduler.scheduleJob(jobDetail, trigger);
        
        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));


        if (TestScheduledJob.m_runCount == 5) {
            System.out.println("Success: Test job was run 5 times.");
        } else {
            fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
        }
       
        scheduler.shutdown();        
    }
    
    /**
     * Tests launching of an OpenCms job with the OpenCms schedule manager.<p>
     *  
     * @throws Exception if something goes wrong
     */
    public void testJobInOpenCmsScheduler() throws Exception {

        System.out.println("Trying to run an OpenCms job 5x with the OpenCms scheduler.");
        TestScheduledJob.m_runCount = 0;
        
        CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
        CmsContextInfo contextInfo = new CmsContextInfo();
        contextInfo.setUserName(OpenCms.getDefaultUsers().getUserAdmin());
        jobInfo.setContextInfo(contextInfo);
        jobInfo.setClassName(TestScheduledJob.class.getName());  
        
        jobInfo.setCronExpression("0/2 * * * * ?");
                
        List jobs = new ArrayList();
        jobs.add(jobInfo);
        // create the scheduler with the test job
        CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
        
        // initialize the manager, this will start the scheduled jobs
        scheduler.initialize(null);
        
        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));

        if (TestScheduledJob.m_runCount == 5) {
            System.out.println("Test job was correctly run 5 times in OpenCms scheduler.");
        } else {
            fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
        }
        
        if (TestScheduledJob.m_instanceCountCopy == 1) {
            System.out.println("Instance counter has correct value of 1.");
        } else {
            fail("Instance counter value of " + TestScheduledJob.m_instanceCountCopy + " invalid!");
        }             
       
        // shutdown the scheduler
        scheduler.shutDown();        
    }         
    
    /**
     * Tests launching of a persistent OpenCms job with the OpenCms schedule manager.<p>
     *  
     * @throws Exception if something goes wrong
     */
    public void testPersitentJobInOpenCmsScheduler() throws Exception {

        System.out.println("Trying to run a persistent OpenCms job 5x with the OpenCms scheduler.");
        TestScheduledJob.m_runCount = 0;
        
        CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
        CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin());
        jobInfo.setContextInfo(contextInfo);
        jobInfo.setClassName(TestScheduledJob.class.getName());
        jobInfo.setReuseInstance(true);
        jobInfo.setCronExpression("0/2 * * * * ?");
        
        List jobs = new ArrayList();
        jobs.add(jobInfo);
        // create the scheduler with the test job
        CmsScheduleManager scheduler = new CmsScheduleManager(jobs);
        
        // initialize the manager, this will start the scheduled jobs
        scheduler.initialize(null);
        
        int seconds = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                fail("Something caused the waiting test thread to interrupt!");
            }
            seconds++;
        } while ((seconds < SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));


        if (TestScheduledJob.m_runCount == 5) {
            System.out.println("Test job was correctly run 5 times in OpenCms scheduler.");
        } else {
            fail("Test class not run after " + SECONDS_TO_WAIT + " seconds.");
        }
        
        if (TestScheduledJob.m_instanceCountCopy == 5) {
            System.out.println("Instance counter was correctly incremented 5 times.");
        } else {
            fail("Instance counter was not incremented!");
        }        
       
        // shutdown the scheduler
        scheduler.shutDown();        
    }
    
    /**
     * Initializes a Quartz scheduler.<p>
     * 
     * @return the initialized scheduler
     * @throws Exception in case something goes wrong
     */
    private Scheduler initOpenCmsScheduler() throws Exception  {
        
        Properties properties = new Properties();
        properties.put("org.quartz.scheduler.instanceName", "OpenCmsScheduler");
        properties.put("org.quartz.scheduler.threadName", "OpenCms: Scheduler");
        properties.put("org.quartz.scheduler.rmi.export", CmsStringUtil.FALSE);
        properties.put("org.quartz.scheduler.rmi.proxy", CmsStringUtil.FALSE);
        properties.put("org.quartz.scheduler.xaTransacted", CmsStringUtil.FALSE);
        properties.put("org.quartz.threadPool.class", "org.opencms.scheduler.CmsSchedulerThreadPool");
        properties.put("org.quartz.jobStore.misfireThreshold", "60000");
        properties.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");

        SchedulerFactory schedulerFactory = new StdSchedulerFactory(properties);
        Scheduler scheduler = schedulerFactory.getScheduler();

        scheduler.getMetaData();
        scheduler.start();
        
        // also make sure CmsUUID is initialized
        CmsUUID.init(CmsUUID.getDummyEthernetAddress());
        
        return scheduler;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -