关于《关于ActiveMQ的整理》所设计的方案虽然可以通过引用demo中的chat.html文件可以实现,但是放到本地上面在测试的时候却怎么也出不来想要的结果,可以发送消息,并且消息可以发送成功(在topic中也可以查看到,可以通过http://127.0.0.1:8161/admin查看,但是一定要先启动ActiveMQ服务)。
这篇文章是介绍websocket的使用方法,websocket是ActiveMQ5.9版本以后才集成出来的,所以下载ActiveMQ插件的时候一定要是5.9版本以后。
先启动ActiveMQ服务,访问下面的网址即可:
http://127.0.0.1:8161/demo/websocket/index.html
界面上面所示,首先要说一下,上面的LisenQueue和UserName自我们自己添加的,源码中是没有这两项的,原因如下:
源码只设置了发送队列却没有设置监听队列,这样就会导致发送的消息被自己监听到,而其他的人就监听不到了消息了,这样就是去了p2p的意义了。所以我们设置了监听队列,而且名字不可以相同。
改动如下(index.html文件):
因为index.html引用的chat.js文件,所以也需要在chat.js里面稍作修改。
这样就可以避免了那种情况的发生。
下面我就介绍一下在后台发送消息,在前台接收消息的配置:
1、新建一个web project项目MQQueue,引入ActiveMQ相关的jar包以及spring相关的jar包。
2、引入之后需要建立一个applicationContext.xml文件对spring进行配置。
<?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="myQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="wangwanxing" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="myQueue" />
</bean>
</beans>
3、然后再web.xml文件中将applicationContext.xml文件加载进来。
<