Distributed Message Service RabbitMQ

Code Examples

2024-06-27 07:03:01

Secure access point (PLAIN and AMQPLAIN authorization mechanisms)

import com.rabbitmq.client.*;
import java.io.IOException;
 
public class RabbitmqAmqpDemo {
    public static void main(String[] args) throws Exception {
        String host = "192.168.0.0"; //secure access point ip
        Integer port = 5672;        //secure access point port
        String username = "xxx";       //username of the cluster management user list
        String password = "xxx";       //Password for cluster management user list
        String vhost = "/";
        String exchangeName = "ex_test";
        String queueName = "qu_test";
 
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
 
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, "test");
 
        String message = "Hello Aop";
        for (int i = 0; i < 10; i++) {
            channel.basicPublish(exchangeName, "test", null, message.getBytes());
            System.out.println("MessageSentSuccessfully");
        }
        Channel consumeChannel = connection.createChannel();
        Consumer consumer = new DefaultConsumer(consume

SSL access point (EXTERNAL authorization mechanism)

import com.rabbitmq.client.*;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
 
public class RabbitmqExternalDemo {
    public static void main(String[] args) throws Exception {
        String host = "192.168.0.0"; //SSL access point ip
        int port = 5671;    //SSL access point port
        //The following 2 SSL files can be used to obtain the installation package through the console. For specific obtaining methods, see the second section of 2.2.1 Access Steps.
        String ksFile = "D:\\tmp\\ssl\\client_rabbitmq_key.p12";
        String tksFile = "D:\\tmp\\ssl\\truststore";
        String vhost = "/";
        String exchangeName = "ex_test";
        String queueName = "qu_test";
 
        char[] keyPassphrase = "W3zT_98Zz9Io".toCharArray();
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(new FileInputStream(ksFile), keyPassphrase);
 
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, keyPassphrase);
 
        char[] trustPassphrase = null;
        trustPassphrase = "W3zT_98Zz9Io".toCharArray();
        KeyStore tks = KeyStore.getInstance("JKS");
        tks.load(new FileInputStream(tksFile), trustPassphrase);


oQvnzGkA7bv0