Problem with scheduling tasks with Calendar Java

0

Please help me resolve a code situation, as I can not.

I have a class that schedules tasks and it will be a Thread to call another Thread.

It reads information from a configuration xml file containing one or more days of the week that will be a list and will also have start time and end time of a task. Example xml:

<dias>dom,seg,ter,qua,qui,sex,sab</dias>
<horarioInicio>20:00</horarioInicio>
<horarioTermino>21:00</horarioTermino>

If the system's day is equal to a position in the list of xml days, it enters the condition e and makes the delay until the start time arrives and execute the task with the Thread until the end time that interrupts the Thread of service, but the problem I want to solve and I do not know how to do, which of these would be the best forms and what would be the code:

When the next week's xml parameter is found and is greater than the current day, you will have to delay the start time of the day from xml with the current system time.

2º Make a delay of the last time of the current day (23:59) with the current time: (18:00 for example) where the system day of the week will change and check if it is the same as the list, if yes enter condition, otherwise redo the check cycle until the last time of day again. How do I resolve this ... follow the code below:

private static final Logger LOGGER = LogManager
        .getLogger(AgendaExtracao.class.getName());

private static final int SECONDS = 60;
private static final int MILISECONDS = 1000;

private Produto produto = null;

public AgendaExtracao(Produto produto) {
    super();
    this.produto = produto;
    setName(produto.getNome());
}

@Override
public void run() {

    try {

        boolean dias = produto.getDias() != null;
        boolean periodo = produto.getHorarioInicio() != null;
        List<String> diaList = produto.getDias();

        if (dias && periodo) {

            Calendar inicio = Calendar.getInstance();
            inicio.setTime(produto.getHorarioInicio());
            Calendar fim = Calendar.getInstance();
            fim.setTime(produto.getHorarioTermino());

            preparaHora(inicio);
            preparaHora(fim);

            while (true) {

                long delay = 0;
                long workingTime = 0;

                try {
                    Calendar horaAtual = Calendar.getInstance();
                    preparaHora(horaAtual);

                    String datahoje = horaAtual.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, new Locale("pt", "BR"))
                            .replace('á', 'a').toLowerCase();

                    for (int i = 0; i < diaList.size(); i++) {

                        if (datahoje.equals(diaList.get(i))) {

                            if (inicio.after(fim) || inicio.equals(fim)) {
                                fim.add(Calendar.DAY_OF_MONTH, 1);
                            }

                            if (horaAtual.equals(fim)) {
                                inicio.add(Calendar.DAY_OF_MONTH, 1);
                                delay = inicio.getTimeInMillis() - fim.getTimeInMillis();
                                inicio.set(Calendar.DAY_OF_MONTH, 1);
                                workingTime = fim.getTimeInMillis()- inicio.getTimeInMillis();
                            }

                            else if (horaAtual.after(inicio) && horaAtual.before(fim)) {
                                workingTime = fim.getTimeInMillis() - horaAtual.getTimeInMillis();
                            }

                            else if (horaAtual.after(fim)) {
                                inicio.add(Calendar.DAY_OF_MONTH, 1);
                                delay = inicio.getTimeInMillis() - horaAtual.getTimeInMillis();
                                inicio.set(Calendar.DAY_OF_MONTH, 1);

                                workingTime = fim.getTimeInMillis() - inicio.getTimeInMillis();
                            }

                            else {
                                delay = inicio.getTimeInMillis() - horaAtual.getTimeInMillis();
                                workingTime = fim.getTimeInMillis() - inicio.getTimeInMillis();
                            }

                            LOGGER.info("DELAY: {}", delay);
                            LOGGER.info("Iniciando o manager em {} minutos",(delay / MILISECONDS / SECONDS));
                            LOGGER.info("WorkingTime: {}", workingTime);

                            Thread.sleep(delay);

                            Extracao extracao = new Extracao(produto);
                            extracao.start();
                            Thread.sleep(workingTime);

                            extracao.interrupt();

                        } else {

                            inicio.add(Calendar.DAY_OF_MONTH, 1);
                            delay = inicio.getTimeInMillis() - horaAtual.getTimeInMillis();
                            inicio.set(Calendar.DAY_OF_MONTH, 1);

                            System.out.println("Delay para o dia seguinte: " + mlsToSegundo(delay));
                            LOGGER.info("DELAY: {}", delay);
                            Thread.sleep(delay);
                        }
                    }

                } catch (InterruptedException e) {
                    LOGGER.error("Erro na interrupcao do Extrator");
                }
            }
        } else {
            Thread.interrupted();
        }
    } catch (Exception e) {
        LOGGER.error("Exception", e);
    } catch (Error e) {
        LOGGER.fatal("Error", e);
    }
}

private void preparaHora(Calendar hora) {
    int diaMes = Calendar.DAY_OF_MONTH;
    int mes = Calendar.DAY_OF_MONTH;
    int ano = Calendar.DAY_OF_MONTH;

    hora.set(Calendar.DAY_OF_MONTH, diaMes);
    hora.set(Calendar.MONTH, mes);
    hora.set(Calendar.YEAR, ano);
}

private String mlsToSegundo(long ms) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    c.add(Calendar.MILLISECOND, (int) ms);
    return new SimpleDateFormat("HH:mm").format(c.getTime());
}
    
asked by anonymous 03.03.2018 / 02:00

0 answers