Hybris/ SAP commerce Cloud: Retry failed/not sent emails due to SMTP connection issue.

 

1. Add Configuration in Local.properties file.

email.retry.attempts = 5
#in milliseconds 300000 ms = 5 minutes
email.retry.interval = 300000

2. Add  "retryAttempts" attribute in EmailMessage item

<itemtype code="EmailMessage" generate="false" autocreate="false">
<attributes>
<attribute type="java.lang.Integer" qualifier="retryAttempts">
<persistence type="property"/>
<modifiers unique="false" optional="false" read="true" write="true" search="true"/>
<defaultvalue>0</defaultvalue>
</attribute>
</attributes>
</itemtype>

3. Extends OOB DefaultEmailService and override send(EmailMessageModel message) method

@Service("testDefaultEmailService")
public class TestDefaultEmailService extends DefaultEmailService {

   @Resource(name = "modelService")
   private ModelService modelService;

    private int  maxRetryAttempts;
    private int emailRetryDelay;

@PostConstruct
void init(){
maxRetryAttempts = configurationService.getConfiguration().getInt("email.retry.attempts");
emailRetryDelay = configurationService.getConfiguration().getInt("email.retry.interval");
}
@Override
public boolean send(EmailMessageModel message) throws RetryLaterException {
boolean sent = super.send(message);

if(!sent && message.getRetryAttempts() < this.maxRetryAttempts && message.getProcess() != null ) {
message.setRetryAttempts(message.getRetryAttempts() + 1);
modelService.save(message);
slf4jLogger.info("Email sending failed - for PK [" + message.getPk() + "] and Process [" + message.getProcess().getCode() + "] and its subject: " + message.getSubject() );
final RetryLaterException retryLaterException = new RetryLaterException();
retryLaterException.setDelay(this.emailRetryDelay); // in milliseconds
retryLaterException.setMethod(RetryLaterException.Method.LINEAR);
retryLaterException.setRollBack(false);
throw retryLaterException;
}
return sent;
}
}

OOB Hybris is configured in such a way that when we throw RetryLaterException
in an action (business process action) then that particular action will be processed/triggered again after specified interval of time.
in my case I have set retrigger delay as 5 minutes and I am retrying 5 times .

Comments

Popular posts from this blog

Hybris / SAP Commerce Cloud Groovy Scripting Job to Generate CSV/Excel Reports and copy to Commerce cloud Blob Storage

Emma's dream - a kids story - By Kavya