1

私は Spring AMQP を使用して、RabbitMQ でキューを作成しています。アプリが実行されているマシンの名前が名前に含まれるキューが必要です。したがって、アプリを実行する場所に応じて、キュー名は「fooQueue.host1」または「fooQueue.host2」になります。

私はこれを行う方法を考え出しました (以下で詳しく説明します) が、少し複雑に思えます。これを達成するためのより簡単/より良い/春の方法はありますか?

私の解決策

まず、マシン名を取得する Bean を作成します。

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

次に、Bean を Spring 構成に登録します。

<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 に答える