Monday, February 27, 2012

App Engine crone jobs not working on cloud sql database in java

        I am trying to create a crone job using scheduler of App Engine. My aim is to create a job that calls a servlet which insert values to the the table in cloud sql.I have created a crone job for update database on each minute.The admin console also shows the status of cron job as "on time success". But the values are not inserting into the table.TestServlet.java is the servlet class used to update database. My instance is testdb and database is db. TestServlet.java     
      package com.my;
        import java.io.IOException;
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.PreparedStatement;
        
        import javax.servlet.http.*;
        
        import com.google.appengine.api.rdbms.AppEngineDriver;
        
        @SuppressWarnings("serial")
        public class TestServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Connection c = null;
           try {
             DriverManager.registerDriver(new AppEngineDriver());
             c = DriverManager.getConnection("jdbc:google:rdbms://testdb/db");
             String fname = "Manu";
             String content = "Hai";
             if (fname == "" || content == "") {
              // out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");
             } else {
             String statement ="INSERT INTO entries (guestName, content) VALUES( ? , ? )";
             PreparedStatement stmt = c.prepareStatement(statement);
             stmt.setString(1, fname);
             stmt.setString(2, content);
             int success = 2;
             success = stmt.executeUpdate();
             }
           }catch(Exception ex){
             
             }
        }
        }
            
            Cron.xml 's url is defined in the web.xml as url-mapping tag as /test. It is placed in the WEB-INF folder along with app-engine.xml
            cron.xml
       
    
         <?xml version="1.0" encoding="UTF-8"?>
            <cronentries>
            <cron>
        <url>/test</url>
        <schedule>every 1 minutes</schedule>
            </cron>
        </cronentries>

No comments:

Post a Comment