i am working on a Struts2 web application and currently have a task of sending automatic mail in CSV, EXCEL, PDF format. I started up with Timer api to send automatic mail (which can schedule tasks for future execution).
All formats are ready, But i'm getting error while sending mails as the HttpServletRequest object gives me null value when inside run () method.
HttpServletRequest request = ServletActionContext.getRequest();
I need to get the request object to get the language and session while sending mails. Here is my class (i am using google guice for injecting service)
public class MailTimerTask extends TimerTask{
private final Utility mails;
private final MailService mailsrv;
private MailTimerTaskBean taskbean = new MailTimerTaskBean();
@Inject
MailTimerTask(Utility mails, MailService mailsrv) {
this.mails= mails;
this.mailsrv= mailsrv;
}
public void run() {
System.out.println("Mails-- Start");
mailList();
System.out.println("Mails-- Finished");
}
public void mailList() {
ByteArrayOutputStream outputStream = null;
HttpServletRequest request = ServletActionContext.getRequest(); // ERROR request object
for (Customer customer : taskbean.getPdfBean().getListCustomers()) {
outputStream = crt.generateOutputStream(taskbean, customer.getIdCustomer());
if (outputStream != null) {
MailBean mailBean = setMailBean(request, customer, outputStream, taskbean.getSite());
mailsrv.sentMail(mailBean);
}
}
}
public MailTimerTaskBean getMailtimertaskbean() {
return taskbean;
}
public void setMailtimertaskbean(MailTimerTaskBean mailtimertaskbean) {
this.taskbean = mailtimertaskbean;
}
I have also tried with pass request object in a Bean (in this case taskbean), by doing that i get the request object but the session is null
Do not know why the request object is getting null ? using ServletActionContext
Also it would be helpful if anyone could suggest me other api's for sending automatic mails ?
thank you.