Search in sources :

Example 1 with SchedulerContext

use of org.quartz.SchedulerContext in project spring-framework by spring-projects.

the class QuartzSupportTests method schedulerFactoryBeanWithApplicationContext.

@Test
public void schedulerFactoryBeanWithApplicationContext() throws Exception {
    TestBean tb = new TestBean("tb", 99);
    StaticApplicationContext ac = new StaticApplicationContext();
    final Scheduler scheduler = mock(Scheduler.class);
    SchedulerContext schedulerContext = new SchedulerContext();
    given(scheduler.getContext()).willReturn(schedulerContext);
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean() {

        @Override
        protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) {
            return scheduler;
        }
    };
    schedulerFactoryBean.setJobFactory(null);
    Map<String, Object> schedulerContextMap = new HashMap<>();
    schedulerContextMap.put("testBean", tb);
    schedulerFactoryBean.setSchedulerContextAsMap(schedulerContextMap);
    schedulerFactoryBean.setApplicationContext(ac);
    schedulerFactoryBean.setApplicationContextSchedulerContextKey("appCtx");
    try {
        schedulerFactoryBean.afterPropertiesSet();
        schedulerFactoryBean.start();
        Scheduler returnedScheduler = schedulerFactoryBean.getObject();
        assertEquals(tb, returnedScheduler.getContext().get("testBean"));
        assertEquals(ac, returnedScheduler.getContext().get("appCtx"));
    } finally {
        schedulerFactoryBean.destroy();
    }
    verify(scheduler).start();
    verify(scheduler).shutdown(false);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HashMap(java.util.HashMap) Scheduler(org.quartz.Scheduler) SchedulerContext(org.quartz.SchedulerContext) SchedulerFactory(org.quartz.SchedulerFactory) Test(org.junit.Test)

Example 2 with SchedulerContext

use of org.quartz.SchedulerContext in project cachecloud by sohutv.

the class CleanUpStatisticsJob method action.

