0条评论
还没有人评论过~
// send - 发送一个消息,使用消息创建接口MessageCreator
publicvoid send(MessageCreator messageCreator)
publicvoid send(finalDestination destination,finalMessageCreator messageCreator)
publicvoid send(finalString destinationName,finalMessageCreator messageCreator)
// sendAndReceive - 发送并接收消息
publicMessage sendAndReceive(MessageCreator messageCreator)
publicMessage sendAndReceive(finalDestination destination,finalMessageCreator messageCreator)
publicMessage sendAndReceive(finalString destinationName,finalMessageCreator messageCreator)
// convertAndSend - 使用MessageConverter接口转换消息,先将对象转换成消息再发送消息。与之对应的是receiveAndConvert方法
publicvoid convertAndSend(Object message)throwsJmsException
publicvoid convertAndSend(Destination destination,finalObject message)
publicvoid convertAndSend(String destinationName,finalObject message)
publicvoid convertAndSend(Object message,MessagePostProcessor postProcessor)
publicvoid convertAndSend(Destination destination,finalObject message,finalMessagePostProcessor postProcessor)
publicvoid convertAndSend(String destinationName,finalObject message,finalMessagePostProcessor postProcessor)
// receive - 接收消息
publicMessage receive()
publicMessage receive(Destination destination)
publicMessage receive(String destinationName)
// receiveSelected - 接收消息,并过滤消息
publicMessage receiveSelected(String messageSelector)
publicMessage receiveSelected(finalDestination destination,finalString messageSelector)
publicMessage receiveSelected(finalString destinationName,finalString messageSelector)
// receiveAndConvert - 接收消息,并使用MessageConverter接口转换消息,把一个消息转换成一个对象
publicObject receiveAndConvert()
publicObject receiveAndConvert(Destination destination)
publicObject receiveAndConvert(String destinationName)
// receiveSelectedAndConvert - 接收消息,并使用消息过滤和消息转换
publicObject receiveSelectedAndConvert(String messageSelector)
publicObject receiveSelectedAndConvert(Destination destination,String messageSelector)
publicObject receiveSelectedAndConvert(String destinationName,String messageSelector)
// browse - 浏览消息
public<T> T browse(BrowserCallback<T> action)
public<T> T browse(Queue queue,BrowserCallback<T> action)
public<T> T browse(String queueName,BrowserCallback<T> action)
// browseSelected - 浏览消息,并使用过滤
public<T> T browseSelected(String messageSelector,BrowserCallback<T> action)
public<T> T browseSelected(finalQueue queue,finalString messageSelector,finalBrowserCallback<T> action)
public<T> T browseSelected(finalString queueName,finalString messageSelector,finalBrowserCallback<T> action)
// execute - 执行SessionCallback、ProducerCallback、BrowserCallback回调接口,并得到回调接口返回值
public<T> T execute(SessionCallback<T> action)
public<T> T execute(SessionCallback<T> action,boolean startConnection)
public<T> T execute(ProducerCallback<T> action)
public<T> T execute(finalDestination destination,finalProducerCallback<T> action)
public<T> T execute(finalDestination destination,finalProducerCallback<T> action)
publicclassQueueMessageListenerimplementsMessageListener
{
@Override
publicvoid onMessage(Message message)
{
try
{
if(message instanceofTextMessage)
{
TextMessage tm =(TextMessage) message;
System.out.println("监听消息:"+ tm.getText());
}
else
{
System.out.println("消息类型:"+ message.getClass());
}
}
catch(JMSException e)
{
e.printStackTrace();
}
}
}
publicclassQueueSessionAwareMessageListenerimplementsSessionAwareMessageListener<TextMessage>
{
/** 回复消息的目的地 */
privateDestination destination;
@Override
publicvoid onMessage(TextMessage message,Session session)throwsJMSException
{
System.out.println("监听消息内容:"+ message.getText());
MessageProducer messageProducer = session.createProducer(destination);
TextMessage replyMessage = session.createTextMessage("已收到消息:"+ message.getJMSMessageID());
messageProducer.send(replyMessage);
}
publicDestination getDestination()
{
return destination;
}
publicvoid setDestination(Destination destination)
{
this.destination = destination;
}
}
<!--消息监听适配器-->
<bean id="messageListenerAdapter"class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<!--通过构造函数指定消息处理器-->
<constructor-arg>
<bean id="readerMessage"class="com.test.ReaderMessage"/>
</constructor-arg>
<!--指定消息处理器类处理消息的方法-->
<property name="defaultListenerMethod" value="receiveMessage"/>
<!--通过delegate属性指定消息处理器-->
<property name="delegate">
<bean id="readerMessage"class="com.test.ReaderMessage"/>
</property>
</bean>
publicclassMessageListenerByAdapter
{
publicvoid handleMessage(String message)
{
System.out.println("handleMessage方法处理消息,消息内容是:"+ message);
}
publicvoid receiveMessage(String message)
{
System.out.println("receiveMessage方法处理消息,消息内容是:"+ message);
}
}
<!--设置连接工厂-->
<property name="connectionFactory" ref="connectionFactory"/>
<!--设置监听地址-->
<property name="destination" ref="queueDestination"/>
<!--设置消息监听处理器,可以是以上三种-->
<property name="messageListener" ref="queueMessageListener"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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"
xsi:schemaLocation="
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-lazy-init="false">
<!-- 配置JMS连接工厂 -->
<beanid="connectionFactory"class="org.apache.activemq.ActiveMQConnectionFactory">
<propertyname="brokerURL"value="failover:(tcp://localhost:61616)"/>
</bean>
<!-- ActiveMQ连接池配置,ActiveMQ实现 -->
<!--
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
-->
<!-- ActiveMQ连接池配置,SpingJMS实现 -->
<beanid="cachingConnectionFactory"class="org.springframework.jms.connection.CachingConnectionFactory">
<propertyname="targetConnectionFactory"ref="connectionFactory"/>
<!-- Session缓存数量,这里属性也可以直接在这里配置 -->
<propertyname="sessionCacheSize"value="100"/>
</bean>
<!-- 消息队列01 -->
<beanid="queueDestination01"class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>spring-jms-queue01</value>
</constructor-arg>
</bean>
<!-- 消息队列02 -->
<beanid="queueDestination02"class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>spring-jms-queue02</value>
</constructor-arg>
</bean>
<!-- 配置JMS模板,Spring提供的JMS工具类,它发送、接收消息。 -->
<beanid="jmsTemplate"class="org.springframework.jms.core.JmsTemplate">
<propertyname="connectionFactory"ref="cachingConnectionFactory"/>
<propertyname="defaultDestination"ref="queueDestination01"/>
<propertyname="receiveTimeout"
标签: