`
chenhua_1984
  • 浏览: 1246164 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

activemq

阅读更多

1activeMq是apache的一个子项目,支持jms1.1和j2ee1.4,主要实现了一个队列,用于网络间消息传输

 

2下载apache actionmq  

  http://www.apache.org/dyn/closer.cgi?path=%2Factivemq%2Fapache-activemq%2F5.5.0%2Fapache-activemq-5.5.0-bin.zip

 

3下载后的目录

 

 activemq-all-5.5.1.jar

 bin

 conf

 data

 docs

 example

 lib

 LICENSE

 NOTICE

 README.txt

 user-guide.html

 webapps

 WebConsole-README.txt

 

启动 mq,进入 bin目录,运行activemq.bat 文件,可以看到绑定的端口和侦听的端口,启动后访问地址

 

http://localhost:8161/

 

可以查看一些例子和查看控制台。这个时候,MQ的队列服务功能已经开启了。接下来需要编码实现

 

package com.mchz.mq.home;

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 org.apache.activemq.ActiveMQConnectionFactory;

public class SendMessage {
	private static final String url = "tcp://localhost:61616";;
	private static final String QUEUE_NAME = "send.queue";
	protected String expectedBody = "hello,word,172.16.4.106,hello,word,"
			+ "172.16.4.106,hello,word,172.16.4.106,hello,word,172.16.4.106,"
			+ "hello,word,172.16.4.106,hello,word,172.16.4.106,hello,word,"
			+ "172.16.4.106,hello,word,172.16.4.106,hello,word,172.16.4.106,"
			+ "hello,word,172.16.4.106,hello,word,172.16.4.106,hello,word,"
			+ "172.16.4.106,hello,word,172.16.4.106,hello,word,172.16.4.106,"
			+ "hello,word,172.16.4.106,hello,word,172.16.4.106,hello,word,"
			+ "172.16.4.106,hello,word,172.16.4.106,hello,word,172.16.4.106,"
			+ "hello,word,172.16.4.106,hello,word,172.16.4.106,";

	public void sendMessage() throws JMSException {

		Connection connection = null;
		Session session = null;
		try {
			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
					url);
			connection = connectionFactory.createConnection();

			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(QUEUE_NAME);
			MessageProducer producer = session.createProducer(destination);
			TextMessage message = session.createTextMessage(expectedBody);
			message.setStringProperty("ipaddress", "172.16.4.106");
			message.setStringProperty("address", "zj.hz.mc.219");
			producer.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
			connection.close();
		}
	}

	public static void main(String[] args) {

		SendMessage send = new SendMessage();
		long startTime=System.currentTimeMillis();
				
		try {
			for (int i = 0; i < 100000; i++) {
				send.sendMessage();
//				System.out.println(i);
			}
		} catch (JMSException e) {
			e.printStackTrace();
			long endTime=System.currentTimeMillis();
			System.out.println(endTime-startTime);
		}
		long endTime=System.currentTimeMillis();
		System.out.println(endTime-startTime);
	}

}
 

 

 

package com.mchz.mq.home;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ReceiveMessage {
	private static final String url = "tcp://localhost:61616";
	private static final String QUEUE_NAME = "send.queue";

	public void receiveMessage() {
		Connection connection = null;
		try {
			try {
				ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
						url);
				connection = connectionFactory.createConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}
			connection.start();
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(QUEUE_NAME);
			MessageConsumer consumer = session.createConsumer(destination);
			consumeMessagesAndClose(connection, session, consumer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void consumeMessagesAndClose(Connection connection,
			Session session, MessageConsumer consumer) throws JMSException {
		for (int i = 0; i < 16349;i++) {
			Message message = consumer.receive(1000);
			if (message != null) {
				onMessage(message);
			}
		}
		System.out.println("Closing connection");
		consumer.close();
		session.close();
		connection.close();
	}

	public void onMessage(Message message) {
		System.out.println("come in onMessage.....");

		try {
			if (message instanceof TextMessage) {
				TextMessage txtMsg = (TextMessage) message;
				String msg = txtMsg.getText();
				System.out.println("Received: " + msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		ReceiveMessage rm = new ReceiveMessage();
		rm.receiveMessage();
	}
}
 

 

一个用与发送消息,一个用于接收消息,先运行发送消息的类SendMessage ,然后查看MQ的控制 http://localhost:8161/admin/queues.jsp

可以看到自己往队列里面放的消息,接下来运行接收的类ReceiveMessage ,在刷新控制台,可以看到消息被接收。

 

另外,MQ还可以把一个队列里面的消息发送到另一个队列。

 

 

 

 

分享到:
评论

相关推荐

    activeMQ收发工具.rar

    ActiveMQ是中国最流行的开源消息中间件之一,由Apache软件基金会开发。它基于Java Message Service (JMS) 规范,提供了可靠的消息传递功能,适用于分布式系统中的应用间通信。本压缩包“activeMQ收发工具.rar”包含...

    ActiveMQ高并发处理方案

    ### ActiveMQ高并发处理方案详解 #### 一、引言 在现代分布式系统中,消息队列作为异步通信的核心组件之一,对于提高系统的吞吐量、降低响应时间和实现服务解耦等方面起着至关重要的作用。Apache ActiveMQ作为一款...

    apache-activemq-5.16.5

    Apache ActiveMQ是业界广泛使用的开源消息中间件,它基于Java消息服务(JMS)标准,提供了高度可扩展、可靠的异步通信能力。标题"apache-activemq-5.16.5"指的是该软件的一个特定版本,即5.16.5版本,通常每个新版本...

    apache-activemq-5.17.3

    Apache ActiveMQ是开源的、基于Java消息服务(JMS)的应用服务器,它是Apache软件基金会的一部分。这个名为"apache-activemq-5.17.3"的压缩包包含了ActiveMQ的5.17.3版本,这是一个稳定且功能丰富的发布版本。在深入...

    jmx监控activeMQ监控

    jmx监控ActiveMQ监控 jmx(Java Management Extensions)是一种Java技术,为Java应用程序提供了管理和监控的功能。ActiveMQ是Apache软件基金会下的一个开源消息队列系统,提供了高效、可靠的消息传递服务。在生产...

    Linux下activeMQ的启动和停止.docx

    在Linux环境下,Apache ActiveMQ是一个广泛使用的开源消息代理和队列服务器,它是Java Message Service (JMS) 的实现,能够处理大量的并发消息传递。ActiveMQ提供了高可用性、可扩展性和稳定性,使得它成为分布式...

    apache-activemq-5.15.9.rar

    Apache ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它基于Java Message Service (JMS) 规范,提供高效、可靠的消息传递服务。在本文中,我们将深入探讨Apache ActiveMQ,特别是针对“apache-activemq-...

    ActiveMQ路由配置方式

    ActiveMQ路由配置方式 ActiveMQ路由配置是Apache ActiveMQ项目中的一种重要配置方式,它依赖另一个Apache项目Camel。ActiveMQ集成了Camel,启动时同时会启动Camel。通过Camel Web Console可以进行Routing配置。 ...

    apache-activemq-5.9.0 下载

    Apache ActiveMQ是开源社区中最流行的消息中间件之一,它基于Java消息服务(JMS)标准,提供高效、可靠的异步通信解决方案。ActiveMQ在企业级应用中广泛应用,因为它支持多种协议,如OpenWire、STOMP、AMQP、MQTT、...

    Spring集成ActiveMQ配置

    Spring集成ActiveMQ是将Spring框架与ActiveMQ消息中间件相结合,实现异步处理和解耦应用程序的关键技术。在本文中,我们将深入探讨如何配置和使用这一组合,以及它在实际项目中的应用。 首先,让我们了解Spring框架...

    activemq自启动并设置用户名密码

    #### 一、ActiveMQ简介 ActiveMQ是一款非常流行的开源消息中间件,它基于Java语言开发,并且遵循了Java消息服务(JMS)规范。ActiveMQ提供了丰富的特性,包括持久化消息存储、事务支持、集群等功能。在企业级应用中,...

    ActiveMQ in Action pdf英文版+源代码

    ActiveMQ in Action pdf英文原版加源代码压缩包。 Apache ActiveMQ in Action is a thorough, practical guide to implementing message-oriented systems in Java using ActiveMQ. The book lays out the core of ...

    apache-activemq Linux版本

    Apache ActiveMQ是业界广泛使用的开源消息中间件,尤其在Linux环境下表现出色。它基于Java语言开发,遵循Apache软件基金会的许可证,并且实现了多种消息传递协议,包括OpenWire、STOMP、AMQP和XMPP等。在Linux系统上...

    自己实现的ActiveMQ连接池和新版本ActiveMQ自带的连接池,封装好的工具类,可直接使用

    本资源提供的内容是关于ActiveMQ的连接池实现,分为两部分:一是作者自己实现的ActiveMQ连接池,二是新版本ActiveMQ自带的连接池。连接池是一种资源管理技术,通过复用已建立的数据库连接或网络连接,减少创建和销毁...

    最新稳定版ActiveMQ5.15.0

    Apache ActiveMQ是开源社区中最流行的Java消息代理,也是企业级消息中间件(Message Broker)的首选之一。在最新的稳定版5.15.0中,它提供了可靠的消息传递功能,适用于分布式应用程序之间的通信,实现了异步处理、...

    springboot集成activemq实现消息接收demo

    而ActiveMQ是Apache出品的一款开源消息中间件,它遵循JMS(Java Message Service)规范,用于处理应用程序之间的异步通信。本教程将详细介绍如何在Spring Boot项目中集成ActiveMQ,实现消息接收的Demo。 首先,我们...

    springboot整合activemq案例

    在本文中,我们将深入探讨如何将Spring Boot与ActiveMQ整合,构建一个实用的消息传递系统。首先,我们需要了解Spring Boot和ActiveMQ的基本概念。 Spring Boot是一个快速开发框架,它简化了基于Spring的应用程序...

    apache-activemq-5.16.6-bin.zip

    Apache ActiveMQ是世界上最流行的开源消息代理和队列服务器,它基于Java消息服务(JMS)规范,提供高可用性、可扩展性和强大的消息传递功能。这个"apache-activemq-5.16.6-bin.zip"文件包含了ActiveMQ的最新稳定版本...

    apache-activemq-5.9.0-bin

    Apache ActiveMQ是世界上最流行的开源消息代理和队列服务器,它基于Java Message Service(JMS)规范,为分布式系统提供高效、可靠和可扩展的消息传递功能。这个“apache-activemq-5.9.0-bin”压缩包包含了Apache ...

    ActiveMQ使用手册(中文版)

    ### ActiveMQ 使用手册知识点概述 #### 一、ActiveMQ 原理与基本构件 **1.1 连接工厂(Connection Factory):** - **定义:** 连接工厂是客户端用来创建连接的对象。在ActiveMQ中,`ActiveMQConnectionFactory` 类...

Global site tag (gtag.js) - Google Analytics