簡體   English   中英

Spring AMQP:具有機器名稱的隊列

[英]Spring AMQP: Queue with machine name

我正在使用Spring AMQP在RabbitMQ中創建隊列。 我想要一個隊列,其名稱包括運行該應用程序的計算機的名稱。 因此,隊列名稱可能是“ fooQueue.host1”或“ fooQueue.host2”,具體取決於您運行應用程序的位置。

我想出了一種方法來做到這一點(下面詳細介紹),但是似乎有點復雜。 有沒有更簡便/更好/更先進的方法來實現這一目標?

我的解決方案

首先制作一個bean來獲取機器名稱:

public class MachineNamePropertyBean {
    public String GetMachineName() throws UnknownHostException {
        InetAddress localMachine = InetAddress.getLocalHost();
        return localMachine.getHostName();
    }
}

然后在您的Spring配置中注冊bean:

<bean id="machineNameBean" class="com.example.myapp.MachineNamePropertyBean" />

然后像下面這樣在Spring AMQP配置中使用它:

<rabbit:queue id="fooQueue"
              name="fooQueue.#{ machineNameBean.GetMachineName() }"
              durable="false"
              auto-delete="false"
              exclusive="false" />

除非使用SpEL,否則沒有其他解決方案:

<bean id="machineName" class="java.lang.String">
   <constructor-arg value="#{T(java.net.InetAddress).localHost.hostName}"/>
</bean>

<rabbit:queue id="fooQueue"
              name="fooQueue.#{ machineName }"
              durable="false"
              auto-delete="false"
              exclusive="false" />

與您所做的相同,但是沒有新類,並且沒有通過SpEL功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM