1

我正在使用 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" />
4

1 回答 1

2

除非使用 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 功能。

于 2014-04-03T19:40:22.230 回答