[rabbitmq-discuss] Exclusive Producers?

Lukasz M. lukasz.miroslaw at googlemail.com
Mon Feb 24 14:10:34 GMT 2014


@Alvaro, your solution seems to be quite interesting. I just wonder if 
there is a simpler one.

I have a similar problem where I 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() .

My first idea was to implement SmartLifeCycle interface from Spring and 
extend RabbitTemplate as follows.:

public class LifeSender extends RabbitTemplate implements SmartLifecycle {

private volatile boolean isRunning = false;

public boolean isAutoStartup() {
return true;

public void start() {
isRunning = true;

public void stop() {
isRunning = false;

Here is the Spring AMQP context:

*<!-- RabbitMQ Sender. --><bean id="sender" 
class="org.springframework.amqp.rabbit.core.Rabbit Template" 
abstract="true"><constructor-arg index="0" ref="connectionFactory" 
/><property name="confirmCallback" ref="premCallback" /><property 
name="exchange" value="myExchange" /></bean>*

<bean id="lifeSender" class="com.ucware.ucpo.cti.core.LifeSender" 

The problem is that lifeSender does not instatiate the properties and 
connection factory from the sender. So I created the constructor:

public LifeSender(ConnectionFactory connectionFactory, PremConfirmCallback 
premCallback) {
System.out.println(super.getConnectionFactory().to String());

Unfortunately, lifeSender.stop() does not do more than just printing 
"STOPPED" to the log. I am still able to send messages.

Second idea was to use controlChannel from Spring Integration. However, a 
simple test

public void testTurnOffSender() throws InterruptedException {
isBean = controlGateway.isRunning( "@sender.isRunning()" );

Assert.isTrue( isBean);
controlGateway.send(new GenericMessage<String>( "@sender.stop()" ));
isBean = controlGateway.isRunning( "@sender.isRunning()" );
Assert.isTrue( isBean == false);


triggers the exception:

*org.springframework.expression.EvaluationException : The method 
'isRunning' is not supported by this command processor. If using the 
Control Bus, consider adding @ManagedOperation or @ManagedAttribute.*

Do you think any of suggested solutions make sense?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20140224/14642dc4/attachment.html>

More information about the rabbitmq-discuss mailing list