On Tue, Oct 4, 2011 at 7:15 PM, Mark Silberbauer <bowserm...@gmail.com>wrote:

> I would like to write a method which handles the flow of communication on
> XMPP.  The sequence of things I'd like to do is:
>
> 1. Send message.
> 2. Wait for response.
> 3. Process the response.
>
> Since we could be waiting longer than 30s for the response (step 2) I'll be
> teeing up a task to take care of this. This task will need to send the
> message and then wait for a response on the XMPP servlet handling the
> incoming message. My question is: How do I wait in the task servlet thread
> for the response to arrive in the XMPP Servlet?
>

You can't. Instead, you should use an asynchronous pattern: Send the
message, and register a handler for incoming XMPP messages. That handler
should match up the response to the corresponding request (stored in the
datastore if necessary) and perform appropriate processing on it.


>
> I'd normally use a listener pattern where the listener would store the
> message in a field in the Task object and then trigger a Semaphore to signal
> that a message has arrived. Like this:
> 1. Install listener in XMPP servlet in a static field.
> 2. Send message.
> 3. Wait for semaphore.  ........ Meanwhile, in the XMPP servlet thread, a
> response will arrive and it will call the listener's callback method which
> stores the message and releases the semaphore.
> 4. Get message from field and process.
>
> I tried this and it worked fine on the development server. However, when I
> uploaded to the cloud I found that I'd install the listener on the XMPP
> servlet (step 1) but then a new instance of the servlet would be
> instantiated when the message came in and there would no longer be a
> reference to the listener to call, event through the listener is a static
> field. My conclusion is XMPPServlet is run in a completely different VM
> meaning the static field is not shared between that servlet and the task
> one. Is this correct?
>

An App Engine app can be run on any number of machines; synchronization
primitives designed for communication between threads will not work.

-Nick Johnson


>
> In general what is the best practice for communication between these
> servlets? How to I share data (normally I would've stored it in an object's
> field) and how do I signal from one to the other when events occur (normally
> I would've used a semaphore)?
>
> Sorry about the long winded question. Tell me if it's not clear and I'll
> refine it a bit.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-appengine-java/-/Ek_Xr5Etj20J.
> To post to this group, send email to
> google-appengine-java@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>



-- 
Nick Johnson, Developer Programs Engineer, App Engine

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to