java实现基于activeMQ的消息推送
此⽂章是基于 
⼀. 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. l
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" xmlns:xsi="/2001/XMLSchema-instance"
xmlns:tx="/schema/tx" xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.1.xsd        /schema/tx /schema/tx/spring-tx-3.1.xsd
/schema/aop /schema/aop/spring-aop-3.1.xsd
/schema/context /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.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"/>
<property name="receiveTimeout" value="60000"/>
</bean>
<!-- 消息队列,表⽰是点到点(P2P)消息通讯模型 -->
<bean id="msgQueue" class="org.apache.activemqmand.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>
View Code
  2. JmsSender.java,消息发送器接⼝
package com.ims.interfaces.jms.activeMQ;
public interface JmsSender {
public void send(final MsgContext context);
}
View Code
  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.JmsTemplate;
import org.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 = ateObjectMessage();
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;
}
}
View Code
  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;
/**activemq默认账号密码
* 发送的⽅式
*/
private final Collection<String> modes;
public MsgContext(Collection<String> users, String message,
Collection<String> modes){
this.users = users;
}
public Collection<String> getUsers() {
return users;
}
public String getMessage() {
return message;
}
public Collection<String> getModes() {
return modes;
}
}
View Code
  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) Object();
}catch (JMSException e) {
return;
}
System.out.Message());
if(context==null || Modes()==null || Modes().size()==0){
return;
}
// 取到消息上下⽂后,可执⾏⾃定义操作
}
}
View Code
  6. TestController.java,测试jms消息发送
package com.ller;
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.imsmon.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 = Service("jmsSender");
Collection<String> users = new HashSet<String>();
Collection<String> modes = new HashSet<String>();
MsgContext msgContext = new MsgContext(users, msg, modes);
jmsSender.send(msgContext);
}
}
View Code
  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>
View Code
三. 测试
  2. 输⼊消息内容,点击发送,MsgListener 类会在后台输出消息内容

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。