繁体   English   中英

如果满足某些条件,如何在启动过程中退出spring app?

[英]how to quit spring app during startup if certain condition meet?

我有一个spring应用程序,该应用程序侦听Rabbitmq并处理消息,当该应用程序运行时,我将在中心数据库上注册以注意一个实例正在运行,在注册过程中如果另一个实例已在运行,那么我需要退出该应用程序如果没有初始化Rabbit连接/队列和侦听器,则可能会错误地使用某些消息。 怎么做,我知道初始化一个bean时有回调。 因此,我应该先创建bean,然后再对其进行初始化,检查是否正在运行另一个实例,但是如何确保此bean在其他bean之前将被init? 但是我还需要在检查bean之前初始化数据库源bean,否则它不能使用配置中定义的数据库。

 <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>

当您使用xml配置时,属性depends-on通常有助于说不应在目标bean之前初始化bean:

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

Bean B应该在A之后初始化(除非存在一些循环依赖性,在这种情况下spring只能发挥最大作用...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM