0
votes

Quartz Scheduler for a simple Java application for storing a jasper report in a directory is working, as i used public void execute instead of public static void main and executed in a job class as shown below,

i am not looking to pass the URL of the servlet id there a right way, when i run the scheduler servlet which will trigger the job class file, which will invoke the servlet class .

Help me out import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

 public class Starter_jasper implements Job {

 public Starter_jasper()
{
    JobExecutionContext jec = null;
    try {
        execute(jec);
    } catch (JobExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void execute(JobExecutionContext jec) throws JobExecutionException{
//include(Jasper_database.class);
System.out.println("" + new Date());

Jasper_database jb = new Jasper_database();
jb.execute();
//fill your jobs here
}}   this code works for a simple java 

It work for a simple java application but My Question is how do i do it for a servlet class, I run my servlet class directly and i am not using any JSP, and My scheduler and trigger is in an another servlet as shown below

      import java.io.IOException;

   import javax.servlet.GenericServlet;
   import javax.servlet.ServletConfig;
   import javax.servlet.ServletException;
   import javax.servlet.ServletRequest;
   import javax.servlet.ServletResponse;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import job.quartz.gateway.GatewayPlugin;

   import org.quartz.JobBuilder;
   import org.quartz.JobDetail;
   import org.quartz.ScheduleBuilder;
   import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
      import org.quartz.SimpleScheduleBuilder;
      import org.quartz.Trigger;
   import org.quartz.TriggerBuilder;
   import org.quartz.impl.StdSchedulerFactory;

   import com.jasper.servlet.JasperSReport;

  /**
    * Servlet implementation class QuartzServlet
   */
     public class QuartzServlet extends GenericServlet {
   private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public QuartzServlet() {
    super();
    // TODO Auto-generated constructor stub
}

public void init(ServletConfig config)throws ServletException{

    super.init(config);
    Scheduler sched;
    try{
        sched = StdSchedulerFactory.getDefaultScheduler();
        sched.start();
  //    JobDetail jd = new JobDetail("myjob", sched.DEFAULT_GROUP,GatewayPlugin.class);
        JobDetail job = JobBuilder.newJob(GatewayPlugin.class).
                        withIdentity("JasperSReport").build();
        ScheduleBuilder ScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().
                                          withIntervalInSeconds(20).
                                          repeatForever();
        Trigger trigger = TriggerBuilder.newTrigger().
                          withIdentity("QuartzTrigger").
                          withSchedule(ScheduleBuilder).
                          startNow().
                          build();

        sched.scheduleJob(job, trigger);

    }catch(SchedulerException e){
        System.out.println("Scheduler exception at class file shcedule or trigger"+e);

    }
}

public void service(
        ServletRequest arg0, ServletResponse arg1)throws ServletException, IOException{

  }
}
1
You mean calling a servlet from a servlet ? - Abderrazak BOUADMA
yes, i got three files 1) First is my Servlet class which can display jasper report on the browser or store a file in a directory 2) a Java file where i implement the job i which the my first servlet class is implemented and 3) the 3rd Servlet class which contains the trigger which invokes the 1st Servlet class through the 2nd java file were job is implemented. - swaroop k

1 Answers

0
votes

you'll not be able to instantiate the Servlet as it's the container concern but you can make :

request.getRequestDispatcher("/TARGET_SERVLET").forward(request,response);