PDA

View Full Version : Message destination



nfedyk
Sep 30th, 2009, 10:41 AM
Right now I'm creating it form my web-application-config.xml by defining <flex:jms-message-destination />. I'm wondering is there any way to create flex message destination dynamically from java class. Thanks.

jeremyg484
Sep 30th, 2009, 03:19 PM
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:



@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.getNa me());
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.