I have created a utility jar file called email-util.jar. The main purpose of the utility is to send email via Amazon SES or Gmail SMTP. I have used factory pattern to decide the email type. (email-util.jar will be used in multiple projects)
EmailService.java (Interface)
public Status sendEmail(Email emailVO)
Implementation for Amazon SES
public class AmazonSimpleEmailServiceImpl implements EmailService {
public Status sendEmail(Email emailVO)
{
Amazon related stuff
}
Implementation for Gmail
public class GmailServiceImpl implements EmailService {
public Status sendEmail(Email emailVO)
{
Google related stuff
}
EmailVO will have ToAddress, FromAddress….. All email related info resides in EmailVO. Using factory pattern I am able to create object of either AWS or Google and send email successfully.
Right now I have hardcoded all the configurations of Amazon SES and Google in their corresponding implementations. The configuration info of Amazon and Gmail is little different.
**Gmail **
- Smpt host
- Smtp port …
Amazon SES
- awsAccessKey
- awsSecretKey
- region
- connectionTimeout
- maxConnections
- socketTimeout
- maxErrorRetry ….
But I don’t want configuration info to be hardcoded. I introduced an additional parameter to sendEmail method.
public Status sendEmail(Email emailVO, EmailConfig config)
EmailConfig is a simple bean where I have the entire amazon and gmail related variables. Everything is working fine. But I have few concerns
Quesions
- Is there a way we can separate Amazon SES configurations and Gmail related configurations.
- One more issue is user doesn’t know which variables are mandatory and which variables are optional. I.e. if user opts for gmail then Region is optional. How to solve this problem
- Any design pattern which addresses this issue
I tried creating a marker interface called EmailConfig and created two classes which implement EmailConfig.
AmazonSESConfig implements EmailConfig amazonSES related variables.
GmailConfig implements EmailConfig gmail related variables.
But it didn’t work out. Not able to store object of AmazonSES /Gmail in EmailConfig since it is a marker interface.
Aucun commentaire:
Enregistrer un commentaire