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