<div dir="ltr"><div>As I mentioned on the Spring Forum (<a href="http://forum.spring.io/forum/spring-projects/integration/amqp/745450-start-stop-rabbittemplate-on-demand">http://forum.spring.io/forum/spring-projects/integration/amqp/745450-start-stop-rabbittemplate-on-demand</a>)</div>
<div><br></div>You need to add isRunning() as a @ManagedOperation if you want to access it using the control bus.<div><br></div><div>You would also need to override the doSend*() methods on your subclass if you want to prevent sends on a "stopped" template.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Feb 24, 2014 at 9:10 AM, Lukasz M. <span dir="ltr"><<a href="mailto:lukasz.miroslaw@googlemail.com" target="_blank">lukasz.miroslaw@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>@Alvaro, your solution seems to be quite interesting. I just wonder if there is a simpler one.</div>
<div><br></div><div>I have a similar problem where I <span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">need to start/stop the RabbitTemplate object on demand. For example to restart the bean in my catch block with sender.stop() and then sender.start() .</span></div>
<div><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">My first idea was to implement SmartLifeCycle interface from Spring and extend RabbitTemplate as follows.:</span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><font face="courier new" style="color:rgb(37,44,47)">@Component<br>public class LifeSender extends RabbitTemplate implements SmartLifecycle {<br>
<br>private volatile boolean isRunning = false;<br><br>@Override<br>public boolean isAutoStartup() {<br>return true;<br>}<br><br>@Override<br>public void start() {<br>System.out.println("STARTED!!!");<br>isRunning = true;<br>
}<br><br>@Override<br>public void stop() {<br>System.out.println("STOPPED!!!");<br>isRunning = false;<br>}</font><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">Here is the Spring AMQP context:</span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><i style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><!-- RabbitMQ Sender. --><br><bean id="sender" class="org.springframework.amqp.rabbit.core.Rabbit Template" abstract="true"><br>
<constructor-arg index="0" ref="connectionFactory" /><br><property name="confirmCallback" ref="premCallback" /><br><property name="exchange" value="myExchange" /><br>
</bean></i><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><bean id="lifeSender" class="com.ucware.ucpo.cti.core.LifeSender" parent="sender"/></span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">The problem is that lifeSender does not instatiate the properties and connection factory from the sender. So I created the constructor:</span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><font face="courier new" style="color:rgb(37,44,47)">@Autowired<br>public LifeSender(ConnectionFactory connectionFactory, PremConfirmCallback premCallback) {<br>
super();<br>super.setConnectionFactory(connectionFactory);<br>super.setExchange("cti.main");<br>super.setConfirmCallback(premCallback);<br>System.out.println(super.getConnectionFactory().to String());<br>}</font><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">Unfortunately, lifeSender.stop() does not do more than just printing "STOPPED" to the log. I am still able to send messages.</span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">Second idea was to use controlChannel from Spring Integration. However, a simple test</span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><font face="courier new" style="color:rgb(37,44,47)">@Test<br>public void testTurnOffSender() throws InterruptedException {<br>isBean = controlGateway.isRunning( "@sender.isRunning()" );<br>
<br>Assert.isTrue( isBean);<br>controlGateway.send(new GenericMessage<String>( "@sender.stop()" ));<br>Thread.sleep(500);<br>isBean = controlGateway.isRunning( "@sender.isRunning()" );<br>Assert.isTrue( isBean == false);<br>
<br>}</font><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">triggers the exception:</span><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><i style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">org.springframework.expression.EvaluationException : The method 'isRunning' is not supported by this command processor. If using the Control Bus, consider adding @ManagedOperation or @ManagedAttribute.</i><br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">
<br style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">Do you think any of suggested solutions make sense?</span></div>
<div><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><br></span></div><div><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">Thanks,</span></div><div><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif">Lukasz</span></div>
<div><span style="color:rgb(37,44,47);font-family:Helvetica,Arial,Verdana,sans-serif"><br></span></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></div>