ActiveMQ之HelloWorld
来源:互联网 发布:csol大刀优化软件 编辑:程序博客网 时间:2024/06/05 16:12
本篇文章从代码角度去实现一个mq。因为ActiveMQ是对JMS的一种实现,因此,AMQ的开发步骤就应该和JMS的开发模型一样。
1. 创建ConnectionFactory
2. 创建Connection
3. 创建Session
4. 创建Destination/Topic
5. 创建Producer/Consumer
package com.zhuyang.mq.p2p;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Producer {// default connection usernameprivate static final String USERNAME = ActiveMQConnection.DEFAULT_USER;// default connection passwordprivate static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;// default connection urlprivate static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;public static void main(String[] args) {ConnectionFactory cf = null;Connection connection = null;// session used to revieve or sendSession session = null;// message destinationDestination destination = null;MessageProducer messageProducer = null;// create ConnectionFactorytry {cf = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);// create activemq connectionconnection = cf.createConnection();connection.start();// create sessionsession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);// create a queue name= helloworlddestination = session.createQueue("helloworld");// create MessageProducermessageProducer = session.createProducer(destination);for (int i = 0; i < 10; i++) {TextMessage msg = session.createTextMessage("hello" + i);messageProducer.send(msg);}session.commit();} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {connection.close();} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
运行完后active mq控制台会显示出创建的Queue"helloworld"
package com.zhuyang.mq.p2p;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Consumer {// default connection usernameprivate static final String USERNAME = ActiveMQConnection.DEFAULT_USER;// default connection passwordprivate static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;// default connection urlprivate static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;public static void main(String[] args) {ConnectionFactory cf = null;Connection connection = null;Session session = null;Destination destination = null;MessageConsumer messageConsumer;try {cf = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);connection = cf.createConnection();connection.start();session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);destination = session.createQueue("helloworld");messageConsumer = session.createConsumer(destination);while (true) {TextMessage msg = (TextMessage) messageConsumer.receive(100000);if (msg != null) {System.out.println("message recieved:" + msg.getText());} else {break;}}} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
Consumer执行完后
message recieved:hello0message recieved:hello1message recieved:hello2message recieved:hello3message recieved:hello4message recieved:hello5message recieved:hello6message recieved:hello7message recieved:hello8message recieved:hello9
0 0
- ActiveMQ之HelloWorld
- ActiveMQ之HelloWorld
- ActiveMQ学习笔记之HelloWorld
- ActiveMQ学习笔记之HelloWorld
- ActiveMQ HelloWorld
- ActiveMQ--HelloWorld
- ActiveMQ--HelloWorld
- 深入浅出JMS之ActiveMQ简单的HelloWorld实例
- (1) ActiveMQ --HelloWorld
- java Activemq的HelloWorld
- ActiveMQ 入门helloworld
- ActiveMQ简单的HelloWorld实例
- ActiveMQ简单的HelloWorld实例
- ActiveMQ简单的HelloWorld实例
- ActiveMQ简单的HelloWorld实例
- ActiveMQ简单的HelloWorld实例
- ActiveMQ简单的HelloWorld实例
- ActiveMQ简单的HelloWorld实例
- 自定义控件之简易Tab结构
- 腾讯认证QQ空间秒升级为专业版方法!
- zoj1200
- bash环境GIT仓库基本操作(2)
- Hadoop 伪分布式配置
- ActiveMQ之HelloWorld
- Mybatis中通过generator生成mapper、Dao、mapper.xml
- spring核心框架体系结构
- 15. 3Sum
- 模板引擎:beetl+servlet
- ViewPager 详解(三)—PagerTabStrip与PagerTitleStrip添加标题栏的异同
- java中PO,VO,DTO,BO,DAO,POJO
- Makefile(1)
- haproxy+keepalived实现高可用负载均衡