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>