篇文章从代码角度去实现一个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 username
- private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
- // default connection password
- private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
- // default connection url
- private 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 send
- Session session = null;
- // message destination
- Destination destination = null;
- MessageProducer messageProducer = null;
- // create ConnectionFactory
- try {
- cf = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
- // create activemq connection
- connection = cf.createConnection();
- connection.start();
- // create session
- session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
- // create a queue name= helloworld
- destination = session.createQueue("helloworld");
- // create MessageProducer
- messageProducer = 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 block
- e.printStackTrace();
- } finally {
- try {
- connection.close();
- } catch (JMSException e) {
- // TODO Auto-generated catch block
- e.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 username
- private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
- // default connection password
- private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
- // default connection url
- private 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 block
- e.printStackTrace();
- }
- }
- }
Consumer执行完后
- message recieved:hello0
- message recieved:hello1
- message recieved:hello2
- message recieved:hello3
- message recieved:hello4
- message recieved:hello5
- message recieved:hello6
- message recieved:hello7
- message recieved:hello8
- message recieved:hello9
112

被折叠的 条评论
为什么被折叠?



