A convenient MessageTemplate
helper class is provided that allows you to push messages to any BlazeDS MessageDestination
from a simple
POJO. This provides a nice abstraction over push style messaging that hides away the details of the underlying messaging protocol. Whether using a simple
AMF based destination or full-blown JMS, etc., the use of the MessageTemplate
stays the same. The only thing the MessageTemplate
requires
is a reference to a Spring-managed MessageBroker
. If the MessageTemplate
is configured as a Spring bean, it will try and auto-detect
the MessageBroker
from its application context.
As an example of how the MessageTemplate
could be used, suppose we have a RESTful travel application that has a Flex-based admin console but also
exposes an API over HTTP. To give the admin console a "live" view of the data, we want to push updates to it anytime a new hotel booking is created. Given the
following setup in our application context:
<flex:message-broker /> <bean id="defaultMessageTemplate" class="org.springframework.flex.messaging.MessageTemplate" /> <flex:message-destination id="bookingUpdates" />
and assuming the Flex client is subscribed to the "bookingUpdates" destination, this could be achieved with the following controller code:
@Controller public class BookingController { private MessageTemplate template; private BookingService bookingService; @RequestMapping(value="/bookings", method=RequestMethod.POST) public String createBooking(Booking booking){ booking = bookingService.saveBooking(booking); template.send("bookingUpdates", booking); return "redirect:/bookings/"+booking.getId(); } @Autowired public void setTemplate(MessageTemplate template) { this.template = template; } @Autowired public void setBookingService(BookingService bookingService) { this.bookingService = bookingService; } }