servlet中的Quartz Scheduler [英] Quartz Scheduler in servlets

查看:229
本文介绍了servlet中的Quartz Scheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我想安排使用quartz发送消息。但我真的不知道如何。
我可以向给定的手机发送消息,但是为了安排它的困难。我正在尝试的是

Hello everyone I want to schedule message sending using quartz. But i dont know really how. I can send message to given mobile no but for scheduling its difficult. What I am trying is

1)我正在从用户(JobSchedule.jsp)接收消息,移动号码,句号

1) I am taking message, mobile no, period, from user (JobSchedule.jsp)

2)我正在调用一个Jobscheduler Servlet(JobSchdeduleServlet.java)..在这里我不知道如何将变量传递给类(TestJob.java)

2) I am calling a Jobscheduler Servlet (JobSchdeduleServlet.java) .. here I dnt know how to pass variables to class(TestJob.java)

// JobSchdeduleServlet.java ..
      //specify the job's details..

        JobDetail job = JobBuilder.newJob(TestJob.class)
            .withIdentity("testJob")
            .build();

    //  SimpleScheduleBuilder.simpleSchedule()
    //  .withIntervalInSeconds(120);
        // specify the running period of the job
        int count=Integer.parseInt(request.getParameter("count"));
        int hours= Integer.parseInt(request.getParameter("Period"));
        request.getSession().setAttribute("nmo", request.getParameter("mobNo"));
        request.getSession().setAttribute("msg", request.getParameter("tskMsg"));
        String msg=request.getParameter("tskMsg");
        String mbno=request.getParameter("mobNo");
        Trigger trigger = TriggerBuilder.newTrigger()
              .withSchedule(
                    SimpleScheduleBuilder
                    .repeatHourlyForTotalCount(count, hours))
                         .build();


        //schedule the job
        SchedulerFactory schFactory = new StdSchedulerFactory();
        Scheduler sch = schFactory.getScheduler();
        sch.start();
        sch.scheduleJob(job, trigger);

3)从我的servlet调用TestJob.java

3) From that servlet I am calling TestJob.java

 // TestJob.java
      public void execute(JobExecutionContext jExeCtx) throws JobExecutionException {
    try {           
        System.out.println("Printing ......"+jExeCtx);
        SendSms.sendSms("9762809280", "Hi");// Here I dont know how to pass user defined mobile no n Message
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
log.debug("TestJob run successfully...");
}

4)在TestJob.java中我调用了我的消息发送方法,但我不这样做知道如何传递移动号码和来自的消息?

4) In TestJob.java i am calling my message sending method but i dont know how to pass mobile no and message from tht?

推荐答案

使用JobDataMap保存自定义数据并在执行作业期间使用。

Use JobDataMap to hold your custom data and use during the executing of job.

Ex:

JobDetail job = JobBuilder.newJob(TestJob.class)
        .withIdentity("testJob")
        .build();
job.getJobDataMap().put("mobile", "1234567890");
job.getJobDataMap().put("msg", "Your balance is low");

public void execute(JobExecutionContext jExeCtx) throws JobExecutionException {
try {           
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String msg = dataMap.getString("msg");
String mobile = dataMap.getFloat("mobile");
SendSms.sendSms(mobile,msg);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
log.debug("TestJob run successfully...");
}

这篇关于servlet中的Quartz Scheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