|
1 |
| -# RabbitMQ AMQP 1.0 Python Client |
2 |
| - |
3 |
| -This library is in early stages of development. It is meant to be used with RabbitMQ 4.0. |
4 |
| - |
5 |
| -# Table of Contents |
6 |
| - |
7 |
| -- [How to Build the project and run the tests](#How-to-Build-the-project-and-run-the-tests) |
8 |
| -- [Installation](#Installation) |
9 |
| -- [Getting started](#Getting-Started) |
10 |
| - * [Creating a connection](#Creating-a-connection) |
11 |
| - * [Managing resources](#Managing-resources) |
12 |
| - * [Publishing messages](#Publishing-messages) |
13 |
| - * [Consuming messages](#Consuming-messages) |
14 |
| - * [Support for streams](#support-for-streams) |
15 |
| - * [SSL connection](#ssl-connections) |
16 |
| - * [Oauth authentication](#oauth-authentication) |
17 |
| - * [Managing disconnections](#Managing-disconnections) |
18 |
| - |
19 |
| - |
20 |
| -## How to Build the project and run the tests |
21 |
| - |
22 |
| -- Start a RabbitMQ 4.x broker |
23 |
| -- poetry build: build the source project |
24 |
| -- poetry install: resolves and install dependencies |
25 |
| -- poetry run pytest: run the tests |
26 |
| - |
27 |
| -## Installation |
| 1 | +## RabbitMQ AMQP 1.0 Python Client |
| 2 | +This library is meant to be used with RabbitMQ 4.0. Suitable for testing in pre-production environments. |
28 | 3 |
|
29 | 4 | The client is distributed via [`PIP`](https://pypi.org/project/rabbitmq-amqp-python-client/):
|
30 | 5 | ```bash
|
31 | 6 | pip install rabbitmq-amqp-python-client
|
32 | 7 | ```
|
33 | 8 |
|
34 |
| -## Getting Started |
35 |
| - |
36 |
| -An example is provided [`here`](./examples/getting_started/getting_started.py) you can run it after starting a RabbitMQ 4.0 broker with: |
37 |
| - |
38 |
| -poetry run python ./examples/getting_started/getting_started.py |
39 |
| - |
40 |
| -Also consider to have a look to the examples documented in the RabbitMQ website: |
41 |
| - |
42 |
| -https://www.rabbitmq.com/client-libraries/amqp-client-libraries |
43 |
| - |
44 |
| -### Creating a connection |
45 |
| - |
46 |
| -A connection to the RabbitMQ AMQP 1.0 server can be established using the Environment object. |
47 |
| - |
48 |
| -For example: |
49 |
| - |
50 |
| -```python |
51 |
| - environment = Environment("amqp://guest:guest@localhost:5672/") |
52 |
| - connection = environment.connection() |
53 |
| - connection.dial() |
54 |
| -``` |
55 |
| - |
56 |
| -### Managing resources |
57 |
| - |
58 |
| -Once we have a Connection object we can get a Management object in order to submit to the server management operations |
59 |
| -(es: declare/delete queues and exchanges, purging queues, binding/unbinding objects ecc...) |
60 |
| - |
61 |
| -For example (this code is declaring an exchange and a queue: |
62 |
| - |
63 |
| -```python |
64 |
| - management = connection.management() |
65 |
| - |
66 |
| - print("declaring exchange and queue") |
67 |
| - management.declare_exchange(ExchangeSpecification(name=exchange_name, arguments={})) |
68 |
| - |
69 |
| - management.declare_queue( |
70 |
| - QuorumQueueSpecification(name=queue_name) |
71 |
| - ) |
72 |
| -``` |
73 |
| - |
74 |
| -### Publishing messages |
75 |
| - |
76 |
| -Once we have a Connection object we can get a Publisher object in order to send messages to the server (to an exchange or queue) |
77 |
| - |
78 |
| -For example: |
79 |
| - |
80 |
| -```python |
81 |
| - addr_queue = AddressHelper.queue_address(queue_name) |
82 |
| - publisher = connection.publisher(addr) |
83 |
| - |
84 |
| - # publish messages |
85 |
| - for i in range(messages_to_publish): |
86 |
| - publisher.publish(Message(body="test")) |
87 |
| - |
88 |
| - publisher.close() |
89 |
| -``` |
90 |
| - |
91 |
| -### Consuming messages |
92 |
| - |
93 |
| -Once we have a Connection object we can get a Consumer object in order to consumer messages from the server (queue). |
94 |
| - |
95 |
| -Messages are received through a callback |
96 |
| - |
97 |
| -For example: |
98 |
| - |
99 |
| -Create a class which extends AMQPMessagingHandler which defines at minimum the on_consumer method, that will receive the |
100 |
| -messages consumed: |
101 |
| - |
102 |
| -```python |
103 |
| -class MyMessageHandler(AMQPMessagingHandler): |
104 |
| - |
105 |
| - def __init__(self): |
106 |
| - super().__init__() |
107 |
| - self._count = 0 |
108 |
| - |
109 |
| - def on_message(self, event: Event): |
110 |
| - print("received message: " + str(event.message.body)) |
111 |
| - |
112 |
| - # accepting |
113 |
| - self.delivery_context.accept(event) |
114 |
| -``` |
115 |
| - |
116 |
| -Then from connection get a consumer object: |
117 |
| - |
118 |
| -```python |
119 |
| - addr_queue = AddressHelper.queue_address(queue_name) |
120 |
| - consumer = connection.consumer(addr_queue, handler=MyMessageHandler()) |
121 |
| - |
122 |
| - try: |
123 |
| - consumer.run() |
124 |
| - except KeyboardInterrupt: |
125 |
| - pass |
126 |
| - |
127 |
| - consumer.close() |
128 |
| -``` |
129 |
| - |
130 |
| -The consumer will run indefinitively waiting for messages to arrive. |
131 |
| - |
132 |
| -### Support for streams |
133 |
| - |
134 |
| -The client natively supports streams: https://www.rabbitmq.com/blog/2021/07/13/rabbitmq-streams-overview |
135 |
| - |
136 |
| -You can consume from a given offset or specify a default starting point (FIRST, NEXT, LAST). |
137 |
| - |
138 |
| -Streams filtering is also supported: https://www.rabbitmq.com/blog/2023/10/16/stream-filtering |
139 |
| - |
140 |
| -You can check the [`stream example`](./examples/streams/example_with_streams.py) to see how to work with RabbitMQ streams. |
| 9 | +### Getting Started |
141 | 10 |
|
142 |
| -### SSL connections |
| 11 | +Inside the [examples](./examples) folder you can find a set of examples that show how to use the client. |
143 | 12 |
|
144 |
| -The client supports TLS/SSL connections. |
145 | 13 |
|
146 |
| -You can check the [`ssl example`](./examples/tls/tls_example.py) to see how to establish a secured connection |
| 14 | +### Documentation |
147 | 15 |
|
148 |
| -### Oauth authentication |
| 16 | +[Client Guide](https://www.rabbitmq.com/client-libraries/amqp-client-libraries) select the python section. |
149 | 17 |
|
150 |
| -The client supports oauth2 authentication. |
151 | 18 |
|
152 |
| -You can check the [`oauth2 example`](./examples/oauth/oaut.py) to see how to establish and refresh a connection using an oauth2 token |
| 19 | +### Build |
153 | 20 |
|
154 |
| -### Managing disconnections |
| 21 | +- `make rabbitmq-server`: run the RabbitMQ server in a docker container |
| 22 | +- `poetry build`: build the source project |
| 23 | +- `poetry install`: resolves and install dependencies |
| 24 | +- `make test`: run the tests |
155 | 25 |
|
156 |
| -The client supports automatic reconnection with the ability to reconnect Managements, Producers and Consumers |
| 26 | +Note for MAC users: |
| 27 | +- TLS does not work, see: https://github.com/rabbitmq/rabbitmq-amqp-python-client/issues/64 |
157 | 28 |
|
158 |
| -You can check the [`reconnection example`](./examples/reconnection/reconnection_example.py) to see how to manage disconnections |
159 | 29 |
|
160 | 30 |
|
161 | 31 |
|
|
0 commit comments