ActiveMq的安装及使用
来源:互联网 发布:肠道益生菌 知乎 编辑:程序博客网 时间:2024/06/06 03:06
ActiveMq的安装
- 解压缩ActiveMq的压缩包
- 启动ActiveMq 在解压目录下bin中使用 activemq start
java操作ActiveMq
消息发布者的使用
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(""); Connection connection=connectionFactory.createConnection(); connection.start(); Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue=session.createQueue(""); MessageProducer producer=session.createProducer(queue); TextMessage textMessage=session.createTextMessage(); textMessage.setText(""); producer.send(textMessage); producer.close(); session.close(); connection.close();
消息接受者
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(""); Connection connection=connectionFactory.createConnection(); connection.start(); Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue=session.createQueue(""); MessageConsumer consumer=session.createConsumer(queue); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { TextMessage textMessage=(TextMessage) message; try { textMessage.getText(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); consumer.close(); session.close(); connection.close();
ActiveMq与Spring的整合
消息生产者
- 先配置jms的connectionFactory
- 然后配置singleConnectionFactory
- 配置queue或者topic
配置JmsTemplate
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.168:61616" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean><!-- 配置生产者 --><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory" /></bean><!--这个是队列目的地,点对点的 --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg></bean>
消费者的配置
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.168:61616" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="myMessageListener" /></bean><!--这个是队列目的地,点对点的 --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg></bean>
阅读全文
0 0
- ActiveMQ的安装及使用
- ActiveMq的安装及使用
- ActiveMQ安装及使用
- ActiveMQ安装配置及使用
- ActiveMQ安装及使用实例
- activemq的安装使用
- activeMQ的原理及使用
- activemq-cpp安装及使用(1)
- activemq-cpp安装及使用(2)
- activemq-cpp安装及使用(3)
- ActiveMQ—安装配置及使用
- ActiveMQ的安装与使用
- ActiveMQ的安装使用教程
- 【ActiveMQ】ActiveMQ的使用
- ActiveMQ的安装及实例运行
- ActiveMQ的简单安装及介绍
- ActiveMQ安装及启动遇到的问题
- 【ActiveMq】ActiveMQ消息队列的使用及应用
- Android 活动的启动模式
- Ionic 三级目录乃至多级目录隐藏tabs
- Java IO流 ---字节流 案例分析
- VRTK插件官方示例分析-040_Controls_PanelMenu
- 微信小程序固定表头
- ActiveMq的安装及使用
- 洪小文:以科学的方式赤裸裸地剖析人工智能
- Android 如何查看apk安装包的AndroidManifest.xml文件
- Nikita and string
- MySql5.7.20解压版安装教程
- 华为生态链的全方位解读
- 用JavaScript实现九九乘法表
- 10秒一部电影,全球首个5G数据连接完成
- Android开发学习之路--MAC下Android Studio开发环境搭建