1、简介:ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
2、建议在使用以前了解一下jms的一些知识
3、第一步:我们部署相关的activemq(我这里是采用自己本地linux虚拟机来实现的,以模拟中间推送消息的原理)
这里有这个zip的是windows用的,tar.gz的是Linux用的。我这里采用的Linux部署
将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'
这样整个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
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)展示结果:
(4)activemq的简单使用和配置方式就才不多这么多
5、我的项目结构