对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

发送消息

JmsTemplate 包含许多用于发送消息的便捷方法。发送方法通过使用 jakarta.jms.Destination 对象来指定目标,其他方法则通过在 JNDI 查找中使用 String 来指定目标。不带目标参数的 send 方法使用默认目标。spring-doc.cadn.net.cn

以下示例使用 MessageCreator 回调从提供的 Session 对象创建文本消息:spring-doc.cadn.net.cn

import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.Queue;
import jakarta.jms.Session;

import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.JmsTemplate;

public class JmsQueueSender {

	private JmsTemplate jmsTemplate;
	private Queue queue;

	public void setConnectionFactory(ConnectionFactory cf) {
		this.jmsTemplate = new JmsTemplate(cf);
	}

	public void setQueue(Queue queue) {
		this.queue = queue;
	}

	public void simpleSend() {
		this.jmsTemplate.send(this.queue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage("hello queue world");
			}
		});
	}
}

在前面的示例中,JmsTemplate是通过传递对ConnectionFactory的引用构造的。作为替代方案,提供了无参数构造函数和connectionFactory,可以用于以JavaBean方式(使用BeanFactory或普通Java代码)构造实例。或者,可以考虑继承Spring的JmsGatewaySupport便捷基类,该类为JMS配置提供了预构建的bean属性。spring-doc.cadn.net.cn

send(String destinationName, MessageCreator creator) 方法允许您通过使用目标的字符串名称发送消息。如果这些名称在 JNDI 中注册, 您应该将模板的 destinationResolver 属性设置为 JndiDestinationResolver 的实例。spring-doc.cadn.net.cn

如果您创建了 JmsTemplate 并指定了默认目标,那么 send(MessageCreator c) 会将消息发送到该目标。spring-doc.cadn.net.cn

使用消息转换器

为了便于发送领域模型对象,JmsTemplate提供了多种send方法,这些方法将Java对象作为消息数据内容的参数。重载方法convertAndSend()receiveAndConvert()JmsTemplate中将转换过程委托给MessageConverter接口的一个实例。此接口定义了在Java对象和JMS消息之间进行转换的简单契约。默认实现(SimpleMessageConverter)支持StringTextMessagebyte[]BytesMessage以及java.util.MapMapMessage之间的转换。通过使用转换器,您可以和您的应用程序代码专注于通过JMS发送或接收的业务对象,而不必关心它作为JMS消息如何表示的细节。spring-doc.cadn.net.cn

沙箱当前包含一个 MapMessageConverter,它使用反射在 JavaBean 和 MapMessage 之间进行转换。您可能需要自己实现的其他流行实现选择是使用现有的 XML 序列化包(如 JAXB 或 XStream)来创建一个表示该对象的 TextMessagespring-doc.cadn.net.cn

为了适应设置消息的属性、头和正文,这些无法被封装在转换器类中,MessagePostProcessor接口在消息被转换后但在发送前为您提供对消息的访问。以下示例显示了如何在java.util.Map被转换为消息后修改消息头和属性:spring-doc.cadn.net.cn

public void sendWithConversion() {
	Map map = new HashMap();
	map.put("Name", "Mark");
	map.put("Age", new Integer(47));
	jmsTemplate.convertAndSend("testQueue", map, new MessagePostProcessor() {
		public Message postProcessMessage(Message message) throws JMSException {
			message.setIntProperty("AccountID", 1234);
			message.setJMSCorrelationID("123-00001");
			return message;
		}
	});
}

这将生成如下形式的消息:spring-doc.cadn.net.cn

MapMessage={
	Header={
		... standard headers ...
		CorrelationID={123-00001}
	}
	Properties={
		AccountID={Integer:1234}
	}
	Fields={
		Name={String:Mark}
		Age={Integer:47}
	}
}

使用 SessionCallbackProducerCallback

虽然发送操作涵盖了许多常见的使用场景,但有时你可能需要对 JMS SessionMessageProducer 执行多个操作。 SessionCallbackProducerCallback 分别公开了 JMS SessionSession / MessageProducer 对。 execute() 方法在 JmsTemplate 上运行这些回调方法。spring-doc.cadn.net.cn