@Override
public void action(JobExecutionContext context) {
    if (!ConstUtils.WHETHER_SCHEDULE_CLEAN_DATA) {
        return;
    }
    try {
        SchedulerContext schedulerContext = context.getScheduler().getContext();
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get(APPLICATION_CONTEXT_KEY);
        InstanceStatsCenter InstanceStatsCenter = applicationContext.getBean("instanceStatsCenter", InstanceStatsCenter.class);
        try {
            InstanceStatsCenter.cleanUpStandardStats(5);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -31);
        Date time = calendar.getTime();
        int cleanCount = jdbcTemplate.update(CLEAN_APP_HOUR_COMMAND_STATISTICS, time);
        logger.warn("clean_app_hour_command_statistics count={}", cleanCount);
        cleanCount = jdbcTemplate.update(CLEAN_APP_MINUTE_COMMAND_STATISTICS, time);
        logger.warn("clean_app_minute_command_statistics count={}", cleanCount);
        cleanCount = jdbcTemplate.update(CLEAN_APP_HOUR_STATISTICS, time);
        logger.warn("clean_app_hour_statistics count={}", cleanCount);
        cleanCount = jdbcTemplate.update(CLEAN_APP_MINUTE_STATISTICS, time);
        logger.warn("clean_app_minute_statistics count={}", cleanCount);
        //清除客户端耗时数据(保存2天)
        ClientReportCostDistriService clientReportCostDistriService = applicationContext.getBean("clientReportCostDistriService", ClientReportCostDistriService.class);
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        long timeFormat = NumberUtils.toLong(new SimpleDateFormat("yyyyMMddHHmm00").format(calendar.getTime()));
        cleanCount = clientReportCostDistriService.deleteBeforeCollectTime(timeFormat);
        logger.warn("clean_app_client_costtime_minute_stat count={}", cleanCount);
        //清除客户端耗时汇总数据(保存14天)
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -14);
        timeFormat = NumberUtils.toLong(new SimpleDateFormat("yyyyMMddHHmm00").format(calendar.getTime()));
        cleanCount = jdbcTemplate.update(CLEAN_APP_CLIENT_MINUTE_COST_TOTAL, timeFormat);
        logger.warn("clean_app_client_costtime_minute_stat_total count={}", cleanCount);
        //清除客户端值数据(保存2天)
        ClientReportValueDistriService clientReportValueDistriService = applicationContext.getBean("clientReportValueDistriService", ClientReportValueDistriService.class);
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        timeFormat = NumberUtils.toLong(new SimpleDateFormat("yyyyMMddHHmm00").format(calendar.getTime()));
        cleanCount = clientReportValueDistriService.deleteBeforeCollectTime(timeFormat);
        logger.warn("clean_app_client_value_minute_stats count={}", cleanCount);
        //清除服务器统计数据
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        String date = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        cleanCount = jdbcTemplate.update(CLEAN_SERVER_STAT_STATISTICS, date);
        logger.warn("clean_server_stat_total count={}", cleanCount);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : Calendar(java.util.Calendar) ClientReportCostDistriService(com.sohu.cache.client.service.ClientReportCostDistriService) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Date(java.util.Date) ApplicationContext(org.springframework.context.ApplicationContext) SchedulerContext(org.quartz.SchedulerContext) InstanceStatsCenter(com.sohu.cache.stats.instance.InstanceStatsCenter) ClientReportValueDistriService(com.sohu.cache.client.service.ClientReportValueDistriService) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with SchedulerContext

use of org.quartz.SchedulerContext in project cachecloud by sohutv.

the class ErrorStatisticsJob method action.

@Override
public void action(JobExecutionContext context) {
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    SchedulerContext schedulerContext;
    try {
        schedulerContext = context.getScheduler().getContext();
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get(APPLICATION_CONTEXT_KEY);
        EmailComponent emailComponent = applicationContext.getBean("emailComponent", EmailComponent.class);
        ErrorLoggerWatcherMBean errorLoggerWatcher = applicationContext.getBean("errorLoggerWatcher", ErrorLoggerWatcherMBean.class);
        //            if (errorLoggerWatcher.getTotalErrorCount() == 0L) {
        //                logger.warn("errorLoggerWatcher.totalErrorCount == 0 -o-");
        //                return;
        //            }
        String title = "CacheCloud异常统计, 日期:" + dateFormat.format(date) + ";服务器:" + System.getProperty("local.ip") + ";总数:" + errorLoggerWatcher.getTotalErrorCount();
        StringBuilder buffer = new StringBuilder();
        buffer.append(title + ":<br/>");
        for (Map.Entry<String, Long> entry : errorLoggerWatcher.getErrorInfos().entrySet()) {
            Long num = entry.getValue();
            if (num == 0L) {
                continue;
            }
            String key = entry.getKey();
            buffer.append(key + "=" + num + "<br/>");
        }
        emailComponent.sendMailToAdmin(title, buffer.toString());
        //清理异常
        errorLoggerWatcher.clear();
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SchedulerException(org.quartz.SchedulerException) SchedulerContext(org.quartz.SchedulerContext) EmailComponent(com.sohu.cache.web.component.EmailComponent) ErrorLoggerWatcherMBean(com.sohu.cache.jmx.ErrorLoggerWatcherMBean) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map) Date(java.util.Date)

Example 4 with SchedulerContext

use of org.quartz.SchedulerContext in project cachecloud by sohutv.

the class InstanceAlertValueJob method action.

@Override
public void action(JobExecutionContext context) {
    try {
        long startTime = System.currentTimeMillis();
        SchedulerContext schedulerContext = context.getScheduler().getContext();
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get(APPLICATION_CONTEXT_KEY);
        InstanceAlertValueService instanceAlertValueService = applicationContext.getBean("instanceAlertValueService", InstanceAlertValueService.class);
        int instanceSize = instanceAlertValueService.monitorLastMinuteAllInstanceInfo();
        logger.warn("InstanceAlertValueJob monitor {} instance, costtime {} ms", instanceSize, (System.currentTimeMillis() - startTime));
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SchedulerException(org.quartz.SchedulerException) InstanceAlertValueService(com.sohu.cache.stats.instance.InstanceAlertValueService) SchedulerContext(org.quartz.SchedulerContext)

Example 5 with SchedulerContext

use of org.quartz.SchedulerContext in project cachecloud by sohutv.

the class InspectorJob method action.

@Override
public void action(JobExecutionContext context) {
    try {
        long start = System.currentTimeMillis();
        SchedulerContext schedulerContext = context.getScheduler().getContext();
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get(APPLICATION_CONTEXT_KEY);
        // 应用相关
        InspectHandler inspectHandler;
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        String inspectorType = MapUtils.getString(jobDataMap, "inspectorType");
        if (StringUtils.isBlank(inspectorType)) {
            logger.error("=====================InspectorJob:inspectorType is null=====================");
            return;
        } else if (inspectorType.equals("host")) {
            inspectHandler = applicationContext.getBean("hostInspectHandler", InspectHandler.class);
        } else if (inspectorType.equals("app")) {
            inspectHandler = applicationContext.getBean("appInspectHandler", InspectHandler.class);
        } else {
            logger.error("=====================InspectorJob:inspectorType not match:{}=====================", inspectorType);
            return;
        }
        inspectHandler.handle();
        long end = System.currentTimeMillis();
        logger.info("=====================InspectorJob {} Done! cost={} ms=====================", inspectHandler.getClass().getSimpleName(), (end - start));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) JobDataMap(org.quartz.JobDataMap) SchedulerContext(org.quartz.SchedulerContext)

Aggregations

SchedulerContext (org.quartz.SchedulerContext)18 SchedulerException (org.quartz.SchedulerException)12 ApplicationContext (org.springframework.context.ApplicationContext)10 JobDataMap (org.quartz.JobDataMap)5 JobExecutionException (org.quartz.JobExecutionException)4 Date (java.util.Date)3 Trigger (org.quartz.Trigger)3 MachineCenter (com.sohu.cache.machine.MachineCenter)2 RedisCenter (com.sohu.cache.redis.RedisCenter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 CamelContext (org.apache.camel.CamelContext)2 Route (org.apache.camel.Route)2 RoutePolicy (org.apache.camel.spi.RoutePolicy)2 ClientReportCostDistriService (com.sohu.cache.client.service.ClientReportCostDistriService)1 ClientReportValueDistriService (com.sohu.cache.client.service.ClientReportValueDistriService)1 ErrorLoggerWatcherMBean (com.sohu.cache.jmx.ErrorLoggerWatcherMBean)1 AppDailyDataCenter (com.sohu.cache.stats.app.AppDailyDataCenter)1 InstanceAlertValueService (com.sohu.cache.stats.instance.InstanceAlertValueService)1 InstanceStatsCenter (com.sohu.cache.stats.instance.InstanceStatsCenter)1 EmailComponent (com.sohu.cache.web.component.EmailComponent)1