ConnectionFactory用于管理连接的连接工厂
JmsTemplate用于发送和接收消息的模板类
MessageListerner消息监听器
ConnectionFactory
一个Spring为我们提供的连接池
JmsTemplate每次发消息都会重新创建连接,会话和producer
spring中提供了SingleConnectionFactory和CachingConnectionFactory(继承了SingleConnectionFactory有缓存)
JmsTemplate
是spring提供的,只需向spring容器内注入这个类就可以使用JmsTemplate方便操作jms
JmsTemplate类是线程安全的,可以在整个应用范围使用。
MessageListerner
实现一个onMessage方法,该方法只接收一个Message参数
pom文件里添加引用
web.xml里的配置
contextConfigLocation
classpath:applicationContext*.xml
applicationContext-common.xml
applicationContext-producer.xml
applicationContext-consumer.xml
package com.imooc.mvcdemo.service.impl;
import com.imooc.mvcdemo.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.*;
/**
* @author Jas
* @create 2018-04-14 9:21
**/
@Service
public class ProducerServiceImpl implements ProducerService {
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
public void sendMessage(final String message) {
Destination destination = jmsTemplate.getDefaultDestination();
System.out.println(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息---------------------->"+message);
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
System.out.println("消息已发送:" + message);
}
public void sendMessage(Destination destination,final String message){
System.out.println(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息---------------------->"+message);
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
package com.imooc.mvcdemo.service.impl;
import com.imooc.mvcdemo.service.ConsumerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* Created by QXJJ1697 on 2018/7/16.
*/
@Service
public class ConsumerServiceImpl implements ConsumerService {
@Resource(name="jmsTemplate")
private JmsTemplate jmsTemplate;
public TextMessage receive(Destination destination){
TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
try{
System.out.println("从队列" + destination.toString() + "收到了消息:\t"
+ textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
return textMessage;
}
}
package com.imooc.mvcdemo.listener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* @author Jas
* @create 2018-04-14 10:07
**/
public class ConsumerMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("消息已接收:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.imooc.mvcdemo.controller;
import com.imooc.mvcdemo.service.ConsumerService;
import com.imooc.mvcdemo.service.ProducerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.TextMessage;
/**
* Created by QXJJ1697 on 2018/7/16.
*/
@Controller
@RequestMapping("/message")
public class MessageController {
private Logger logger = LoggerFactory.getLogger(MessageController.class);
@Resource(name = "activeMQQueue")
private Destination destination;
//队列消息生产者
@Resource(name = "producerServiceImpl")
private ProducerService producer;
//队列消息消费者
@Resource(name = "consumerServiceImpl")
private ConsumerService consumer;
@RequestMapping(value = "/SendMessage", method = RequestMethod.POST)
@ResponseBody
public void send(String msg) {
logger.info(Thread.currentThread().getName()+"------------send to jms Start");
//producer.sendMessage(msg);
producer.sendMessage(destination,msg);
logger.info(Thread.currentThread().getName()+"------------send to jms End");
}
@RequestMapping(value= "/ReceiveMessage",method = RequestMethod.GET)
@ResponseBody
public void receive(){
logger.info(Thread.currentThread().getName()+"------------receive from jms Start");
consumer.receive(destination);
logger.info(Thread.currentThread().getName()+"------------receive from jms End");
}
}
在上面的配置文件中,我们已经默认的添加了这段监听器的配置文件,如果同学们不想使用这个监听器,可以直接注释掉。
我们可以看到,使用监听器的效果,和手动接收消息的效果是一样的。