一.前期准备
1.导入依赖 :
先导入SpringMVC所有依赖
然后导入三个额外的
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
2.配置SpringMVC : SSM-servlet.xml
略
3.配置ActiveMQ : SSM-MQ.xml
<!--配置ActiveMQ的连接信息-->
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://127.0.0.1:61616"
userName="admin"
password="admin"/>
<!--配置JMS的连接工厂-->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory"/>
<property name="sessionCacheSize" value="100"/>
</bean>
<!--配置消息队列-->
<bean id="queueDestination"
class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="0602springMQ"/>
</bean>
<!--配置JMS模板,是Spring提供的JMS工具类,它负责发送和接收消息-->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<!--指定连接工厂-->
<property name="connectionFactory"
ref="connectionFactory"/>
<!--指定消息队列-->
<property name="defaultDestination"
ref="queueDestination"/>
<!--指定接收的超时时间-->
<property name="receiveTimeout"
value="10000"/>
<!--
如果是true,消息模式为topic
如果是false,就是queue
如果不写,默认是false
-->
<property name="pubSubDomain" value="false"/>
</bean>
</beans>
4.配置web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--ActiveMQ配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SSM-MQ.xml</param-value>
</context-param>
<!--防止显示乱码-->
<filter>
<filter-name>SpringEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--ActiveMQ监听器配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC的配置-->
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SSM-servlert.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二.编写程序
1.目录结构
2.编写service层
-
ProducerService
@Service
public class ProducerService {
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
//发送到参数中指定的目的地
public void sendMessage(Destination destination,final String msg){
System.out.println("发送: "+msg+" 到: "+destination);
MessageCreator messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
};
jmsTemplate.send(destination,messageCreator);
}
//发送到默认目的地(SSM-MQ.xml配置文件中配置的目的地)
public void sendMessage(final String msg){
String destinationName = jmsTemplate.getDefaultDestinationName();
System.out.println("发送: "+msg+" 到: "+destinationName);
MessageCreator messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
};
jmsTemplate.send(destinationName,messageCreator);
}
}
-
ConsumerService
@Service
public class ConsumerService {
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
public TextMessage receiveMsg(Destination destination){
TextMessage textMessage =
(TextMessage) jmsTemplate.receive(destination);
if (textMessage!=null){
try {
System.out.println("接收到消息: "+
textMessage.getText() +
"; 来自: "+destination);
} catch (JMSException e) {
e.printStackTrace();
}
}
return textMessage;
}
}
3.编写Controller层
@Controller
public class MQController {
@Resource
private Destination queueDestination;
@Resource
private ProducerService producerService;
@Resource
private ConsumerService consumerService;
@RequestMapping(value = "/producer")
public String producerHandle(){
return "producer";
}
@RequestMapping(value = "/receive")
public ModelAndView queueReceive() throws JMSException {
ModelAndView mv = new ModelAndView();
TextMessage tm = consumerService.receiveMsg(queueDestination);
if (tm!=null){
mv.addObject("textMessage",tm.getText());
}else {
mv.addObject("textMessage","没有新消息");
}
//站内信/微信/私聊
mv.setViewName("consumer");
return mv;
}
//在消息提供页面,使用form表单发送信息
//发送完返回首页
@RequestMapping(value = "/onsend")
public String sendMsg(
@RequestParam("msg") String message){
System.out.println(">>>消息发送到jms<<<");
producerService.sendMessage(queueDestination,message);
return "home";
}
}
4.jsp测试页面
-
producer.jsp
<body>
消息发送
<form action="/onsend" method="post">
文本信息:<textarea name="msg">测试信息</textarea>
<input type="submit" value="提交信息">
</form>
<a href="/home">回到首页</a>
</body>
-
consumer.jsp
<body>
<h1>接收消息</h1>
<h2>${textMessage}</h2>
<a href="/home">回到首页</a>
</body>
-
home.jsp
<body>
首页
<h2><a href="/producer">消息发送页面</a></h2>
<h2><a href="/receive">消息接收页面</a></h2>
</body>