-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart_amqp_client_example.dart
52 lines (43 loc) · 1.56 KB
/
dart_amqp_client_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'package:dart_amqp_client/dart_amqp_client.dart';
void main() {
// Create a ConnectionSettings instance with your AMQP server details.
ConnectionSettings connectionSettings = ConnectionSettings(
host: 'localhost',
port: 5672,
maxConnectionAttempts: 5, // Set the maximum connection attempts
);
// Create an instance of the AMQP service.
AmqpService(connectionSettings);
// Set callback functions for connection events.
AmqpService.onConnected(() {
print('Connected to AMQP server.');
// Use the service to establish an AMQP connection.
Client client = AmqpService.getClient();
// ... Perform AMQP operations with the client and channel ...
String consumeTag = "hello";
String msg = "Hello World!";
client.channel().then((Channel channel) {
return channel.queue(consumeTag, durable: false);
}).then((Queue queue) {
queue.publish(msg);
print(" [x] Sent $msg");
client.close();
});
String queueTag = "hello";
Channel? channel = AmqpService.getChannel();
channel!.queue(queueTag, durable: false).then((Queue queue) {
print(" [*] Waiting for messages in $queueTag. To Exit press CTRL+C");
return queue.consume(consumerTag: queueTag, noAck: true);
}).then((Consumer consumer) {
consumer.listen((AmqpMessage event) {
print(" [x] Received ${event.payloadAsString}");
});
});
});
AmqpService.onDisconnected(() {
print('Disconnected from AMQP server.');
});
AmqpService.onError((Exception error) {
print('An error occurred: $error');
});
}