java springmvc jms_springmvc和activemq的整合使用

本文介绍了Apache ActiveMQ的部署步骤,包括在Linux环境下解压、启动、配置开机自启等,并提供了Java代码示例,展示了如何使用Spring框架进行消息发送与监听。在Java代码中,实现了TopicMessageListen监听器和TopicSendMessage发送器,详细说明了配置文件与代码的编写。
摘要由CSDN通过智能技术生成

1、简介:ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。

2、建议在使用以前了解一下jms的一些知识

3、第一步:我们部署相关的activemq(我这里是采用自己本地linux虚拟机来实现的,以模拟中间推送消息的原理)

79ac047be4b1da548d4482350e6dc228.png

这里有这个zip的是windows用的,tar.gz的是Linux用的。我这里采用的Linux部署

73f7ae391afa68d5fc9a737f09e47497.png

将activemq的tar解压启动

启动方式在:

/root/apache-activemq-5.14.5/bin/linux-x86-64

启动:./activemq start 暂停: ./activemq stop 重启:./activemq restart

启动起来后,访问地址为:http://192.168.5.10:8161/admin 账号密码:都是admin(默认)

开机自启:vi /etc/rc.local 然后在末尾加入

su - root -c '/usr/local/activemq/bin/activemq start'

5730230e3354058dc0de9c4da3748c53.png

这样整个activemq就部署好了,因为activemq是单独的项目,启动过后不用再理会

4、第二步:Java代码实现,具体项目的配置

1)导包:pom.xml

4.0.0

com.troy

activemq

0.0.1-SNAPSHOT

war

org.springframework

spring-webmvc

3.2.8.RELEASE

org.apache.activemq

activemq-spring

5.14.5

org.springframework

spring-jms

3.2.8.RELEASE

c5223671ebd9cd6e6aa83bce68f46cee.png

2)根据相关jar的属性进行xml的配置

(1)配置activemq的连接spring-config.properties

activemq_url=tcp://192.168.5.10:61616

activemq_username=admin

activemq_password=admin

(2)配置相关的activeMQ

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

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd

http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd

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

">

classpath:conf/spring-config.properties

3)代码层面我谢了两个:方便测试

(1)一个是监听接口获取数据(TopicMessageListen)

public class TopicMessageListen implementsMessageListener{public voidonMessage(Message message) {

System.out.println("监听==================监听");try{

System.out.println(message);

TextMessage tm=(TextMessage)(message);

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

}catch(Exception e) {

e.printStackTrace();

}

}

}

因为我们在配置文件里面加入了监听,这里只需要实现MessageListener接口就可以了,然后在处理message信息

(2)发送消息(TopicSendMessage)

public classTopicSendMessage {private ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml");private JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");public voidsend(){

jmsTemplate.send(newMessageCreator() {public Message createMessage(Session session) throwsJMSException {

TextMessage msg=session.createTextMessage();

msg.setText("发送数据++++++++++++发送数据");

System.out.println("发送数据++++++++++++发送数据");returnmsg;

}

});

}public voidreceive(){

Message msg=jmsTemplate.receive();

TextMessage tm=(TextMessage)msg;

System.out.println("非监听------------------非监听");

System.out.println(msg);

}public static voidmain(String[] args) {newTopicSendMessage().send();

}

}

说明:发送数据的方式基本上大同小异通过获取jmsTemplate来实现发送的操作,因为我没有直接启动容器,所以采用获取bean的方式

接收上面没有运行,因为接收的这个receive()方法是同步运行的,会卡线程这里不做演示,我通过启动两个方式测试可以成功的,但是不建议这种方式

(3)展示结果:

604624f8caa9ff2115bd001beee885d8.png

(4)activemq的简单使用和配置方式就才不多这么多

5、我的项目结构

dc636f01e6dcbe217c8eaca78db490f9.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值