user9642844
user9642844

Reputation:

JavaFx: about quartz scheduler

Hello I have a problem in a routine using quartz scheduler I need to shutdown my Scheduler method: javafx stop

I can't declare my scheduler out of my stage start:

@Override
    public void start(Stage stage) throws Exception {
        Scheduler s = StdSchedulerFactory.getDefaultScheduler();
        JobDetail j = JobBuilder.newJob(ChecarJob.class).build();
        Trigger t = TriggerBuilder.newTrigger().withIdentity("CroneTrigger")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60).repeatForever()).build();  
        try { 
        s.start();
        try {
            s.scheduleJob(j,t);
        } catch (Exception e) {
             e.printStackTrace();
        }      
        } catch (SchedulerException se) {
        se.printStackTrace();
        }
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Principal.fxml")); //carrega fxml
        Scene scene = new Scene(root); //coloca o fxml em uma cena
        stage.setScene(scene); // coloca a cena em uma janela
        stage.show(); //abre a janela
        setStage(stage);

    }

and I need to declare it was outside my start to be able to use shutdown inside stop ()

@Override
    public void stop() {
        UsuarioDAO u = new UsuarioDAO();
                    u.setOffiline();
                    s.shutdown();
                    Platform.exit();
                    System.exit(0);
    }

if I do that I did above I have an error because my Scheduler was created inside a method and is not global And the scheduler doesn't allow me to create it at global scope for some reason

my code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import dao.UsuarioDAO;
import dao.qtdRegistrosDAO;
import rotinas.BackupJob;
import rotinas.ChecarJob;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.JOptionPane;

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


/**
 * FXML Controller class
 *
 * @author SpiriT
 */
public class Principal extends Application {

    private static Stage stage; //uma janela
    private static qtdRegistrosDAO aQtdRegistrosDAO;
    public Principal() {
    }

    private void blockMultiInstance() {
        try {
            ServerSocket serverSocket = new ServerSocket(9581);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Software já está aberto!", "Atenção", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }
    }


    private void backup (){
        try {
            Scheduler sx = StdSchedulerFactory.getDefaultScheduler();
            JobDetail jx = JobBuilder.newJob(BackupJob.class).build();
            Trigger tx = TriggerBuilder.newTrigger().withIdentity("CroneTrigger2")
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60).repeatForever()).build();
            try {
                sx.start();
                try {
                    sx.scheduleJob(jx,tx);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (SchedulerException se) {
                se.printStackTrace();
            }
        } catch (SchedulerException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
        public void stop() {
            UsuarioDAO u = new UsuarioDAO();
                        u.setOffiline();
                        s.shutdown();
                        Platform.exit();
                        System.exit(0);
        }

    @Override
    public void start(Stage stage) throws Exception {
        Scheduler s = StdSchedulerFactory.getDefaultScheduler();
        JobDetail j = JobBuilder.newJob(ChecarJob.class).build();
        Trigger t = TriggerBuilder.newTrigger().withIdentity("CroneTrigger")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60).repeatForever()).build();  
        try { 
        s.start();
        try {
            s.scheduleJob(j,t);
        } catch (Exception e) {
             e.printStackTrace();
        }      
        } catch (SchedulerException se) {
        se.printStackTrace();
        }
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Principal.fxml")); //carrega fxml
        Scene scene = new Scene(root); //coloca o fxml em uma cena
        stage.setScene(scene); // coloca a cena em uma janela
        stage.show(); //abre a janela
        setStage(stage);
    }

    public static Stage getStage() {
        return stage;
    }

    public static void setStage(Stage stage) {
        Principal.stage = stage;
    }

    public static void main(String[] args) {
        launch(args);
    }

}

If I raise her out of my start stage I can't

Scheduler s = StdSchedulerFactory.getDefaultScheduler();
JobDetail j = JobBuilder.newJob(ChecarJob.class).build();
Trigger t = TriggerBuilder.newTrigger().withIdentity("CroneTrigger"

Upvotes: 0

Views: 230

Answers (1)

jewelsea
jewelsea

Reputation: 159406

  1. Define a reference to the scheduler as a member of the Application class.
  2. Assign the scheduler reference in your start method.
  3. When the application is stopped, call the appropriate method on the scheduler to safely shut it down.

Sample code

public class Principal extends Application {
    private Scheduler s;

    public void start(Stage stage) throws Exception {
        s = StdSchedulerFactory.getDefaultScheduler();
        // other work ...
    }

    public void stop() {
        if (s != null) {
            // pass true as a parameter if you want to wait
            // for scheduled jobs to complete 
            // (though it will hang UI if you do that on FX thread).
            s.shutdown();  
        }
    }
}

There may be other issues with your code (I haven't checked), and I don't know if this answer will solve the core of your problem, but it will allow you to define a Scheduler instance as a reference in your application, which seems to be something you are asking for.

Upvotes: 2

Related Questions