@@ -11,8 +11,97 @@ This library is in early stages of development. It is meant to be used with Rabb
11
11
12
12
## Getting Started
13
13
14
- An example is provide in ./getting_started_main.py you can run it after starting a RabbitMQ 4.0 broker with:
14
+ An example is provided in ./getting_started_main.py you can run it after starting a RabbitMQ 4.0 broker with:
15
15
16
16
poetry run python ./examples/getting_started/main.py
17
17
18
+ ### Creating a connection
19
+
20
+ A connection to the RabbitMQ AMQP 1.0 server can be established using the Connection object.
21
+
22
+ For example:
23
+
24
+ ``` python
25
+ connection = Connection(" amqp://guest:guest@localhost:5672/" )
26
+ connection.dial()
27
+ ```
28
+
29
+ ### Managing resources
30
+
31
+ Once we have a Connection object we can get a Management object in order to submit to the server management operations
32
+ (es: declare/delete queues and exchanges, purging queues, binding/unbinding objects ecc...)
33
+
34
+ For example (this code is declaring an exchange and a queue:
35
+
36
+ ``` python
37
+ management = connection.management()
38
+
39
+ print (" declaring exchange and queue" )
40
+ management.declare_exchange(ExchangeSpecification(name = exchange_name, arguments = {}))
41
+
42
+ management.declare_queue(
43
+ QuorumQueueSpecification(name = queue_name)
44
+ )
45
+ ```
46
+
47
+ ### Publishing messages
48
+
49
+ 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)
50
+
51
+ For example:
52
+
53
+ ``` python
54
+ addr_queue = AddressHelper.queue_address(queue_name)
55
+ publisher = connection.publisher(addr)
56
+
57
+ # publish messages
58
+ for i in range (messages_to_publish):
59
+ publisher.publish(Message(body = " test" ))
60
+
61
+ publisher.close()
62
+ ```
63
+
64
+ ### Consuming messages
65
+
66
+ Once we have a Connection object we can get a Consumer object in order to consumer messages from the server (queue).
67
+
68
+ Messages are received through a callback
69
+
70
+ For example:
71
+
72
+ Create a class which extends AMQPMessagingHandler which defines at minimum the on_consumer method, that will receive the
73
+ messages consumed:
74
+
75
+ ``` python
76
+ class MyMessageHandler (AMQPMessagingHandler ):
77
+
78
+ def __init__ (self ):
79
+ super ().__init__ ()
80
+ self ._count = 0
81
+
82
+ def on_message (self , event : Event):
83
+ print (" received message: " + str (event.message.body))
84
+
85
+ # accepting
86
+ self .delivery_context.accept(event)
87
+ ```
88
+
89
+ Then from connection get a consumer object:
90
+
91
+ ``` python
92
+ addr_queue = AddressHelper.queue_address(queue_name)
93
+ consumer = connection.consumer(addr_queue, handler = MyMessageHandler())
94
+
95
+ try :
96
+ consumer.run()
97
+ except KeyboardInterrupt :
98
+ pass
99
+
100
+ consumer.close()
101
+ ```
102
+
103
+ The consumer will run indefinitively waiting for messages to arrive.
104
+
105
+
106
+
18
107
0 commit comments