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

标签: AMQPQPID
760人阅读 评论(0) 收藏 举报
分类:

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>




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:385085次
    • 积分:6337
    • 等级:
    • 排名:第4069名
    • 原创:227篇
    • 转载:0篇
    • 译文:5篇
    • 评论:157条
    博客专栏