This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

How to register bean with dynamic generated ObjectNamePage Title Module

How to register bean with dynamic generated ObjectName

Aug 24th, 2007, 09:00 PM

Hi,

ObjectName for MBean is usually defined in xml (key) or annotation. But it seems ObjectName has to be defined statistically.

However if ObjectName has to be generated at runtime, how should I do it?
For instances, I have UserService bean (non-singleton). When multiple users log in, multiple UserService instances need to be created. ObjectName need to contains sessionId. Where and How should I define this dynamic generated ObjectName?

You can create the ObjectName yourself and then call MBeanExporter.registerManagedResource(Object managedResource, ObjectName objectName). Just inject a reference to the MBeanExporter in the object that should perform the registration.
Another option would be to make sure the sessionid becomes available to the ObjectNamingStrategy and then create an implementation of that yourself and register it with the MBeanExporter. If this is possible, I would consider that the cleanest, since your code will be completely decoupled from Spring.
Finally, you could also make your services implement SelfNaming to provide their own ObjectName (which couples your services to Spring, obviously, but it could be the simplest solution if the sessionid is available already to your services).