[rabbitmq-discuss] Exclusive Producers?
Lukasz M.
lukasz.miroslaw at googlemail.com
Mon Feb 24 14:10:34 GMT 2014
Hi,
@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.:
@Component
public class LifeSender extends RabbitTemplate implements SmartLifecycle {
private volatile boolean isRunning = false;
@Override
public boolean isAutoStartup() {
return true;
}
@Override
public void start() {
System.out.println("STARTED!!!");
isRunning = true;
}
@Override
public void stop() {
System.out.println("STOPPED!!!");
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"
parent="sender"/>
The problem is that lifeSender does not instatiate the properties and
connection factory from the sender. So I created the constructor:
@Autowired
public LifeSender(ConnectionFactory connectionFactory, PremConfirmCallback
premCallback) {
super();
super.setConnectionFactory(connectionFactory);
super.setExchange("cti.main");
super.setConfirmCallback(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
@Test
public void testTurnOffSender() throws InterruptedException {
isBean = controlGateway.isRunning( "@sender.isRunning()" );
Assert.isTrue( isBean);
controlGateway.send(new GenericMessage<String>( "@sender.stop()" ));
Thread.sleep(500);
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?
Thanks,
Lukasz
-------------- 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