ActiveMQ入门

1. 新建maven工程
2. 引入jar包,本来是下载5.15.0,运行的时候,显示和JDK版本不一致,就版ActiveMQ的版本改的低一点

<dependency>
      <groupId>org.apache.activemq</groupId>
  	  <artifactId>activemq-all</artifactId>
      <version>5.11.1</version>
    </dependency>

3. 编写消息生产者

package com.nineclient.activemq;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.nineclient.util.ConnFactory;

public class JMSProducer {
	//发送的消息数量
	private static final int SENDNUM = 10;
	
	public static void main(String[] args) {
		//连接
		Connection connection = null;
		//会话,接受或者发送消息的线程
		Session session = null;
		//消息的目的地
		Destination destination = null;
		//消息的生产者
		MessageProducer messageProducer = null;
		
		try {
			//通过连接工厂获取连接
			connection = ConnFactory.getConnectionFactory().createConnection();
			//启动连接
			connection.start();
			//创建session
			session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
			//创建一个名称为HelloWorld消息队列
			destination = session.createQueue("HelloWorld");
			//创建生产者
			messageProducer = session.createProducer(destination);
			//发送消息
			sendMessage(session,messageProducer);
			session.commit();
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
		

	}
	
	public static void sendMessage(Session session, MessageProducer messageProducer) throws JMSException {
		for (int i=0; i<JMSProducer.SENDNUM; i++) {
			//创建一条文本消息
			TextMessage message = session.createTextMessage("ActiveMQ send message " + i);
			System.out.println("发送消息:ActiveMQ 发送消息 " + i);
			//通过消息生产者发出
			messageProducer.send(message);
		}
	}
	
	
}
package com.nineclient.util;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ConnFactory {
    //默认连接用户名
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	//默认连接密码
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	//默认连接地址
	private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
	
	private static ConnectionFactory connectionFactory = null;
	private ConnFactory () {
	}
	
	public static synchronized ConnectionFactory getConnectionFactory(){
		if(connectionFactory == null) {
			connectionFactory = new ActiveMQConnectionFactory(ConnFactory.USERNAME,ConnFactory.PASSWORD,ConnFactory.BROKERURL);
		}
		return connectionFactory;
	}
	
	

}
4. 编写消息消费者

package com.nineclient.activemq;

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 JMSConsumer {
	//默认连接用户名
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	//默认连接密码
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	//默认连接地址
	private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;

	public static void main(String[] args) {
		//连接工厂
		ConnectionFactory connectionFactory = null;
		//连接
		Connection connection = null;
		//会话 接受或者发送消息的线程
		Session session = null;
		//消息的目的地
		Destination destination = null;
		//消息的消费者
		MessageConsumer messageConsumer;
		//实例化连接工厂
		connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME,JMSConsumer.PASSWORD,JMSConsumer.BROKERURL);
		
		try {
			//通过连接工厂获取连接
			connection = connectionFactory.createConnection();
			//启动连接
			connection.start();
			//创建session
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			//创建一个连接HelloWorld的消息队列
			destination = session.createQueue("HelloWorld");
			//创建消息消费者
			messageConsumer = session.createConsumer(destination);
			
			while(true) {
				TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
				if(textMessage != null) {
					System.out.println("收到的消息 " + textMessage.getText());
				}
			}
			
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		
	}
}

运行测试
先启动ActiveMQ,双击bin目录下的activemq.bat即可,在浏览器里面输入http://localhost:8161/admin/,用户名和密码 admin/admin

运行生产者







运行消费者





至此,实例就结束了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值