2017-05-15 11 views
1

SkipListenerから取得しようとしています。SkipListenerからExecutionContextを取得する際の問題

import com.xxxx.domain.UserAccount; 
import lombok.extern.slf4j.Slf4j; 
import org.springframework.batch.core.StepExecution; 
import org.springframework.batch.core.annotation.BeforeStep; 
import org.springframework.batch.core.annotation.OnSkipInWrite; 
import org.springframework.mail.MailSendException; 
import org.springframework.stereotype.Component; 

@Slf4j 
@Component 
public class MailSkipListener { 

    private StepExecution stepExecution; 

    @BeforeStep 
    public void saveStepExecution(StepExecution stepExecution) { 
     this.stepExecution = stepExecution; 
    } 

    @OnSkipInWrite 
    public void logSkippedEmail(UserAccount userAccount, Throwable t) { 
     if (t instanceof MailSendException) { 
      MailSendException e = (MailSendException) t; 
      log.warn("FailedMessages: " + e.getFailedMessages()); 
     } 
    } 
} 

MailSendExceptionが発生したときただし、logSkippedEmailメソッドが実行されることはありません。ここで

は、私は(私は私のリスナーを実装するために、注釈の代わりに、インターフェースに依存している)を試みてきたものです。 saveStepExecutionメソッドを削除すると、MailSendExceptionの場合にはlogSkippedEmailが再度実行されます。

私は次のように私のMailSkipListenerを登録します。

@Bean 
public Step messagesDigestMailingStep(EntityManagerFactory entityManagerFactory) { 
    return stepBuilderFactory 
      .get("messagesDigestMailingStep") 
      .<UserAccount, UserAccount>chunk(5) 

      ... 

      .writer(itemWriter) 
      .listener(mailSkipListener)//Here 
      .build(); 
} 

を私はここで達成しようとしている何が私のSkipListenerからExecutionContextを取得しています。これはどのように達成できますか? ExecutionContextをオートワイヤリングする方法がないようです。

答えて

0

あなたはbeforeStep()方法の間に、あなたのstepExecutionにコンテキストを保存するためにあなたのMailSkipListenerStepExecutionListenerを実装することができます

public class MailSkipListener implements StepExecutionListener { 

    @Override 
    public void beforeStep(StepExecution stepExecution) { 
     this.stepExecution = stepExecution; 
    } 
+0

こんにちは。これは私が上記で試したことですが、インターフェースの代わりに注釈を使用しています – balteo

0

は、これはかなり古い質問ですが、私はあまりにもこれに苦労しました。 Skiplistenerを登録するために、StepExecutionListenerとして、SkipListenerとしてもう一度登録しました。 それはうまくいくが、うまくいくようだ。

@Bean 
public Step messagesDigestMailingStep(EntityManagerFactory entityManagerFactory) { 
    return stepBuilderFactory 
     .get("messagesDigestMailingStep") 
     .<UserAccount, UserAccount>chunk(5) 

     ... 

     .writer(itemWriter) 
     .listener((StepExecutionListener) mailSkipListener) // <--- 1 
     .listener((SkipListener) mailSkipListener)   // <--- 2 
     .build(); 
} 
関連する問題