gonzalo diethelm wrote:
> I am trying to figure out the proper way to use 0MQ from a servlet. If I
> am not mistaken, when a servlet receives an HTTP request, what is really
> happening is that this servlet runs on a separate thread, allowing the
> Web container to use many such threads in parallel to serve content to
> many simultaneous HTTP requests. I would like to have this servlet
> thread send out a message using 0MQ. In order to do this, I think I
> would have to initialize / terminate 0MQ from each servlet thread, every
> time I receive an HTTP request, incurring a cost that might be too high;
> is this correct? Or is there a place where I could initialize 0MQ just
> once, for all threads that will use it? (My apologies, I know this last
> question is not strictly related to 0MQ.)

I have little idea of what a servlet is, however, spawning a new thread 
for each HTTP request doesn't seem to be very efficient. (Others may 
have more experience with the topic though.)

As for initialising 0MQ just once the best place to do so it at the 
beginning of main() and storing it in a global variable.

> Now, say there IS a place to initialize 0MQ just once. If I do that, I
> would have to make a guess as to the number of concurrent threads (HTTP
> requests) that I would be using it, right? Say, 100 simultaneous HTTP
> requests. What happens if I initialize 0MQ with that assumption but
> then, during runtime, end up going above that?

It returns EMTHREAD error when you try to create a socket.

> Finally, say I have five concurrent threads serving five simultaneous
> HTTP requests. Each of these threads will want to send out a message
> over 0MQ, and then wait for a response. It seems to me the only safe way
> of doing this is using REQ/REP sockets, but if all these threads send a
> message to the same process (the one that will generate the answers),
> will I be assured that the answers will come back to the appropriate
> thread? Have replies any "affinity" to the underlying socket that
> actually sent the corresponding request?

Yes, they do.

> Is this a pattern (let's call it "multi-threaded web server") that is
> well understood? Has anybody implemented something similar? If yes, can
> you share some thoughts about it? Or maybe this is a mismatch for 0MQ's
> architecture?

When confronted with "multi-threaded server" I would imagine a server 
application with a shared queue and N worker threads connected to the 
queue using inproc transport. M clients would be connected to the shared 
queue via TCP transport. But it seems that you have something different 
in mind.

Martin
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to