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