RabbitMQ & Java Servlet life cycle.

236 views
Skip to first unread message

黄立文

unread,
Jul 22, 2015, 10:07:51 PM7/22/15
to rabbitmq-users
Dear,
      I hava meet a question about RabbitMQ with Servlet.
      I was confused about the life cycle of Servlet and RabbitMQ server.
      For Example:
           I open rabbitmq-server connection in init() ,and then send topic message in post() or get() method of servlet, finally I closed the connection in destroy() method. 
           is this way correct? I also find connection error when using servlet & rabbitmq in this way.
      Looking for your kind help.

      Best Wishes.

           

Michael Klishin

unread,
Jul 23, 2015, 3:48:32 AM7/23/15
to 黄立文, rabbitmq-users
Yes, this is how it's typically done. Additional channels or queues can be declared on the fly depending on your system's needs.

MK

michael...@gmx.net

unread,
Jul 23, 2015, 5:30:37 AM7/23/15
to rabbitmq-users, huangyado...@hotmail.com
Typically a servlet container only creates one instance of a servlet. Private variables used in the servlet are not thread-safe.
So you may experience errors when two requests are executed on the same time and both access the same connection object.

What exact error are you seeing?

(See http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading/3106909)

Michael Klishin

unread,
Jul 23, 2015, 5:58:51 AM7/23/15
to rabbitmq-users, michael...@gmx.net, huangyado...@hotmail.com
On 23 Jul 2015 at 12:30:42, michael...@gmx.net (michael...@gmx.net) wrote:
> Typically a servlet container only creates one instance of
> a servlet. Private variables used in the servlet are not thread-safe.
> So you may experience errors when two requests are executed on
> the same time and both access the same connection object.

Without seeing the stack trace, I wouldn’t speculate about what the issue may be.

Concurrency considerations of the Java client should be decently documented
on rabbitmq.com
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Reply all
Reply to author
Forward
0 new messages