Question

I have a deferred task to format body email in HTML doing an UrlFetch to my own application domain:

//Format body email in HTML
final Charset UTF8_CHARSET = Charset.forName("UTF-8");
final String FORMAT_EMAIL = "http://www." + NamespaceManager.getGoogleAppsNamespace() + "/email/formatEmail";

URL url = new URL(FORMAT_EMAIL);
HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST, FetchOptions.Builder.withDeadline(60.0));
req.setPayload(Utils.getPostData(map, "UTF-8").getBytes());
HTTPResponse resp = urlFetchService.fetch(req);
if (resp.getResponseCode() == HttpURLConnection.HTTP_OK)
    bodyMessage = new String(resp.getContent(), UTF8_CHARSET);
//here create new task to send email
SendEmail sendEmail = new SendEmail(emailTO, nameTO, emailREPLYTO, nameREPLYTO, subject, bodyMessage);
queue.add(TaskOptions.Builder.withPayload(sendEmail));

Where I pass some params to servlet formatEmail to make a lovely HTML email.. :) This code works fine, create a new push task, and sends email successfully.

The problem is this task always, 100% of times, create a new instance, even without any user access, to make this arduous task and the instance shutdown at the end of code.

How to avoid this?

If I make an UrlFetch to external domain instead of my own domain, usually the GAE does not create a new instance for that.... not strange?

Was it helpful?

Solution

The Java runtime is not multithreaded, so each instance is only able to handle one request at a time. When your application makes a request to itself, there will generally not be a free instance available to service the new request--the caller is already occupying that space.

You could use warmup requests and the idle instances setting to ensure that there is an extra instance around to handle your formatting request, but it seems that you could call the formatting code directly in the original request instead of making the HTTP request to yourself.

UPDATE: As Nick points out, this is only true by default. Have you set <threadsafe>true</threadsafe>?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top