Cron Job only executes once in Google App Engine for Java - java

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!

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

Related

Can't forward to jsp when handling an exception

So i got this problem, i do a servlet project using tomcat and I got this class that should handle the exception that was thrown and then display the jsp with error status code.
public class ErrorHandler extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(ErrorHandler.class);
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
if (throwable != null) {
LOGGER.fatal("Exception: " + throwable);
}
if (statusCode != null) {
LOGGER.fatal("Error, status code: " + statusCode);
}
req.getRequestDispatcher("error.jsp").forward(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
This handler is mapped in the web.xml this way.
<servlet-mapping>
<servlet-name>ErrorHandler</servlet-name>
<url-pattern>/error</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error</location>
</error-page>
The question is, why do i keep getting default tomcat exception page, instead of mine. My logger works just fine, so it's clear, that after exception was happened, method doGet is called, but when it comes to request dispatcher, it just doesn't work. My jsp pages are placed in webapp folder, near WEB-INF.
Because the request dispatcher for error.jsp is just a dispatcher. Java/tomat isn't going to realize that error.jsp means you intended for this to be the error handler. It's just a page, just like foo.jsp. So, that dispatcher picks up the call, so to speak, notices that the request involves an error state, and it, in turn, forwards the request to the error dispatcher.

request.getRequestDispatcher() called recursively

I've encountered a strange problem where my servlet doGet method will be run three times.
I tested this in Chrome and Firefox, and it's the same. I also deployed the project on another computer and used apache server to see if also does it when the site is live, but no change.
I use a switch in my doGet and a getParameter request to find the proper method to be run. I've shortened the switch to just two cases. But everytime the action is "create" It'll run the method three times.
I know this is very narrow, and I'd wish I were able to give you more information, but I do not know where to dig after it.
EDIT: The problem has been found. A request.getRequestDispatcher is causing it. Any help with this issue is very appreciated.
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
String action = request.getParameter("action");
// To see that it runs 3 times.
System.out.println(action);
switch (action) {
case "create":
this.create(request, response);
break;
default:
this.greetGuest(request, response);
break;
}
}
protected void createEvent(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("AbstractUser", SessionUser);
// Here's the problem. This part will cause the doGet to be called multiply times.
request.getRequestDispatcher("/WEB-INF/jsp/view/Organisation/create.jsp").forward(
request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
//html
Create
//Configuration
#WebServlet(name = "eventHandler", urlPatterns = { "/eventHandler" }, loadOnStartup = 1)
//web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspf</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>

error 500 on google app engine

I am trying to create a servlet on the Google App Engine. I have done this sucsessfully a few times in the past, but now I get an "Internal Server Error" when running it from the cloud.
It works on Eclipse on development mode, though.
My servlet is called AsyncServer and the code is:
public class AsyncServer extends HttpServlet {
static final long serialVersionUID=0L;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
PrintWriter writer = resp.getWriter();
writer.write("Hello");
writer.flush();
writer.close();
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
}
I use the following address to call it on the local development server:
http://127.0.0.1:8888/test4/AsyncServer
and I get "Hello", as expected.
When deployed to the Google servers under the ID fa100-1130
I use the following address
http://fa100-1130.appspot.com/test4/AsyncServer
I get an http error 500
Currently entering the URL mentioned in your question and returns an affirmative respesta:
http://fa100-1130.appspot.com/test4/AsyncServer
run the development and performs deploy again
bye :) #locoalien

HTTP method GET is not supported by this URL though it executes doGet [duplicate]

This question already has answers here:
HTTP Status 405 - HTTP method is not supported by this URL
(2 answers)
Closed 9 years ago.
public class RoarHistoryUpdate extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
super.doGet(request, response);
System.out.println("do Get");
response.setContentType("text/html");
response.getOutputStream().print("Success");
}
}
This is my Servlet. And it is registerd in the web.xml like this:
<servlet>
<display-name>RoarHistoryUpdateServlet</display-name>
<servlet-name>RoarHistoryUpdateServlet</servlet-name>
<servlet-class>de.ulm.uni.vs.avid.roary.servlets.RoarHistoryUpdate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RoarHistoryUpdateServlet</servlet-name>
<url-pattern>/Roary/UpdateServlet</url-pattern>
</servlet-mapping>
When I go to the URL http://localhost:8080/Roary-JSP/Roary/UpdateServlet It says HTTP Status 405 - HTTP method GET is not supported by this URL
Funny thing is I get the do Get logged to my console. So it actually found the doGet-method.
I am using a GlassFish Server Open Source Edition 3.1.2.2
Because when you do super.doGet(request, response); in your Servlet's doGet() method, you actually call the doget() of the HttpServlet class. The Tomcat 7 implementation of that method is as below (may be a similar implementation exists for Glassfish):
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
My guess is that it's because of invoking super.doGet(). If you check the source code of HttpServlet, you'll see that it sets this status code on the response. So drop the super call. It's not needed.

Appengine Channel Api presence _ah/channel/connected not working

I'm using the channel API in Java runtime. The servlet I have mapped to /_ah/channel/connected does not appear to be running. I am creating a channel, passing the token, and opening it on the server. This works fine. I do see the call to /_ah/channel/connected in my log, however no log messages appear and the code does not appear to be running. Below is my code and web.xml
ChannelConnectedServlet.java:
public class ChannelConnectedServlet extends HttpServlet{
private static final Logger logger = Logger.getLogger(ChannelConnectedServlet.class
.getName());
private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
logger.log(Level.WARNING,"test");
//do stuff here
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
logger.log(Level.WARNING,"Channel connected!");
process(req, resp);
}
}
web.xml:
<servlet-mapping>
<servlet-name>ChannelConnected</servlet-name>
<url-pattern>/_ah/channel/connected</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ChannelConnected</servlet-name>
<servlet-class>com.myapp.server.channel.ChannelConnectedServlet</servlet-class>
</servlet>
The same behavior happens with the disconnect request. HELP!!!
This entry in web.xml should have included "/" at the end of the url, such as:
<servlet-mapping>
<servlet-name>ChannelConnected</servlet-name>
<url-pattern>/_ah/channel/connected/</url-pattern>
Works now.

Categories