ActiveMQ是一个强大的消息服务器,准确的说其定位是消息总线。然后今天用1上午时间入入门体验一下消息队列
咱们的项目一般都是 基于ssh,其中spring扮演的角色就是万金油啊,这次也不例外整合ActiveMQ就靠他了
首先下载下来ActiveMQ 然后进入bin目录 启动消息服务器即可,你可以通过后台的控制窗口看消息服务器运行情况
也可以在 http://127.0.0.1:8161/admin 这里看消息服务器情况,比如几个链接当前开了几个队列,队列里是什么内容,设计相当贴心啊
言归正传谈谈整合
activation-1.1.jar
activemq-camel-5.5.0.jar
activemq-console-5.5.0.jar
activemq-core-5.5.0.jar
activemq-jaas-5.5.0.jar
activemq-pool-5.5.0.jar
activemq-protobuf-1.1.jar
activemq-web-5.5.0.jar
javax.jms.jar
geronimo-spec-j2ee-management-1.0-rc2.jar
将这些依赖包丢入工程,我可是尝试N遍 在一次又一次找不到class的异常中百度出这些jar包
然后单独定义一个spring配置文件配置消息服务
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
</property>
</bean>
<bean id="dest" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="myDest"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="defaultDestination" ref="dest"/>
</bean>
<bean id="messageSender" class="com.us.sysmanager.bs.impl.MessageSenderImpl">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
</beans>
package com.us.sysmanager.bs.impl;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.support.JmsGatewaySupport;
import com.us.sysmanager.bs.IMessageSender;
public class MessageSenderImpl extends JmsGatewaySupport implements IMessageSender {
public void sendMesage(){
//HD_Queue 是队列名 没有的话其会自行创建
getJmsTemplate().send("HD_Queue",
new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
return session.createTextMessage("哈哈哈哈");
}
});
}
public void processMessage(){
Message msg = getJmsTemplate().receive("HD_Queue");
try{
TextMessage textMessage = (TextMessage) msg;
if( msg!=null){
System.out.println(" Message Received -->" +
textMessage.getText());
}
}catch(Exception e){
e.printStackTrace();
}
}
}
ok配置好来试试吧,我是直接在action中测试的 好搓啊 呵呵
@RequestMapping("/sendMsg")
public void DelSmsTypeForId(HttpServletResponse res) throws IOException
{
messageSender.sendMesage();
res.getWriter().print("helloWorld");
}
@RequestMapping("/getMsg")
public void getMsg(HttpServletResponse res)throws IOException
{
messageSender.processMessage();
res.getWriter().print(" ");
}
测试通过啊 如果 我一开始调用 getMsg 这时队列没有信息 程序会一直阻塞 然后调用一下 sendMsg 放入消息
getMsg 就会获取消息继续执行了
现在越来越感慨,Spring把什么东西都封装的那么好以后没有spring怎么开发啊
当然 ActiveMQ的其它高级特性还有待发掘研究,这只是个简单的消息队列入门
分享到:
相关推荐
This is an introduction to the document describing ActiveMQ
activeMQ入门到精通
JMS、ActiveMQ入门及深入使用的例子,这些例子都是我测试的
程度: 入门 一.安装ActiveMQ 首先去http://activemq.apache.org/download.html 下载最新版本4.1.0release (http://activemq.apache.org/activemq-410-release.html), 解压apache-activemq-4.1-incubator.zip...
ActiveMQ入门,ActiveMQ介绍和应用案例,提供一个实例(基于spring);
activemq 配置说明与activemq入门讲解
Activemq入门实例.pdf
简单消息服务-httpsqs 博文链接:https://sosuny.iteye.com/blog/509846
activemq入门代码,里面有详细的操作步骤,是入门的不错例子,包含基本消息应用方
ActiveMQ实践入门指南
ActiveMQ 入门实战(3)--SpringBoot 整合 ActiveMQ(csdn)————程序
这是一个activemq应用的简单示例代码,使用maven搭建的.适合刚刚开始学习activemq的程序员
Apache ActiveMQ5.8 入门最简单例子 消息队列 Master
HETF-ActiveMQ入门手册.doc
下载最新版本4.1.0release, 解压apache-activemq-4.1-incubator.zip(或者apache-activemq-4.1-incubator.tar.gz) 目录如下: +bin (windows下面的bat和unix/linux下面的sh) +conf (activeMQ配置目录,包含最...
MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。?特点:?1、支持多种语言...
HETF-ActiveMQ入门手册.zip
使用activeMQ实现生产者消费者
消息队列-activemq入门实例 博客地址:https://blog.csdn.net/u010476739/article/details/100076646
Spring整合JMS(activeMQ)的示例,开发环境为eclipse+maven