[rabbitmq-discuss] Exclusive Producers?

Gary Russell grussell at gopivotal.com
Mon Feb 24 17:12:08 GMT 2014


As I mentioned on the Spring Forum (
http://forum.spring.io/forum/spring-projects/integration/amqp/745450-start-stop-rabbittemplate-on-demand
)

You need to add isRunning() as a @ManagedOperation if you want to access it
using the control bus.

You would also need to override the doSend*() methods on your subclass if
you want to prevent sends on a "stopped" template.


On Mon, Feb 24, 2014 at 9:10 AM, Lukasz M.
<lukasz.miroslaw at googlemail.com>wrote:

> 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
>
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20140224/cfc37b6a/attachment.html>


More information about the rabbitmq-discuss mailing list