Listing 16-16. The MessageSender Interface
package com.apress.prospring3.ch16.jms.sender;
public interface MessageSender {
public void sendMessage(String message);
}
Listing 16-17. The SimpleMessageSender Class
package com.apress.prospring3.ch16.jms.sender;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.jms.core.JmsTemplate;
import
org.springframework.jms.core.MessageCreator;
import
org.springframework.stereotype.Component;
@Component("messageSender")
public class SimpleMessageSender implements MessageSender {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(final String message) {
this.jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
return session.createTextMessage(message);
}
});
}
}
As shown in Listing 16-17, an instance of JmsTemplate is injected. In the sendMessage() method, we
call the JmsTemplate.send() method, with an in-place construction of an instance of the
org.springframework.jms.core.MessageCreator interface. In the MessageCreator instance, the
createMessage() method is implemented to create a new instance of TextMessage that will be sent to
ActiveMQ.
Listing 16-18 shows the Spring configuration of the JMS sender (jms-sender-app-context.xml).
Listing 16-18. Spring Configuration for Sending Messages
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:p="http://www.springframework.org/schema/p"
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home