Reputation: 2647
I wanted to put the schedule job in one server and run jobs on another server can we assign job class of different application,
I am using JobStoreTX and use MySQL for data store.
or
is there any other way that we can call another application in java and schedule can be done there only?
I am using following quartz property file.
# 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
I am using following server for store in local table
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();
}
}
}
and
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;
}
}
Upvotes: 2
Views: 2882
Reputation: 2647
This can be done by only starting the scheduler from another application. you can put following code in your another application.
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();
}
}
}
And in your first application don't start the scheduler only assign the job class and other trigger.
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;
}
}
Upvotes: 2
Reputation: 11
I think what you are asking is a feature of the commercial version of quartz. What you can do is separating triggering form esecution. Use quartz to send a jms message to a server test will perform the job.
Upvotes: 0