[2]AMQP(高级消息队列协议) ----QPID不得不说的事

原创 2016年07月02日 23:35:54

如果说到AMQP协议,则不得不提的就是QPID。QPID的论坛现在十分的活跃,基本上白天提的问题,晚上马上就能得到回复。由此可见QPID的活跃程度。

大家可以到http://qpid.2158936.n2.nabble.com/网站上面注册一个账号,真的非常的好,吐血推荐给大家。但是要求大家的因为一定要好。下面的这个例子就是我们当时提的http://qpid.2158936.n2.nabble.com/connect-Qpid-broker-using-the-latest-amqp-1-0-qpid-jms-client-0-9-ssl-td7644406.html


另外,QPID的JIRA系统上面也能报Bug,这个bug就是我们当时报的: https://issues.apache.org/jira/browse/QPIDJMS-183,回复这么迅速,作为一个开源项目还是挺给力的。


关于具体的QPID的介绍,请大家参考我上一篇文章,而且安装方式网络上也有很多,大家可以自己去找。其官方网站为http://qpid.apache.org/ 文档的地址为
http://qpid.apache.org/documentation.html


因为QPID的服务器支持AMQP-1-0协议,也支持AQMP0-9等协议。所以其最新的客户端的API的地址为:

(1)qpid-java-6.0.4 支持AMQP 0-10, 0-9-1, 0-9, 0-8

https://qpid.apache.org/releases/qpid-java-6.0.4/


 (2) qpid-jms-0.9 支持AMQP 1-0 协议


初学者很容易混淆上面两种最新的API的区别.

下面给一个简单的基于qpid-jms-0.9 客户端Java API 的发送和接受消息的例子。下面的例子我是基于QPID本身提供的Sample的一个改造。借花献佛,希望对大家有所帮助。

下图是我项目代码的基本结构


(1) hello.properties的文件配置为

java.naming.factory.initial = org.apache.qpid.jms.jndi.JmsInitialContextFactory
connectionfactory.myFactoryLookup =  amqp://192.168.45.111:5710?jms.username=admin&jms.password=admin&transport.connectTimeout=30000
queue.queue=queue_henry
queue.myQueueLookup=queue_henry

