ActiveMQ与spring整合,从后台发送消息,从前台接收消息。

来源:互联网 发布:2017c语言成绩查询系统 编辑:程序博客网 时间:2024/06/01 08:30

关于ActiveMQ文档整理(以topic为例,从后台发送消息,从前台接收消息)

1、首先需要创建一个Web Project项目,引入ActiveMQ所需要的jar包,如图所示:

 

2、和spring进行整合,首先需要创建一个applicationContext.xml文件,在这个文件里面需要配置的内容。

(1)配置链接工厂,设置代理URL

<bean id="connectionFactory"    class="org.apache.activemq.ActiveMQConnectionFactory">

        <property name="brokerURL" value="tcp://127.0.0.1:61616" />

</bean>

(2)配置jms模板

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

         <property name="connectionFactory" ref="connectionFactory" />

         <property name="defaultDestination" ref="myTopic" />

</bean>

(3)创建一个topic

<bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic">

        <constructor-arg index="0" value="CHAT.DEMO" />

</bean>

所以完整的applicationContext.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:amq="http://activemq.apache.org/schema/core" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

  http://www.springframework.org/schema/beans 

  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

  http://activemq.apache.org/schema/core 

  http://activemq.apache.org/schema/core/activemq-core-5.1.0.xsd

  http://activemq.apache.org/camel/schema/spring 

  http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

 

<bean id="connectionFactory"   class="org.apache.activemq.ActiveMQConnectionFactory">

        <property name="brokerURL" value="tcp://127.0.0.1:61616" />

</bean>

 

<bean id="myTopic"    class="org.apache.activemq.command.ActiveMQQueue">

           <constructor-arg index="0" value="CHAT.DEMO" />

</bean>

 

<bean id="jmsTemplate"   class="org.springframework.jms.core.JmsTemplate">

        <property name="connectionFactory" ref="connectionFactory" />

        <property name="defaultDestination" ref="myTopic" />

</bean>

</beans>

配置完applicationContext.xml文件之后,需要在web.xml中进行配置,也就是启动项目的时候讲applicationContext.xml加载进来。如下所示:

<listener>

          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

        <param-name>contextConfigLocation</param-name>

         <param-value>classpath:applicationContext.xml</param-value>

</context-param>

这样就把applicationContext.xml文件加载进来了。

3、建一个发送者类(TopicSender),发送一个消息。代码如下:

package com.activemq.test;

 

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jms.core.JmsTemplate;

import org.springframework.jms.core.MessageCreator;

public class TopicSender {

private JmsTemplate jmsTemplate;

/**

 * 发送一条消息到指定的队列(目标)

 * 

 * @param queueName

 *            队列名称

 * @param message

 *            消息内容

 */

private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(  

            new String[] { "classpath:/applicationContext.xml" }); 

public void send(String topicName, final String message) {

jmsTemplate=(JmsTemplate) applicationContext.getBean("jmsTemplate");

// 获得发送消息的目的地  

        Destination destination = (Destination)applicationContext.getBean("myTopic");

jmsTemplate.send(destination, new MessageCreator() {

public Message createMessage(Session session) throws JMSException {

TextMessage m = session.createTextMessage(message);

System.out.println(m.getText());

return session.createTextMessage(message);

}

});

}

}

4、建一个Test类来进行测试,我用的是servlet来发送消息。代码如下:

package com.activemq.test;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class InitServlet extends HttpServlet {

private static final long serialVersionUID = -2736542937406867893L;

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

TopicSender topicSender = new TopicSender();

String message = request.getParameter("message");

//CHAT.DEMO就是队列名称,向这个队列里面发送消息

topicSender.send("CHAT.DEMO""<message type=\"chat\" from=\"admin\">"+message+"</message>");

}

}

5、新建一个页面index.html文件,着这个文件里面我们直接用iframe引入ActiveMQdemo里面的chat.html,也就是说用demo里面的chat.html进行监听消息。页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>index.html</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<meta http-equiv="Access-Control-Allow-Origin" content="*">

</head>

<body>

<iframe src="http://127.0.0.1:8161/demo/chat.html" frameborder="0" width="100%" height="100%" scrolling="auto"></iframe>

</body>

</html>

 

其实你可以查看一下demo里面的chat.html文件

 

web.xml文件(所以我们引用demochat.html之后就不需要我们再配置了)

 

6、访问地址入下(我的项目名称为MQAjax

(1)发送消息的访问路径为:

http://127.0.0.1:8080/MQAjax/servlet/InitServlet?message=ncaishizhune

(注意:后面的message参数是发送的消息,这里的我传的的用户名为admin),每当刷新一次就会发送一条消息:在后台可以看到发送的消息:

 

2)接收消息的访问路径为:

http://127.0.0.1:8080/MQAjax,效果如下:

 

这样就可以实现聊天功能了,每当我发送一条信息之后,都可以在接收页面查看到消息。

为了加上提示功能,我把easyui插件引进来了,用messager组件做了一个消息提醒功能(显示在右下角):

demochat.html文件中加入以下代码(当然要引入easyui所需要的js文件):

 

function showMessage(message){

$.messager.show({

title:'消息提醒',

msg:message,

timeout:5000,

showType:'slide'

});

}

这就是提示功能,所以在监听到消息的时候就需要调用这个方法(在chat.js文件中调用):

 

这样就可以在右下角提示了,效果图:

 

这就是我通过servlet发送过来的消息,在右下角有提醒,在聊天框中也有显示,这样就实现了在后台发送,在前台接收的功能了。

遇到的问题:直接引用demo中的chat.html文件报了一个错误,就是跨域问题。为了解决这个问题需要在配置文件(web.xml)中加入以下代码:

 

<!-- 跨域支持-->

<filter>

   <filter-name>cross-origin</filter-name>

<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter

   </filter-class>

   <init-param>

       <param-name>allowedOrigins</param-name>

       <param-value>*</param-value>

   </init-param>

   <init-param>

       <param-name>allowedMethods</param-name>

       <param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>

   </init-param>

   <init-param>

       <param-name>allowedHeaders</param-name>

   <param-value>origin,content-type,accept,uthorization</param-value>

   </init-param>

</filter>

<filter-mapping>

    <filter-name>cross-origin</filter-name>

    <url-pattern>*</url-pattern>

</filter-mapping>


 

0 0
原创粉丝点击