Announcement Announcement Module
Collapse
No announcement yet.
Managing exception in Tasklet Step Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Managing exception in Tasklet Step

    Hello everybody.

    I'm writing a step which implements the Tasklet Interface and I would like to manage any exception or error would could possible happen in it.

    Here is the source of my problem. In the execute() method of my tasklet step I'm doing this kind of things.
    Code:
    try{
       //Call to an ejb service
    }
    catch(Exception e){
        //Call to my logger (or anything else)
    }
    The problem happen when my Ejb is throwing a RuntimeException. This kind of exception is needed so that the EJB container rollbacks the transaction.
    The EJB is driven by Spring Batch thanks to a REQUIRED transaction.
    I guess that Spring Batch detects the exception throught the JTA Transaction Manager and then, rollbacks the transaction.
    Then, even if my catch block is well executed. I've no way to do my log (either it is by log4j or calling a service) without getting a TransactionRollbakException.

    Then, I thought to use the methods offered by SpringBatch.
    In the previous version of Spring Batch, I think that, by implementing the StepExecutionListener Interface, it was possible to override a "onError()" method on order to manage any exception (for logging for example) during the process.
    Now, this method doesn't exist anymore, except in ReaderListener or WriterListener. But, as I don't have implemented these interface (because of the use of a TaskletStep) how can I do ?

    So, a little help would be very welcome !
    and just let me know if I'm not clear enough.

  • #2
    StepExecutionListener.afterStep() will be called after the step whether an error has occurred or not. Within this method you can check whether the step failed or succeeded:

    Code:
    public ExitStatus afterStep(StepExecution stepExecution) {
        if (StepExecution.getStatus() == BatchStatus.FAILED) {
            //do on-error logic...
        }
        return null;
    }

    Comment


    • #3
      Thanks for your answer.

      Yes, this is true. I've thought about doing it this way.
      But the problem is (and I guess it would be same with "onError()") that I will know that a error happened, but no way to know where it happened.

      For example, here is a process:
      Code:
      try{
         //Call the service_1
      }
      catch(Throwable e){
        //Call to service Log("Error in service 1")
      }
      
      try{
         //Call the service_2
      }
      catch(Throwable e){
        //Call to service Log("Error in service 2")
      }
      Do you know if there is way to "block" the transaction of Spring Batch, catching the exception, doing some process, forwarding the exception ?
      I've tried, this works for "normal" Exception but not for RuntimeException.
      I've put the transaction of log service as NOT_SUPPORTED so that it won't be concerned by SpringBatch Transaction but still doesn't work. But maybe this problem has nothing to do with Spring batch....

      Anyway, I'm going to try with "afterStep()". But if you have any idea, it will be very welcome.
      Thanks.

      Comment


      • #4
        Are you saying that your catch block is never executed when the ejb throws an exception?

        Comment


        • #5
          It sounds like what you want is RUIRES_NEW pragation. However, if even not supported isn't worked, there's probably an issue with how you've set up your transactions, which seems even more likely if your batch job is calling an EJB, which is using a JTA transaction.

          Comment


          • #6
            No, catch block is working. But there is no way to call a service or a database in it without having a TransactionRolledBackException.
            Moreover, even if I don't forward the exception, Spring Batch manage to trace the exception. Just like if I did not put a catch block. Maybe spring Batch got the information direct from the JTA transaction manager ?

            Anyway...I manage to do almost waht I wanted. Using afterStep() and using my exception kept in a member variable (i did not find better solution)

            Comment


            • #7
              Originally posted by Zico View Post
              No, catch block is working. But there is no way to call a service or a database in it without having a TransactionRolledBackException.
              Generally if a call to a service fails, you DO want the transaction to rollback since something went wrong and you need to restore consistent state.

              Comment

              Working...
              X