Sure, it's possible. All you really need is a reference to the MessageBroker (which you could have injected by Spring), then you can use its API to create the destination. For example:
Code:
@Component
public class DynamicDestinationExample {
private final MessageBroker broker;
@Autowired
public DynamicDestinationExample(MessageBroker broker) {
this.broker = broker;
}
public void createMessageDestination(String id) {
MessageService service = (MessageService) broker.getServiceByType(MessageService.class.getName());
MessageDestination dest = (MessageDestination) service.createDestination(id);
//... set properties on the destination and initialize its adapter ...
dest.start();
}
}
The thing that's a bit tricky is getting everything initialized correctly, especially if you want the destination to be backed by JMS because you have the additional burden of configuring and setting the JmsAdapter at runtime.
I'd suggest taking a look at the source of MessageDestinationFactory to see what all is required. You might even consider setting up and invoking an instance of MessageDestinationFactory directly from your code and overriding its configureAdapter() method to programmatically set up the JmsAdapter.
Jeremy Grelle
Staff Engineer, Web Products Team
SpringSource