在quartz中可以在一台服务器上配置作业并在另一台服务器上运行该作业吗?

时间:2014-01-23 07:15:30

标签: java quartz-scheduler

我想将计划作业放在一台服务器上并在另一台服务器上运行作业我们可以分配不同应用程序的作业类,

我正在使用JobStoreTX并使用MySQL进行数据存储。

有没有其他方法可以在java中调用另一个应用程序,只能在那里完成日程安排?

我正在使用以下quartz属性文件。

# Configure Main Scheduler Properties  

org.quartz.scheduler.instanceName: TestScheduler
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 1000
org.quartz.threadPool.threadPriority: 5

# Configure JobStore  
org.quartz.jobStore.misfireThreshold: 300000 
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties: false
org.quartz.jobStore.dataSource: myDS
org.quartz.jobStore.tablePrefix: QRTZ_
org.quartz.jobStore.isClustered: false

# Configure Datasources  
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/bsviewer2
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = root
org.quartz.dataSource.myDS.maxConnections = 5

我在本地表中使用以下服务器存储

package com.agileinfotech.bsviewer.servlet;


public class ScheduleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        Session session = HibernateUtil.getSessionFactory().openSession();

        try {



            PrintWriter out = response.getWriter();
            response.setContentType("text/html");


            HttpSession httpSession = request.getSession();
            String usrid = httpSession.getAttribute("userId") != null ? httpSession
                    .getAttribute("userId").toString() : "0";




            String txtJobName = request.getParameter("txtJobName");
            String schedule_type = request.getParameter("scheduleType");
            String onceDate = request.getParameter("oncejobDate");
            String onceTime = request.getParameter("oncejobTime");
            String rc_day = "";
            String[] rc_day1 = request.getParameterValues("sch_days");
            if (rc_day1 != null) {
                for (int j = 0; j < rc_day1.length; j++) {
                    rc_day = rc_day + "," + rc_day1[j];
                }
                if (rc_day1.length > 1) {
                    rc_day = rc_day.substring(1);
                }
            }
            String rc_time = request.getParameter("recjobTime");
            String documentName = request.getParameter("documentName");
            String isDocFlag = request.getParameter("DocFlag");
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            String emailCount = request.getParameter("cnter") != null ? request
                    .getParameter("cnter").toString() : "0";
            Date ffdate = null;
            Date once_time = null;
            Date recTime = null;
            HashMap paramMap = null;
            String documentFormat = "";

            if (schedule_type.equals("rightNow")) {
                jobStatus = tiggerJob.schedule_rightNow(all parameter);
            }
            if (schedule_type.equals("Once")) {
                jobStatus = tiggerJob.schedule_Once(ffdate, once_time, all parameter);
            }
            if (schedule_type.equals("Recurring")) {
                jobStatus = tiggerJob.schedule_Rec(recTime, rc_day, all parameter);
            }


        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

public class TriggerJob {

                    String jobStatus = "";
                    SchedulerMetaData metaData = null;

                    public String schedule_rightNow(HashMap ParamMap){  

                        try{
                            SchedulerFactory sf = new StdSchedulerFactory();
                            Scheduler sch = sf.getScheduler();
                            String txtjob=ParamMap.get("txtJobName").toString();
                            JobKey jobKey = new JobKey(txtjob, "Group1");
                            JobDetail job = newJob(JobSchedule.class).withIdentity(jobKey)
                                    .build();
                            long timestamp=System.currentTimeMillis();
                            String sTimeStamp = String.valueOf(timestamp);
                            SimpleTrigger trigger = newTrigger().withIdentity(sTimeStamp, "Group1")
                                    .startNow().
                                    withSchedule(simpleSchedule()
                                            .withIntervalInSeconds(1).withMisfireHandlingInstructionFireNow())
                                            .build();
                            sch.getListenerManager().addJobListener(new ListernerTrigger(), KeyMatcher.keyEquals(jobKey));
                            job.getJobDataMap().putAll(ParamMap);
                            Date ft  = sch.scheduleJob(job,trigger);


                            sch.start();
                            Thread.sleep(5L * 1000L);
                            sch.shutdown(true);
                            metaData = sch.getMetaData();
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                        return jobStatus;
                    }
                }

2 个答案:

答案 0 :(得分:2)

只需从其他应用程序启动调度程序即可完成。 您可以将以下代码放在另一个应用程序中。

    package packagename...;

    import java.io.File;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerFactory;
    import org.quartz.impl.StdSchedulerFactory;


    public class ScheduleServer {

        public ScheduleServer() {
            // TODO Auto-generated constructor stub
        }

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {


                SchedulerFactory sf = new StdSchedulerFactory();
                Scheduler sch = sf.getScheduler();
                sch.start();

            } catch (SchedulerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

在第一个应用程序中,不启动调度程序,只分配作业类和其他触发器。

public class TriggerJob {

                String jobStatus = "";
                SchedulerMetaData metaData = null;

                public String schedule_rightNow(HashMap ParamMap){  

                    try{
                        SchedulerFactory sf = new StdSchedulerFactory();
                        Scheduler sch = sf.getScheduler();
                        String txtjob=ParamMap.get("txtJobName").toString();
                        JobKey jobKey = new JobKey(txtjob, "Group1");
                        JobDetail job = newJob(JobSchedule.class).withIdentity(jobKey)
                                .build();
                        long timestamp=System.currentTimeMillis();
                        String sTimeStamp = String.valueOf(timestamp);
                        SimpleTrigger trigger = newTrigger().withIdentity(sTimeStamp, "Group1")
                                .startNow().
                                withSchedule(simpleSchedule()
                                        .withIntervalInSeconds(1).withMisfireHandlingInstructionFireNow())
                                        .build();
                        sch.getListenerManager().addJobListener(new ListernerTrigger(), KeyMatcher.keyEquals(jobKey));
                        job.getJobDataMap().putAll(ParamMap);
                        Date ft  = sch.scheduleJob(job,trigger);


                        sch.start();
                        Thread.sleep(5L * 1000L);
                        sch.shutdown(true);
                        metaData = sch.getMetaData();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    return jobStatus;
                }
            }

答案 1 :(得分:0)

我认为你所问的是石英商业版的特色。你可以做的是分离触发形式的执行。使用quartz将jms消息发送到服务器测试将执行该作业。