<div dir="ltr">Real quick sample code from the Spring AMQP site:<br><br><pre><code class="">
<span class="">ConnectionFactory</span> <span class="">cf</span> <span class="">=</span> <span class="">new</span> <span class="">CachingConnectionFactory</span><span class="">();</span>
<span class="">// set up the queue, exchange, binding on the broker</span>
<span class="">RabbitAdmin</span> <span class="">admin</span> <span class="">=</span> <span class="">new</span> <span class="">RabbitAdmin</span><span class="">(</span><span class="">cf</span><span class="">);</span>
<span class="">Queue</span> <span class="">queue</span> <span class="">=</span> <span class="">new</span> <span class="">Queue</span><span class="">(</span><span class="">"myQueue"</span><span class="">);</span>
<span class="">admin</span><span class="">.</span><span class="">declareQueue</span><span class="">(</span><span class="">queue</span><span class="">);</span>
<span class="">TopicExchange</span> <span class="">exchange</span> <span class="">=</span> <span class="">new</span> <span class="">TopicExchange</span><span class="">(</span><span class="">"myExchange"</span><span class="">);</span>
<span class="">admin</span><span class="">.</span><span class="">declareExchange</span><span class="">(</span><span class="">exchange</span><span class="">);</span>
<span class="">admin</span><span class="">.</span><span class="">declareBinding</span><span class="">(</span>
<span class="">BindingBuilder</span><span class="">.</span><span class="">bind</span><span class="">(</span><span class="">queue</span><span class="">).</span><span class="">to</span><span class="">(</span><span class="">exchange</span><span class="">).</span><span class="">with</span><span class="">(</span><span class="">"foo.*"</span><span class="">));</span>
<span class="">// set up the listener and container</span>
<span class="">SimpleMessageListenerContainer</span> <span class="">container</span> <span class="">=</span>
<span class="">new</span> <span class="">SimpleMessageListenerContainer</span><span class="">(</span><span class="">cf</span><span class="">);</span>
<span class="">Object</span> <span class="">listener</span> <span class="">=</span> <span class="">new</span> <span class="">Object</span><span class="">()</span> <span class="">{</span>
<span class="">public</span> <span class="">void</span> <span class="">handleMessage</span><span class="">(</span><span class="">String</span> <span class="">foo</span><span class="">)</span> <span class="">{</span>
<span class="">System</span><span class="">.</span><span class="">out</span><span class="">.</span><span class="">println</span><span class="">(</span><span class="">foo</span><span class="">);</span>
<span class="">}</span>
<span class="">};</span>
<span class="">MessageListenerAdapter</span> <span class="">adapter</span> <span class="">=</span> <span class="">new</span> <span class="">MessageListenerAdapter</span><span class="">(</span><span class="">listener</span><span class="">);</span>
<span class="">container</span><span class="">.</span><span class="">setMessageListener</span><span class="">(</span><span class="">adapter</span><span class="">);</span>
<span class="">container</span><span class="">.</span><span class="">setQueueNames</span><span class="">(</span><span class="">"myQueue"</span><span class="">);</span>
<span class="">container</span><span class="">.</span><span class="">start</span><span class="">();</span>
<span class="">// send something</span>
<span class="">RabbitTemplate</span> <span class="">template</span> <span class="">=</span> <span class="">new</span> <span class="">RabbitTemplate</span><span class="">(</span><span class="">cf</span><span class="">);</span>
<span class="">template</span><span class="">.</span><span class="">convertAndSend</span><span class="">(</span><span class="">"myExchange"</span><span class="">,</span> <span class="">"foo.bar"</span><span class="">,</span> <span class="">"Hello, world!"</span><span class="">);</span>
<span class="">Thread</span><span class="">.</span><span class="">sleep</span><span class="">(</span><span class="">1000</span><span class="">);</span>
<span class="">container</span><span class="">.</span><span class="">stop</span><span class="">();<br><br><br></span></code></pre><pre><code class=""><span class="">ON a war, you add shutdown hooks to the web.xml file. In a non-web environment, there are shutdown hooks through the Runtime:<br>
<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html">http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html</a><br><br>In the end, they all call container.shutdown:<br><a href="http://docs.spring.io/spring-amqp/docs/1.2.1.RELEASE/api/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainer.html">http://docs.spring.io/spring-amqp/docs/1.2.1.RELEASE/api/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainer.html</a><br>
<br></span></code></pre><pre><code class=""><span class="">Code for that I found here:<br><a href="http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.amqp/spring-rabbit/1.0.0.RELEASE/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainer.java">http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.amqp/spring-rabbit/1.0.0.RELEASE/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainer.java</a><br>
<br></span></code></pre><pre><code class=""><span class="">The spring libraries are fantastic for RabbitMQ work (though sometimes there are some headaches on "Which factory is this? Which MessageProperties do I use?" kinda stuff, but overall they're not bad and they're getting better. I tend to be very careful on how much abstraction I add to my systems (hence why Groovy drives me nuts), but overall spring is has been very clean implementation wise.<br>
</span></code></pre><pre><code class=""><span class="">Jason<br></span></code></pre><br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Apr 2, 2014 at 3:17 PM, Bertrand Guay-Paquet <span dir="ltr"><<a href="mailto:bernie@step.polymtl.ca" target="_blank">bernie@step.polymtl.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
Hi Jason,<br>
<br>
Thanks for the hint, but I'm unfortunately not using Spring for
this. Do you know how Spring achieves this "shutdown" command? I
don't know Spring at all but I'll have a look at their doc.<br>
<br>
Regards,<br>
Bertrand<div><div class="h5"><br>
<br>
<div>On 02/04/2014 4:10 PM, Jason McIntosh
wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div>We use consumers here with the spring libraries. If you
use them, there's a "Shutdown" command you can call as
needed. We've had pretty good luck with it so far. The
spring consumer stuff manages how many consumers are running,
connection threading, etc. - and it has very good shutdown
hooks.<br>
</div>
Jason<br>
</div>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Wed, Apr 2, 2014 at 3:02 PM,
Bertrand Guay-Paquet <span dir="ltr"><<a href="mailto:bernie@step.polymtl.ca" target="_blank">bernie@step.polymtl.ca</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I'm using the RabbitMQ Java client API and need some
guidance on the proper application shutdown procedure.<br>
<br>
Here's how I start the application:<br>
1-Create a Connection<br>
2-Create different types of consumers, each with its own
channel, and call channel.basicConsume("queue", false,
consumer)<br>
3-Let it all run<br>
<br>
This works great, but I can't figure out how to cleanly
shutdown the application. If I simply close the Connection,
each created channel immediately (or soon enough) becomes
invalid and any Consumer currently doing some work fails
when trying to ack their current message or perform any
other action on the channel. I'd like to let the consumers
finish whatever message they're processing and then close
everything down. I guess I need to keep track of the created
Consumers and somehow signal them to stop accepting new
messages and after they're all done with their current job,
close the connection? Is that possible or is there another
way? I haven't found any management methods for the consumer
classes to control or query their status.<br>
<br>
The information I found so far is related to manually
created threads that poll the queues to process messages. In
that case, it's really easy because I can just set a flag on
each runnable to exit after processing their current message
and join on all the threads before closing the underlying
connection. So this leads me to ask, as a side note: are
Consumers the way to go to use RabbitMQ in real-world
scenarios or should I poll on the queues? It seems to me
that Consumers would be the better choice (polling is bad),
but if they're less powerful, perhaps they're not a silver
bullet in my case.<br>
<br>
Thank you,<br>
Bertrand<br>
_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com" target="_blank">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
</blockquote>
</div>
<br>
<br clear="all">
<br>
-- <br>
<div dir="ltr">Jason McIntosh<br>
<a href="https://github.com/jasonmcintosh/" target="_blank">https://github.com/jasonmcintosh/</a><br>
<a href="tel:573-424-7612" value="+15734247612" target="_blank">573-424-7612</a></div>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
rabbitmq-discuss mailing list
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com" target="_blank">rabbitmq-discuss@lists.rabbitmq.com</a>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a>
</pre>
</blockquote>
<br>
</div></div></div>
<br>_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr">Jason McIntosh<br><a href="https://github.com/jasonmcintosh/" target="_blank">https://github.com/jasonmcintosh/</a><br>573-424-7612</div>
</div>