java - how to quit spring app during startup if certain condition meet? -
i have spring app listens on rabbitmq , processing message, when app runs, register on hub database note 1 instance running, during registering if instance running, need quit app without init rabbit connection/queue , listener, otherwise message wrongly consumed. how that, know there callback when init bean. should create bean , before init it, checking whether instance running, how make sure bean init before other beans? need init database source bean before checking bean, otherwise can't use 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, default key same queue name, , need send message correct key, otherwise listener can't receive 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 set how many threads running concurrently consume messsage, code need thread safe--> <rabbit:listener-container id="mylistenercontainer" connection-factory="connectionfactory" prefetch="1" concurrency="10"> <rabbit:listener ref="alistener" queues="etlqueue" /> </rabbit:listener-container>
as using xml configuration, attribute depends-on
helps bean should not initialized before target bean :
<bean id="a" .../> <bean id="b" ... depends-on="a"/>
bean b should initialized after (unless there cyclic dependency in case spring best ...)
Comments
Post a Comment