Cron Job Send Email Trong Google App Engine Java

Hôm nay mình giới thiệu đến các bạn cách gửi nhiều email trong Google App Engine(GAE).
Trong thực tế đôi khi chúng ta gặp phải yêu cầu chẳng hạn như:
Gửi đến 1 nội dung tới list danh sách email.Chúng ta không thể đưa chúng vào 1 vòng lặp thông thường được.
Bởi vì khi xử lý như vậy rất có thể khi xảy ra 1 lỗi nào đó như mạng bị gián đoạn sẽ khiến cho các xử lý gửi mail
tiếp theo sẽ bị lỗi.Cách giải quyết ra sao ?Chúng ta phải đưa nó vào 1 Queue và sử dụng Cron để thực thi xư lý
đã được cài đặt.Nếu có lỗi như mạng bị gián đoạn thì nó sẽ tiếp tục thực hiện khi mạng được kết nối trở lại.
Chúng ta hãy đi vào chi tiết code sample nhé !
Bước 1: Tạo file queue với file name queue.xml:



default
5/m


send-user-email
5/m


Bước 2: Tạo file queue với file name cron.xml:

  

  
    /cron/gaejsendusermail
    send report by email
    every 1 minutes
  


/cron/gaejsendusermail

Chính là đường dẫn của 1 Servlet đảm nhiệm việc thực thi xử lý.
Chúng ta sẽ xem code để xử lý ở sau.
Bước 3: Tạo 1 Servlet có tên GAEJCreateTaskSendUserMail.java đảm nhiệm việc khởi tạo 1 task như sau:

@SuppressWarnings("serial")
public class GAEJCreateTaskSendUserMail {
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws IOException, ServletException {
  
  String[] lstsendmail = {
         "email1@mail.com",
         "email2@mail.com",
         "email3@mail.com",
         "email4@mail.com",
         "email5@mail.com",
         "email6@mail.com"
        };
  
  if (lstsendmail != null) {
      // Send lst mail
   for (int i = 0; i < lstsendmail.length; i++) {
    Queue queue = QueueFactory.getQueue("send-user-email");
    queue.add(withUrl("/cron/gaejsendusermail")
      .param("fromUser", "fromuseremail@abc.com")
      .param("fromUserName", "fromUserName")
      .param("toUser", lstsendmail[i])
      .param("toUserName","toUserName"));
   }   
  }
 }

 public void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGet(req, resp);
 }
}

Queue queue = QueueFactory.getQueue("send-user-email")

Để gọi Queue và thêm cron job
Các param được add để đấy tới 1 Servlet thực thi việc gửi email.
Bước 4: Tạo 1 Servlet thực thi xử lý gửi email với tên SendUserMailServlet.java
Chúng ta sử dụng thư viện Google Mail để gửi.
public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException { 
  
  try {
   String toUser = req.getParameter("toUser");
   String toUserName = req.getParameter("toUserName");
   String fromUser = req.getParameter("fromUser");
   String fromUserName = req.getParameter("fromUserName");
   
   String dateTitle = "20150306";
   String subject = dateTitle + "_Report";
   String mailcontent = "This is report content";
   
   Properties props = new Properties();
   Session session = Session.getInstance(props);
   Message msg = new MimeMessage(session);
   
   if (toUser != null) {
    msg.addRecipient(
      Message.RecipientType.TO,
      new InternetAddress(toUser, MimeUtility.encodeText(
        toUserName, "utf-8", "B")));
    msg.addRecipient(
      Message.RecipientType.CC,
      new InternetAddress(fromUser, MimeUtility.encodeText(
        fromUserName, "utf-8", "B")));
    msg.setHeader("X-Priority", "1");
    msg.setSubject(MimeUtility.encodeText(subject, "utf-8", "B"));
    Transport.send(msg);
    
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
Bước 5: Khai báo 2 Servlet trong file web.xml

  SendUserMailServlet
  cronjobdemo.SendUserMailServlet
 
 
  GAEJCreateTaskSendUserMail
  cronjobdemo.GAEJCreateTaskSendUserMail
 


  GAEJCreateTaskSendUserMail
  /tasksenduseremail
 
 
 
 
  SendUserMailServlet
  /cron/gaejsendusermail
 

Link tham khảo cron job: Cron Jobs Gửi mail trong GAE: Google Mail Chúc thành công !
Previous
Next Post »