0

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"

1 Answers1

2
  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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Hello i thought it would solve but not when i close the stage is still open –  Sep 19 '19 at 01:03
  • @Override 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). try { UsuarioDAO u = new UsuarioDAO(); u.setOffiline(); s.shutdown(); Platform.exit(); System.exit(0); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } –  Sep 19 '19 at 01:03
  • I can't understand why –  Sep 19 '19 at 01:03
  • Code I provided should be sufficient to request the Quartz scheduler to shutdown. Likely you have other issues with your code than the scheduler shutdown request. Without all of the code to replicate the issue (a [mcve], emphasis on minimal) it may be hard for you to get help. – jewelsea Sep 19 '19 at 18:30
  • You do not need to call `Platform.exit()` and `System.exit(0)` (though that is not the root cause of your issue). Likely you have a non-daemon thread running (https://stackoverflow.com/questions/19505682/java-daemon-thread-and-non-daemon-thread) which prevents shutdown, or an unexpected exception being thrown which you aren't handling gracefully, or a long running scheduled job which never exits. You can break your app into minimal parts get it to a state which allows a clean exit, then gradually re-enable services. [jvisualvm](https://visualvm.github.io) might help. – jewelsea Sep 19 '19 at 18:34