package com.apc.external.model.monitoring.hp; import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.security.cert.X509Certificate; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; import com.rabbitmq.client.AMQP.Queue.DeclareOk; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.DefaultSaslConfig; import com.rabbitmq.client.GetResponse; public class SCMBTest { public static void main(String arg[]) throws Exception { //c://MyKeyStore contains client certificate and private key. Load it into Java Keystore final char[] keyPassphrase = "CoolingCalc2".toCharArray(); final KeyStore ks = KeyStore.getInstance("jks"); ks.load(new FileInputStream("C:\\HP\\certificate\\MyKeyStore"), keyPassphrase); final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, keyPassphrase); //c://MyTrustStore contains CA certificate. Load it into Java Trust Store final char[] trustPassphrase = "CoolingCalc2".toCharArray(); final KeyStore tks = KeyStore.getInstance("jks"); tks.load(new FileInputStream("C:\\HP\\certificate\\truststore"), trustPassphrase); final TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(tks); TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; //load SSLContext with keystore and truststore. final SSLContext c = SSLContext.getInstance("SSL"); c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom()); final ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.169.115.205"); //Set Auth mechanism to "EXTERNAL" so that commonName of the client certificate is mapped to AMQP user name. Hence, No need to set userId/Password here. factory.setSaslConfig(DefaultSaslConfig.EXTERNAL); factory.setPort(5671); factory.useSslProtocol(c); final Connection conn = factory.newConnection(); final Channel channel = conn.createChannel(); //do not specify queue name. AMQP will create a queue with random name starting with amq.gen* e.g. amq.gen-32sfQz95QJ85K_lMBhU6HA final DeclareOk queue = channel.queueDeclare("", true, false, true, null); //Now get the queue name from above call and bind it to required Exchange with required routing key. channel.queueBind(queue.getQueue(), "scmb", "scmb.#"); //Now you should be able to receive messages from queue final GetResponse chResponse = channel.basicGet(queue.getQueue(), false); if (chResponse == null) { System.out.println("No message retrieved"); } else { final byte[] body = chResponse.getBody(); System.out.println("Received: " + new String(body)); } channel.close(); conn.close(); } }