此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台
一. jar包介绍
1. 点击此下载 apache-activemq-5.3.0,得到:
activemq-all-5.3.0.jar
activemq-web-5.3.0.jar
2. spring-framework-4.3.4.RELEASE 的 libs 文件夹下得到:
spring-jms-4.3.4.RELEASE.jar
二. 相关程序代码
1. applicationInterface.xml
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- vm: 表示客户端和activeMQ代理在同一个Java虚拟机(VM)中运行 -->
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost?broker.persistent=false&broker.useJmx=false" />
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="receiveTimeout" value="60000" />
</bean> <!-- 消息队列,表示是点到点(P2P)消息通讯模型 -->
<bean id="msgQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="msgQueue" />
</bean> <!-- 消息发送端 -->
<bean id="jmsSender"
class="com.ims.interfaces.jms.activeMQ.JmsSenderImpl">
<property name="jmsTemplate" ref="jmsTemplate" />
<property name="msgQueue" ref="msgQueue"/>
</bean> <!-- 消息异步接收端-->
<bean id="msgListenerContainer"
class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="msgQueue" />
<property name="messageListener" ref="msgListener"/>
</bean> <!-- 消息监听、执行器 -->
<bean id="msgListener"
class="com.ims.interfaces.jms.activeMQ.MsgListener">
</bean> </beans>
2. JmsSender.java,消息发送器接口
package com.ims.interfaces.jms.activeMQ; public interface JmsSender {
public void send(final MsgContext context);
}
3. JmsSenderImpl.java,消息发送器实现
package com.ims.interfaces.jms.activeMQ; import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session; import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; public class JmsSenderImpl implements JmsSender{
private JmsTemplate jmsTemplate; /**
* 消息通告队列,表示点到点通讯模型
*/
private Destination msgQueue; /**
* 发送消息
* @param context 消息上下文
*/
public void send(final MsgContext context) {
getJmsTemplate().send(getMsgQueue(), new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
final ObjectMessage message = session.createObjectMessage();
message.setObject(context);
return message;
}
});
} public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
} public Destination getMsgQueue() {
return msgQueue;
}
public void setMsgQueue(Destination msgQueue) {
this.msgQueue = msgQueue;
} }
4. MsgContext.java,消息上下文类
package com.ims.interfaces.jms.activeMQ; import java.io.Serializable;
import java.util.Collection; public class MsgContext implements Serializable{ private static final long serialVersionUID = -7877770277875813295L; /**
* 消息接收者
*/
private final Collection<String> users; /**
* 发送的消息
*/
private final String message; /**
* 发送的方式
*/
private final Collection<String> modes; public MsgContext(Collection<String> users, String message,
Collection<String> modes){
this.users = users;
this.message = message;
this.modes = modes;
} public Collection<String> getUsers() {
return users;
} public String getMessage() {
return message;
} public Collection<String> getModes() {
return modes;
}
}
5. MsgListener.java,消息监听、执行器
package com.ims.interfaces.jms.activeMQ; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage; import org.springframework.jms.support.JmsUtils; public class MsgListener implements MessageListener{ /**
* 监听到有消息时,执行的操作
* @param msg 消息对象
*/
@Override
public void onMessage(Message message) {
if(message == null) { return; }
// 获取对象消息
final ObjectMessage objectMessage = (ObjectMessage) message; // 消息上下文
MsgContext context = null; try {
// 从对象消息中获取消息上下文对象
context = (MsgContext) objectMessage.getObject();
}catch (JMSException e) {
JmsUtils.convertJmsAccessException(e);
return;
} System.out.println(context.getMessage());
if(context==null || context.getModes()==null || context.getModes().size()==0){
return;
} // 取到消息上下文后,可执行自定义操作
} }
6. TestController.java,测试jms消息发送
package com.ims.web.controller; import java.util.Collection;
import java.util.HashSet; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.ims.common.ServiceLocator;
import com.ims.interfaces.jms.activeMQ.JmsSender;
import com.ims.interfaces.jms.activeMQ.MsgContext; @Controller
@RequestMapping("test")
public class TestController extends BaseController{ @RequestMapping("view")
public ModelAndView test(){
ModelAndView view = new ModelAndView("test.jsp");
return view;
} @RequestMapping("jms!send")
public void send(@RequestParam String msg){
JmsSender jmsSender = ServiceLocator.getService("jmsSender");
Collection<String> users = new HashSet<String>();
Collection<String> modes = new HashSet<String>();
MsgContext msgContext = new MsgContext(users, msg, modes);
jmsSender.send(msgContext);
}
}
7. jms.jsp,测试jms消息发送
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
<%@ include file="/common/basePath.jsp"%>
</head>
<body>
~~~~~~~~~~~~~~~~~~~~~~jms消息发送~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<br><br>
消息: <input type="text" name="msg" id="msg">
<br><br>
<button type="button" onclick="send();">发送</button> <script type="text/javascript" src="content/js/jquery/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
function send(){
$.ajax({
url:rootPath+"/test/jms!send.do?msg="+$('#msg').val(),
async:false
});
}
</script>
</body>
</html>
三. 测试
1. 访问:http://localhost:8080/ims/test/jms.do
2. 输入消息内容,点击发送,MsgListener 类会在后台输出消息内容