0

When I deploy 2 packages with Spring AMQP I get JMX error in the below code:

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

I error Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.amqp.rabbit.connection:name=connectionFactory,type=CachingConnectionFactory

Full error stack: https://pastebin.com/CdU3epMz

How I can set unique name for connectionFactory?

EDIT:

I also tried to place application.properties under src/main/java/resources this configuration:

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=*

But I get the same error.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • I think a simliar question is already handled here: [link](https://stackoverflow.com/questions/7075007/same-jmx-mbean-class-for-many-application-at-same-server) – Halko Karr-Sajtarevic Jul 08 '18 at 10:02
  • Yes this solves my issue. Is there a way to use @Bean configuration to get the same result? – Peter Penzov Jul 08 '18 at 10:21
  • There are 2 ways i could think of. The first would be to disable jmx completely (if you don't need it). The second is setting the property `spring.application.name` for both and hoping that your exposed jmx-bean uses the application name as a prefix. – Halko Karr-Sajtarevic Jul 08 '18 at 10:26
  • I already tries some configuration. See the updated post. – Peter Penzov Jul 08 '18 at 10:34

1 Answers1

1

The solution:

... 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);
    }

}
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808