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) I am taking message, mobile no, period, from user (JobSchedule.jsp)
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) 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) In TestJob.java i am calling my message sending method but i dont know how to pass mobile no and message from tht?
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 (Exception e) {
e.printStackTrace();
}
log.debug("TestJob run successfully...");
}