ActiveMQ学习笔记之HelloWorld

1、首先去官方下载activeMQ,我用的版本是5.5.1

http://activemq.apache.org/download.html


2、打开压缩包,展现以下目录结构


3、打开bin目录,启动activemq.bat文件,开启activemq服务


4、访问http://localhost:8161/ 进入manager页面,创建一个队列。注意该队列名我们之后会在代码中用到。


5、开始写代码,先写一个Receiver.java 类,这个类的作用是监听一个IP和端口,接送信息。

package com.lvpeng.activemq.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

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

public class Receiver {
	public static void main(String[] args) {
		
		//链接工厂
		ConnectionFactory connectionFactory;
		//客户端到Provider的链接
		Connection connection = null;
		//一个发送或者接受消息的线程
		Session session;
		//消息目的地
		Destination destination;
		//消费者 消息接受着
		MessageConsumer consumer;
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD,
				"tcp://localhost:61616");
	
		try {
			//获取链接对象
			connection = connectionFactory.createConnection();
			//启动
			connection.start();
			//获取操作链接
			session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue("FirstQueue");
			consumer = session.createConsumer(destination);
			while(true){
				//设置接受者接受消息的时间 为了便于测试 这里谁定为10s
				TextMessage message = (TextMessage)consumer.receive(100000);
				if(null != message){
					System.out.println("收到消息:"+message.getText());
				}else{
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(null != connection)
					connection.close();
			} catch (Exception e) {
			}
		}
	}
}

6、然后再写一个sender.java 这个类的作用就是给我们制定的ip和端口发送消息。

package com.lvpeng.activemq.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

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

public class Sender {
	private static final int SEND_NUMBER = 5;
	public static void main(String[] args) {
		//连接工厂 JMS用他创建链接
		ConnectionFactory connectionFactory = null;
		//JMS客户端带哦Provider的链接
		Connection connection = null;
		//一个发送或者接受请求的线程
		Session session = null;
		//消息的目的地 消息发送给谁
		Destination destination = null;;
		//消息发送者
		MessageProducer producer = null;;
		//构造connectionFactory 此处采用ActiveMq的实现jar
		connectionFactory = new ActiveMQConnectionFactory(
				 ActiveMQConnection.DEFAULT_USER,
				 ActiveMQConnection.DEFAULT_PASSWORD,
				 "tcp://localhost:61616");
		try {
			//构造从工厂得到链接对象
			connection = connectionFactory.createConnection();
			//启动
			connection.start();
			//获取操作链接
			session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			//获取session注意参数值xingbo.xu-queue是一个服务的队列 需要在actionMQ的console配置
			destination = session.createQueue("FirstQueue");
			//得到消息生成者[发送者]
			producer = session.createProducer(destination);
			//设置不持久化 此处学习 实际根据项目决定
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			//构造消息 此处写死 项目就是参数 或者方法获取
			sendMessage(session,producer);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(null != connection)
					connection.close();
			} catch (Exception e) {
			}
		}
		
	}
	
	public static void sendMessage(Session session, MessageProducer producer) {
		try {
			for(int i=1;i<=SEND_NUMBER;i++){
				TextMessage message = session.createTextMessage("ActiveMQ 发送的消息 "+i);
				//发送消息到目的地
				System.out.println("发送消息:" + "ActiveMQ 发送的消息 "+i);
				producer.send(message);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}

7、执行sender.java 向服务器发送消息


发送消息:ActiveMQ 发送的消息 1
发送消息:ActiveMQ 发送的消息 2
发送消息:ActiveMQ 发送的消息 3
发送消息:ActiveMQ 发送的消息 4
发送消息:ActiveMQ 发送的消息 5

查看队列的变化:


8、启动Receiver.java 看是否可以接受到消息

收到消息:ActiveMQ 发送的消息 1
收到消息:ActiveMQ 发送的消息 2
收到消息:ActiveMQ 发送的消息 3
收到消息:ActiveMQ 发送的消息 4
收到消息:ActiveMQ 发送的消息 5


再次观察队列:


9、完成



10:、发布订阅模式参考网址:

http://www.open-open.com/lib/view/open1328079945062.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值