[3]AMQP(高级消息队列协议) ----QPID JMS-0.9 API 对 SSL 的支持

原创 2016年07月02日 23:46:27

QPID JMS-0.9 Java的API 版本只支持 AMQP-1-0的协议,那么如何用QPID JMS-0.9 Java的API 去调用QPID的基于SSL的端口的服务呢。一般来说有两种方式,后面将会给出第一种方式的具体代码.


#1 直接设置相应的属性

String truststore_File_Path="D:/QpidSSL/clientts.jks";
String truststore_password="123456";
String keystore_File_Path="D:/QpidSSL/clientks.jks";
String keystore_Password="123456";
System.setProperty("javax.net.ssl.keyStore",keystore_File_Path); 
System.setProperty("javax.net.ssl.keyStorePassword",keystore_Password); 
System.setProperty("javax.net.ssl.trustStore",truststore_File_Path); 
System.setProperty("javax.net.ssl.trustStorePassword",truststore_password); 



#2 通过QPID JMS-0.9 Java的客户端参数配置支持。具体可以参考面的这些参数(具体请参考https://qpid.apache.org/releases/qpid-jms-0.9.0/docs/index.html)

transport.keyStoreLocation default is to read from the system property "javax.net.ssl.keyStore"
transport.keyStorePassword default is to read from the system property "javax.net.ssl.keyStorePassword"
transport.trustStoreLocation default is to read from the system property "javax.net.ssl.trustStore"
transport.trustStorePassword default is to read from the system property "javax.net.ssl.keyStorePassword"
transport.storeType The type of trust store being used. Default is "JKS".
transport.contextProtocol The protocol argument used when getting an SSLContext. Default is "TLS".
transport.enabledCipherSuites The cipher suites to enable, comma separated. No default, meaning the context default ciphers are used. Any disabled ciphers are removed from this.
transport.disabledCipherSuites The cipher suites to disable, comma separated. Ciphers listed here are removed from the enabled ciphers. No default.
transport.enabledProtocols The protocols to enable, comma separated. No default, meaning the context default protocols are used. Any disabled protocols are removed from this.
transport.disabledProtocols The protocols to disable, comma separated. Protocols listed here are removed from the enabled protocols. Default is "SSLv2Hello,SSLv3".
transport.trustAll Whether to trust the provided server certificate implicitly, regardless of any configured trust store. Defaults to false.
transport.verifyHost Whether to verify that the hostname being connected to matches with the provided server certificate. Defaults to true.
transport.keyAlias The alias to use when selecting a keypair from the keystore if required to send a client certificate to the server. No default.


下面假设我们的KeyStore和TrustStore的相关证书都保存在了相应的JKS文件里面

<span style="font-size:14px;">String truststore_File_Path="D:/QpidSSL/clientts.jks";
String truststore_password="123456";
String keystore_File_Path="D:/QpidSSL/clientks.jks";
String keystore_Password="123456";</span>


则 客户端SSL的调用非常的简单. 结合上一章节( [2]AMQP(高级消息队列协议) ----QPID不得不说的事http://blog.csdn.net/chancein007/article/details/51813218)的Sender.java的例子,具体代码如下:

<span style="font-size:14px;">/*
 *
 * 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() {
		String truststore_File_Path="D:/QpidSSL/clientts.jks";
		String truststore_password="123456";
		String keystore_File_Path="D:/QpidSSL/clientks.jks";
		String keystore_Password="123456";
		System.setProperty("javax.net.ssl.keyStore",keystore_File_Path); 
		System.setProperty("javax.net.ssl.keyStorePassword",keystore_Password); 
		System.setProperty("javax.net.ssl.trustStore",truststore_File_Path); 
		System.setProperty("javax.net.ssl.trustStorePassword",truststore_password); 
		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);
		}
	}
}</span>




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

[4]AMQP(高级消息队列协议) ----改造QPID JMS-0.9 API 对 SSLContext的 支持

默认情况下QPID JMS-0.9 API对SSLConext没有提供相应的接口. 但是在笔者项目中,又需要其对SSLContext对象的支持。这个问问深深的难住了笔者2~3天时间,笔者通过深入的研究...

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

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

Python通过amqp消息队列协议中的Qpid实现数据通信

简介:     这两天看了消息队列通信,打算在配置平台上应用起来。以前用过zeromq但是这东西太快了,还有就是rabbitmq有点大,新浪的朋友推荐了qpid,简单轻便。自己总结了下文档,大家...
  • Raysen
  • Raysen
  • 2014年08月08日 02:05
  • 1009

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

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

[5]AMQP(高级消息队列协议) ----如何创建自己的SSLContext对象

在[4]AMQP(高级消息队列协议) ----改造QPID JMS-0.9 API 对 SSLContext的 支持(http://blog.csdn.net/chancein007/article/...

消息队列之JMS和AMQP对比

AMQP & JMS对比 初次接触消息队列时,在网上搜索,总是会提到如JMS、AMQP等一些术语。查看了一些文档,对JMS和AMQP的一些理解记录如下。 JMS         ...

消息队列之JMS和AMQP对比

消息队列之JMS和AMQP对比 JMS AMQP 对比AMQP和JMS消息队列之JMS和AMQP对比JMS通常而言提到JMS(Java MessageService)实际上是指JMS API。JMS是...

消息队列之JMS和AMQP对比

from:http://blog.csdn.net/hpttlook/article/details/23391967 AMQP & JMS对比 初次接触消息队列时,在网上搜索...
  • bloomzy
  • bloomzy
  • 2015年04月08日 08:28
  • 393

消息队列之JMS和AMQP对比

AMQP & JMS对比 初次接触消息队列时,在网上搜索,总是会提到如JMS、AMQP等一些术语。查看了一些文档,对JMS和AMQP的一些理解记录如下。 JMS          通常而言提到J...
  • thjnemo
  • thjnemo
  • 2015年03月11日 14:19
  • 409

消息队列之JMS和AMQP对比

AMQP & JMS对比 初次接触消息队列时,在网上搜索,总是会提到如JMS、AMQP等一些术语。查看了一些文档,对JMS和AMQP的一些理解记录如下。 JMS          通常而言提到J...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[3]AMQP(高级消息队列协议) ----QPID JMS-0.9 API 对 SSL 的支持
举报原因:
原因补充:

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