ActiveMQ入门_20110720

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的其它高级特性还有待发掘研究,这只是个简单的消息队列入门
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值