ActiveMQ之HelloWorld

本篇文章从代码角度去实现一个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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值