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