• No se han encontrado resultados

3.3 El Sanatorio Durán

3.3.1 Datos generales del sitio

To summarize the above steps in terms of what we have to code, here is the list:

1. Code the GAEJCreateTaskServlet that will accept the request parameter and create a Task instance in the subscription-queue.

2. Code the GAEJSignupSubscriberServlet that will be invoked by Google App Engine automatically. We will currently only print out a log statement because the intent is to demonstrate the whole sequence.

3. Configure our queue (subscription-queue) in a file named queue.xml. This file needs to be placed in the WEB-INF folder of your application.

Episode 10: Using the Task Queue Service 108

4. Configure our GAEJCreateTaskServlet and GAEJSignupSubscriberServlet in the

web.xml file.

Finally, we can execute our application and use the local development server to see the application in action. Users can optionally even deploy it to the Google App Engine cloud if they wish.

So let us get started.

GAEJCreateTaskServlet.java

This servlet accepts our request for subscription. We shall invoke it via the following url :

http://appurl/gaejcreatetask?emailid=XYZ. packagecom.gaejexperiments.taskqueue; importjava.io.IOException; importjavax.servlet.ServletException; importjavax.servlet.http.*; importcom.google.appengine.api.labs.taskqueue.Queue; importcom.google.appengine.api.labs.taskqueue.QueueFactory; importcom.google.appengine.api.labs.taskqueue.TaskOptions; @SuppressWarnings("serial")

publicclassGAEJCreateTaskServlet extendsHttpServlet {

publicvoiddoGet(HttpServletRequest req, HttpServletResponse resp) throwsIOException {

String strCallResult = "";

resp.setContentType("text/plain"); try{

//Extract out the To, Subject and Body of the Email to be sent String strEmailId = req.getParameter("emailid");

//Do validations here. Only basic ones i.e. cannot be null/empty

if(strEmailId == null) thrownewException("Email Id field cannot be empty.");

//Trim the stuff

strEmailId = strEmailId.trim();

if(strEmailId.length() == 0) thrownewException("Email Id field cannot be empty.");

//Queue queue = QueueFactory.getDefaultQueue();

Queue queue = QueueFactory.getQueue("subscription-queue");

queue.add(TaskOptions.Builder.url("/gaejsignupsubscriber").param("emai lid",strEmailId));

strCallResult = "Successfully created a Task in the Queue"; resp.getWriter().println(strCallResult);

}

catch (Exception ex) {

strCallResult = "Fail: "+ ex.getMessage(); resp.getWriter().println(strCallResult); }

}

@Override

publicvoiddoPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

doGet(req, resp); }

}

The code listed below is straightforward to understand. It does the following:

1. Extracts out the request parameter (emailid) and does some basic validation on it. 2. It gets a handle to the subscription-queue through the following statement:

Queue queue = QueueFactory.getQueue("subscription-queue");

3. It adds a Task to the above queue by providing a Task URL (/gaejsignupsubscriber) and Data (emailid parameter). It uses a helper class TaskOptions.Builder to help create the instance of the task. As you can see it provides a url and then the param. The task is created by invoking the add method on the queue.

queue.add(TaskOptions.Builder.url("/gaejsignupsubscriber").param("email id",strEmailId));

4. For the readers information, I have shown a commented out line

//Queue queue = QueueFactory.getDefaultQueue();

which shows how to get the handle to the default queue in case you wish to place your

5. Do not that all the Task Queue classes are experimental and are present in the ture.

This servlet contains the core task logic. This will be invoked by Google App engine if it

t it got invoked and it also logs the email id.

tasks in the default queue itself.

com.google.appengine.api.labs.taskqueue package. This could change in the fu

GAEJSignupSubscriberServlet.java

finds any tasks present in the subscription-queue. If any tasks are there, it will pick them up and invoke the URL mentioned in the Task and pass to it the data present in the Task instance. The code shown below is straightforward, it simply logs a statement saying tha

Episode 10: Using the Task Queue Service 110 packagecom.gaejexperiments.taskqueue; importjava.io.IOException; importjava.util.logging.Logger; importjavax.servlet.ServletException; importjavax.servlet.http.*; @SuppressWarnings("serial")

publicclassGAEJSignupSubscriberServlet extendsHttpServlet { privatestaticfinalLogger _logger =

Logger.getLogger(GAEJSignupSubscriberServlet.class.getName()); publicvoiddoGet(HttpServletRequest req, HttpServletResponse resp) throwsIOException {

String strCallResult = "";

resp.setContentType("text/plain"); try{

String strEmailId = req.getParameter("emailid");

_logger.info("Got a Signup Subscriber Request for Email ID : "+ strEmailId);

//

// PUT YOUR TASK CODE HERE //

strCallResult = "SUCCESS: Subscriber Signup"; _logger.info(strCallResult);

resp.getWriter().println(strCallResult); }

catch (Exception ex) {

strCallResult = "FAIL: Subscriber Signup : "+ ex.getMessage(); _logger.info(strCallResult); resp.getWriter().println(strCallResult); } } @Override

publicvoiddoPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

doGet(req, resp); }

}

queue.xml

figured in a file named queue.xml. Google App Engine provides a s queue is aptly named “default“. But in case you need to define your own queues, which is what we are going to do, we need to define them in a file called All queues are con

default queue. Thi

queue.xml. This file is placed in the WEB-INF directory of your application. You can also override settings of the default queue by defining it in the file and providing your own values.

Take a look at the queue.xml shown below: <?xmlversion="1.0"encoding="UTF-8"?> <queue-entries> <queue> <name>default</name> <rate>5/s</rate> </queue> <queue> <name>subscription-queue</name> <rate>5/s</rate> </queue> </queue-entries>

In the above configur

on-queue

ation, you will find that we have defined our own queue named

. There is also another element that we have defined for the

te>. This element determines the rate at which you tell Google App Engine to execute tasks. If you do not specify a rate, then the default execution rate is 5

ads er

Please make sure that the above file (queue.xml) is present in the WEB-INF folder at the

<servlet-mapping/> entry to the web.xml file. This necessary fragment to be added

u can use your own namespace and servlet class. Just modify it accordingly if you do so. We are defining here both our “subscripti

<queue> called <ra

tasks per second. In the above file, we have provided the expression as “5/s”, which re as 5 per second. Other examples of <rate> expressions are 1000/d (One thousand p day), etc. I suggest to read up the documentation for more examples.

You will also find that we have defined the default queue and we can change the rate if we want. But I have left it as is.

time of deploying the application.

Configuring the Servlets (web.xml)

Documento similar