当我使用Spring AMQP部署2个软件包时,在以下代码中出现JMX错误:

    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
        connectionFactory.setBeanName("Test_123");
        return connectionFactory;
    }


我错误了Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.amqp.rabbit.connection:name=connectionFactory,type=CachingConnectionFactory

完整的错误堆栈:
https://pastebin.com/CdU3epMz

如何为connectionFactory设置唯一名称?

编辑:

我还尝试将application.properties放在此配置的src / main / java / resources下:

spring.jmx.enabled=false
spring.datasource.jmx-enabled=false
spring.jmx.default-domain=ssds # JMX domain name.
spring.jmx.server=apiServer # MBeanServer bean name.
management.metrics.export.jmx.domain=metccriddcs # Metrics JMX domain name.
management.metrics.export.jmx.enabled=false # Whether exporting of metrics to JMX is enabled.
management.endpoints.jmx.exposure.exclude=*


但是我得到了同样的错误。

最佳答案

解决方案:

... implements ObjectNamingStrategy {

    @Override
    public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
        Class managedClass = AopUtils.getTargetClass(managedBean);
        String domain = ClassUtils.getPackageName(managedClass);

        Hashtable<String, String> properties = new Hashtable<>();
        properties.put("type", ClassUtils.getShortName(managedClass));
        properties.put("name", "asdsdsd");
        // ensure the application name is included as a property in the object name
        properties.put("app", "api");
        return ObjectNameManager.getInstance(domain, properties);
    }

}

10-02 23:16