(2) Sender.java的代码

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.jms.example.success;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Sender {
	private static final String USER = "guest";
	private static final String PASSWORD = "guest";
	private static final int DEFAULT_COUNT = 10;
	private static final int DELIVERY_MODE = DeliveryMode.NON_PERSISTENT;

	private Context getContext() {
		InitialContext context = null;
		try {
			InputStream resourceAsStream = this.getClass().getResourceAsStream("hello.properties");
			Properties properties = new Properties();
			properties.load(resourceAsStream);
			context = new InitialContext(properties);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return context;

	}

	public void publish(int count) {
		try {
			// The configuration for the Qpid InitialContextFactory has been
			// supplied in
			// a jndi.properties file in the classpath, which results in it
			// being picked
			// up automatically by the InitialContext constructor.
			Context context = this.getContext();

			ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup");
			Destination queue = (Destination) context.lookup("myQueueLookup");

			Connection connection = factory.createConnection(USER, PASSWORD);
			connection.setExceptionListener(new MyExceptionListener());
			connection.start();

			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

			MessageProducer messageProducer = session.createProducer(queue);

			long start = System.currentTimeMillis();
			for (int i = 1; i <= count; i++) {
				TextMessage message = session.createTextMessage("Text!");
				messageProducer.send(message, DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);

				if (i % 100 == 0) {
					System.out.println("Sent message " + i);
				}
			}

			long finish = System.currentTimeMillis();
			long taken = finish - start;
			System.out.println("Sent " + count + " messages in " + taken + "ms");

			connection.close();
		} catch (Exception exp) {
			System.out.println("Caught exception, exiting.");
			exp.printStackTrace(System.out);
			System.exit(1);
		}
	}

	public static void main(String[] args) throws Exception {
		int count = DEFAULT_COUNT;
		if (args.length == 0) {
			System.out.println("Sending up to " + count + " messages.");
			System.out
					.println("Specify a message count as the program argument if you wish to send a different amount.");
		} else {
			count = Integer.parseInt(args[0]);
			System.out.println("Sending up to " + count + " messages.");
		}
		Sender sender=new Sender();
		sender.publish(count);

	}

	private static class MyExceptionListener implements ExceptionListener {
		@Override
		public void onException(JMSException exception) {
			System.out.println("Connection ExceptionListener fired, exiting.");
			exception.printStackTrace(System.out);
			System.exit(1);
		}
	}
}

(3) Receiver.java的代码

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.jms.example.success;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Receiver {
	private static final String USER = "guest";
	private static final String PASSWORD = "guest";
	private static final int DEFAULT_COUNT = 10;

	private Context getContext() {
		InitialContext context = null;
		try {
			InputStream resourceAsStream = this.getClass().getResourceAsStream("hello.properties");
			Properties properties = new Properties();
			properties.load(resourceAsStream);
			context = new InitialContext(properties);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return context;

	}

	public void receiveMessage(int count) {
		try {
			// The configuration for the Qpid InitialContextFactory has been
			// supplied in
			// a jndi.properties file in the classpath, which results in it
			// being picked
			// up automatically by the InitialContext constructor.

			Context context = this.getContext();//new InitialContext();

			ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup");
			Destination queue = (Destination) context.lookup("myQueueLookup");

			Connection connection = factory.createConnection(USER, PASSWORD);
			connection.setExceptionListener(new MyExceptionListener());
			connection.start();

			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

			MessageConsumer messageConsumer = session.createConsumer(queue);

			long start = System.currentTimeMillis();

			int actualCount = 0;
			boolean deductTimeout = false;
			int timeout = 1000;
			for (int i = 1; i <= count; i++, actualCount++) {
				Message message = messageConsumer.receive(timeout);
				if (message == null) {
					System.out.println("Message " + i + " not received within timeout, stopping.");
					deductTimeout = true;
					break;
				}
				if (i % 100 == 0) {
					System.out.println("Got message " + i);
				}
				System.out.println("The message:"+message.getJMSDestination().toString());
			}

			long finish = System.currentTimeMillis();
			long taken = finish - start;
			if (deductTimeout) {
				taken -= timeout;
			}
			System.out.println("Received " + actualCount + " messages in " + taken + "ms");

			connection.close();
		} catch (Exception exp) {
			System.out.println("Caught exception, exiting.");
			exp.printStackTrace(System.out);
			System.exit(1);
		}
	}

	public static void main(String[] args) throws Exception {
		int count = DEFAULT_COUNT;
		if (args.length == 0) {
			System.out.println("Consuming up to " + count + " messages.");
			System.out.println(
					"Specify a message count as the program argument if you wish to consume a different amount.");
		} else {
			count = Integer.parseInt(args[0]);
			System.out.println("Consuming up to " + count + " messages.");
		}
		Receiver rec=new Receiver();
		rec.receiveMessage(count);
	}

	private static class MyExceptionListener implements ExceptionListener {
		@Override
		public void onException(JMSException exception) {
			System.out.println("Connection ExceptionListener fired, exiting.");
			exception.printStackTrace(System.out);
			System.exit(1);
		}
	}
}







版权声明:本文为博主原创文章,未经博主允许不得转载。

AMQP之QPID和RabbitMQ

基础 高级消息队列协议(AMQP1)是一个异步消息传递所使用的应用层协议规范,是一个线路层协议,而不是API,不能被开发者直接使用,它的客户端能够无视消息的来源任意发送和接受信息,AMQP的原始用途...
  • maoyeqiu
  • maoyeqiu
  • 2017年04月24日 11:42
  • 667

[AMQP] QPID安装与测试

安装依赖项 apache qpidc安装: 必须安装: * boost      http://www.boost.org>                             ...
  • Xuguang121
  • Xuguang121
  • 2012年05月24日 11:29
  • 3664

【消息队列MQ】各类MQ比较

目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常...
  • sunxinhere
  • sunxinhere
  • 2012年09月11日 21:22
  • 281617

大型网站架构之分布式消息队列

大型网站架构之分布式消息队列   以下是消息队列以下的大纲,本文主要介绍消息队列概述,消息队列应用场景和消息中间件示例(电商,日志系统)。 本次分享大纲 消息队列概述消息队列应用场景消息中...
  • shaobingj126
  • shaobingj126
  • 2016年01月26日 08:48
  • 122466

amqp高级消息队列协议

  • 2013年11月05日 19:59
  • 709KB
  • 下载

[1]AMQP(高级消息队列协议) ----入门

接触AMQP协议已经有一段时间了,这个协议比想象的中的要复杂一些,特别是针对AMQP-1-0以前的版本。具体的概念,网上可以搜索到很多的资料,本文借花献佛,让读者对AMQP协议有一个大概的了解。重点是...
  • chancein007
  • chancein007
  • 2016年07月02日 23:09
  • 2543

大型网站架构系列:分布式消息队列

大型网站架构系列:分布式消息队列 一、消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和...
  • chenglinhust
  • chenglinhust
  • 2016年06月09日 22:40
  • 1050

MFC edit控件实现自动换行

必须设置Vertical Scrollbar = TRUE 必须设置MultiLine = TRUE 必须设置Horizontal Scrollbar = FALSE 必须设置Auto HScr...
  • a200638012
  • a200638012
  • 2016年07月08日 17:22
  • 4794

设置MFC Edit control可以换行显示

MFC Edit control可以换行显示 1.设置edit control 属性Multiline为true 2.写入数据时,想换行的时候写入"\r\n"即可,CString str.Re...
  • a780902723
  • a780902723
  • 2014年12月09日 16:52
  • 2470

MFC中EDIT控件实现换行

一、通过回车换行: 这里要有两个设置 .将控件的属性设置为Mutilines->true; .将控件的另一个属性设置为Want return->true. 水平和垂直滚动条设置...
  • dearwind153
  • dearwind153
  • 2015年12月09日 21:13
  • 3893
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[2]AMQP(高级消息队列协议) ----QPID不得不说的事
举报原因:
原因补充:

(最多只允许输入30个字)