0

I have a spring app which listens on rabbitmq and processing the message, when the app runs, I will register on a hub database to note that one instance is running, during the registering if another instance is already running, then I need to quit app without init the rabbit connection/queue and listener, otherwise some message could be wrongly consumed. How to do that, I know there are callback when init a bean. So I should create bean and before init it, checking whether another instance is running, but how to make sure this bean will be init before other beans? But I also need to init database source bean before the checking bean, otherwise it can't use the database defined in configuration.

 <rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" /> <rabbit:admin id="containerAdmin" connection-factory="connectionFactory" /> <rabbit:queue id="ETLQueue" name="ETLQueue" /> <rabbit:direct-exchange id="myExchange" name="ETL"> <rabbit:bindings> <!--if doens't specifiy key here, the by default the key will be the same as queue name, and then need to send message with the correct key, otherwise the listener can't receive the mssage--> <rabbit:binding queue="ETLQueue" key="ETLQueue" /> </rabbit:bindings> </rabbit:direct-exchange> <bean id="aListener" class="com.testcom.amqp.listener.ConnectorListener" c:dataSource-ref="dataSource"/> <!--concurrency will set how many threads running concurrently to consume the messsage, the code need to be thread safe--> <rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory" prefetch="1" concurrency="10"> <rabbit:listener ref="aListener" queues="ETLQueue" /> </rabbit:listener-container> 

1 Answer 1

1

As you are using xml configuration, the attribute depends-on normally helps to say that a bean should not be initialized before the target bean :

<bean id="A" .../> <bean id="B" ... depends-on="A"/> 

Bean B should be initialized after A (unless there are some cyclic dependency in which case spring only does its best ...)

Sign up to request clarification or add additional context in comments.

2 Comments

depends-on could resolve the issue that datasource bean be init before my checking bean. But how to let checking bean have the top priority to be init other than datasource? if use depends on, then I need to make ALL other beans depend on checking bean?
@DanielWu I do not think that it is necessary, it should be enough that rabbit initialization depends on checking bean

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.