java @Schedule job will be restarted after NPEX in the body of the task

StackOverflow https://stackoverflow.com/questions/23648578

  •  22-07-2023
  •  | 
  •  

Pergunta

i have an

@Singleton
public class RecurringEventBackgroundJob {
    ...
    @Schedule(hour = "3")
    public void execute(Timer timer) {
        for (Data currentData : datalist) {
            ...
            amethodwhichproducearuntimeexception();
            ...
        }
    }
    private amethodwhichproducearuntimeexception() {
        ...
        nullpointer_exception eventually happens here
    }

}

scheduled task. because of a bug i got NPEX in the private method. Rollback was done. is OK. BUT! the schedule is restarted once again. (and got NPEX again) and after trying to do the task twice, doesn't tried more.

The question is: why the system tried to run second time the task? Is it possible to prevent this behavior?

thanks Peter

Foi útil?

Solução

In the EJB 3.2 spec you can read the following

13.4.3 Timer Expiration and Timeout Callback Method

If container-managed transaction demarcation is used and the REQUIRED or REQUIRES_NEW transaction attribute is specified or defaulted (Required or RequiresNew if the deployment descriptor is used), the container must begin a new transaction prior to invoking the timeout callback method. If the transaction fails or is rolled back, the container must retry the timeout at least once.

emphasis (mine) on the bold part.

glassfish has an option to configure the number of retries as described in this question: avoid-expunging-timer-on-glassfish. Not sure if similiar exists on jboss/wildfly

The answer to same question gives a nice way to avoid the problem all together by placing the @Schedule annotated method in a separate ejb.

Outras dicas

You can just add to execute method try/catch statement. Also remember, that since you have not add

persistent=false

property to your Schedule annotation, the timer service will survive your server restart.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top