1
votes

I want to set up a cron job that sends an email every 2 minutes. However, when I initiate the cron job, it sends an email right away and then never again. But, when I go to the Google Cloud Console and look at my cron jobs, it says it's running successfully, but I'm not receiving emails.

I followed this tutorial: https://rominirani.com/episode-9-using-the-cron-service-to-run-scheduled-tasks-8bc7dba91a77

web.xml file:

<servlet>
    <servlet-name>subscribe</servlet-name>
    <servlet-class>blogapp.CronServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>subscribe</servlet-name>
    <url-pattern>/subscribe</url-pattern>
</servlet-mapping>

cron.xml file:

<cronentries>
  <cron>
    <url>/subscribe</url>
    <description>Daily Digest from The Rambling Programmer</description>
    <!-- <schedule>every day 17:00</schedule> -->
    <schedule>every 2 minutes</schedule>
    <timezone>America/Chicago</timezone>
  </cron>
</cronentries>

CronServlet.java file:

public class CronServlet extends HttpServlet {

private static final Logger _logger = Logger.getLogger(CronServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
    try {
    _logger.info("Cron Job has been executed");
    /// other logic to send email
    /// sendEmail(email, subject, content);
    }

    resp.sendRedirect("/subscribe.jsp");
    }
    catch (Exception ex) {
        resp.getWriter().println("Error subscribing");
    }
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);

}

There's no errors popping up and one email successfully sends so I'm not sure why it's not running every two minutes like I wanted it to.

Thank you!

1

1 Answers

1
votes

I found out it's because it wasn't calling sendEmail() for some reason!