14.客户端

予早 2025-02-21 01:08:23
Categories: Tags:

Get started

https://www.rabbitmq.com/tutorials

https://www.rabbitmq.com/client-libraries/devtools

code examples:

pika

https://github.com/pika/pika

https://github.com/pika/pika/tree/main/examples

https://pika.readthedocs.io/en/stable/intro.html

Is Pika thread safe?

Pika does not have any notion of threading in the code. If you want to use Pika with threading, make sure you have a Pika connection per thread, created in that thread. It is not safe to share one Pika connection across threads, with one exception: you may call the connection method add_*caback_*threadsafe from another thread to schedule a callback within an active pika connection. See connection adapter for more details.

kombu

https://github.com/celery/kombu

https://docs.celeryq.dev/projects/kombu/en/stable/

pika or kombu

https://stackoverflow.com/questions/48524536/can-anyone-please-tell-me-what-are-the-differences-between-pika-and-kombu-messag

https://zhuanlan.zhihu.com/p/42859874

其他

Java的mq封装

https://blog.csdn.net/lihuajian1024/article/details/123004957

https://blog.csdn.net/wangc_gogo/article/details/97363550

pika_pool

import json

import pika

import pika_pool_utils


def connection_factory():
    credentials = pika.PlainCredentials(username='sync_test',
                                        password='sync_test')
    parameters = pika.ConnectionParameters(credentials=credentials,
                                           host='192.168.1.211',
                                           port=5673,
                                           virtual_host='/sync_test')
    return pika.BlockingConnection(parameters)


pool = pika_pool_utils.QueuedPool(
    create=connection_factory,
    recycle=45,
    max_size=10,
    max_overflow=10,
    timeout=10,
)

with pool.acquire() as cxn:
    cxn.channel.basic_publish(
        body=json.dumps({'type': 'banana', 'color': 'yellow'}),
        exchange='exchange',
        routing_key='banana',
    )

Java

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerTest {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
        // 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码
        factory.setHost("192.168.25.128");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("rabbit");
        factory.setPassword("rabbit123456");
        // 1.2.建立连接
        Connection connection = factory.newConnection();

        // 2.创建通道Channel
        Channel channel = connection.createChannel();

        // 3.创建队列
        String queueName = "simple.queue";
        channel.queueDeclare(queueName, false, false, false, null);

        // 4.订阅消息
        channel.basicConsume(queueName, true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 5.处理消息
                String message = new String(body);
                System.out.println("接收到消息:【" + message + "】");
            }
        });
        System.out.println("等待接收消息。。。。");
    }
}
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class PublisherTest {
    @Test
    public void testSendMessage() throws IOException, TimeoutException {
        // 1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
        // 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码
        factory.setHost("192.168.25.128");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("rabbit");
        factory.setPassword("rabbit123456");
        // 1.2.建立连接
        Connection connection = factory.newConnection();

        // 2.创建通道Channel
        Channel channel = connection.createChannel();

        // 3.创建队列
        String queueName = "simple.queue";
        channel.queueDeclare(queueName, false, false, false, null);

        // 4.发送消息
        String message = "hello, rabbitmq!";
        channel.basicPublish("", queueName, null, message.getBytes());
        System.out.println("发送消息成功:【" + message + "】");

        // 5.关闭通道和连接
        channel.close();
        connection.close();

    }
}