Search code examples
javajakarta-eeapache-tomee

@Schedule starts twice


this program starts every day at 4 am, but every day the schedule is started twice, I have already checked any cycle but everything is in order. why does it always start twice? how can i solve?

import javax.ejb.LocalBean;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class RichiamiSync {
    @Schedule (minute = "00", hour = "04")

    public  void main() {

        try {
            Logica.esegui();
            }
            catch (Exception e) {
                Log.write("ERROR  " + e);
                Log.write("---------------------ERRORE FATALE IL PROGRAMMA E' STATO ARRESTATO-------------------------");   
                }
        Ottimizza.sql();

        Log.sendEmail();


    }




}

initially the methods were noted like this:

@Singleton
@LocalBean
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

but in another question they said that with stateless it would be solved, now the problem is always the same


Solution

  • this is the solution:

    @Singleton
    @LocalBean
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public class ServerStartStop {
    
        private ExecutorService es2 = Executors.newSingleThreadExecutor();
        private Future<Void> operazioniAggiornamentoUI =null;
        
        
        @Schedule(hour = "*" ,minute = "*",second = "*/10", persistent = false)
        public void AggiornamentoUI(Timer timerInfo) {
            
            if (operazioniAggiornamentoUI!=null)
                if(!operazioniAggiornamentoUI.isDone())
                    return;
            
            
            Callable<Void> callableObj = () -> { try {
    
                GestoreAcquisizioneOrdiniUI.aggiornaUiAcquisizioneOrdini();
                
                
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
            };
            
            
            
            operazioniAggiornamentoUI=es2.submit(callableObj);
            
            
            
            
        }}