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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
|
package Mojo::RabbitMQ::Client::Channel;
use Mojo::Base 'Mojo::EventEmitter';
use Mojo::Promise;
use Mojo::RabbitMQ::Client::LocalQueue;
use Mojo::RabbitMQ::Client::Method;
use Mojo::RabbitMQ::Client::Method::Publish;
use Scalar::Util qw(isweak weaken);
use constant DEBUG => $ENV{MOJO_RABBITMQ_DEBUG} // 0;
has id => 0;
has is_open => 0;
has is_active => 0;
has client => undef;
has queue => sub { Mojo::RabbitMQ::Client::LocalQueue->new };
has content_queue => sub { Mojo::RabbitMQ::Client::LocalQueue->new };
has consumer_cbs => sub { {} };
has return_cbs => sub { {} };
sub _open {
warn "Deprecated call to _open on channel";
return shift->open(@_);
}
sub open {
my $self = shift;
if ($self->is_open) {
$self->emit(error => 'Channel has already been opened');
return $self;
}
weaken $self;
$self->client->_write_expect(
'Channel::Open' => {},
'Channel::OpenOk' => sub {
warn "-- Channel::OpenOk\n" if DEBUG;
$self->is_open(1)->is_active(1)->emit('open');
},
sub {
$self->emit(
error => 'Invalid response received while trying to open channel: '
. shift);
},
$self->id,
);
return $self;
}
sub _push_queue_or_consume {
my $self = shift;
my ($frame) = @_;
weaken $self;
if ($frame->isa('Net::AMQP::Frame::Method')) {
my $method_frame = $frame->method_frame;
if ($method_frame->isa('Net::AMQP::Protocol::Channel::Close')) {
$self->client->_write_frame(Net::AMQP::Protocol::Channel::CloseOk->new(),
$self->id);
$self->is_open(0)->is_active(0);
$self->client->delete_channel($self->id);
$self->emit(close => $frame);
return $self;
}
elsif ($method_frame->isa('Net::AMQP::Protocol::Basic::Deliver')) {
my $cb = $self->consumer_cbs->{$method_frame->consumer_tag} || sub { };
$self->_push_read_header_and_body(
'deliver',
$frame => sub {
$cb->emit(message => @_);
},
sub {
$self->emit(error => 'Consumer callback failure: ' . shift);
}
);
return $self;
}
elsif ($method_frame->isa('Net::AMQP::Protocol::Basic::Return')) {
my $cb
= $self->return_cbs->{$method_frame->exchange . '_'
. $method_frame->routing_key}
|| sub { };
$self->_push_read_header_and_body(
'return',
$frame => sub {
$cb->emit(reject => @_);
},
sub {
$self->emit(error => 'Return callback failure: ' . shift);
}
);
return $self;
}
elsif ($method_frame->isa('Net::AMQP::Protocol::Channel::Flow')) {
$self->is_active($method_frame->active);
$self->client->_write_frame(
Net::AMQP::Protocol::Channel::FlowOk->new(
active => $method_frame->active
),
$self->id
);
return $self;
}
$self->queue->push($frame);
}
else {
$self->content_queue->push($frame);
}
return $self;
}
sub close {
my $self = shift;
my $connection = $self->client or return;
return $self if !$self->is_open;
return $self->_close() if 0 == scalar keys %{$self->consumer_cbs};
for my $consumer_tag (keys %{$self->consumer_cbs}) {
my $method = $self->cancel(consumer_tag => $consumer_tag);
weaken $self unless isweak $self;
$method->on(
success => sub {
$self->_close();
}
);
$method->catch(
sub {
$self->_close();
$self->emit(error => 'Error canceling consumption: ' . shift, @_);
}
);
$method->deliver();
}
return $self;
}
sub _close {
my $self = shift;
my %args = @_;
return unless 0 == scalar keys %{$self->consumer_cbs};
$self->client->_write_expect(
'Channel::Close' => {},
'Channel::CloseOk' => sub {
warn "-- Channel::CloseOk\n" if DEBUG;
$self->is_open(0)->is_active(0);
$self->client->delete_channel($self->id);
$self->emit('close');
},
sub {
$self->is_open(0)->is_active(0);
$self->client->delete_channel($self->id);
$self->emit(error => 'Failed closing channel: ' . shift);
},
$self->id,
);
return $self;
}
sub _assert_open {
my $self = shift;
return 0 unless $self->is_open and $self->is_active;
return 1;
}
sub _prepare_method {
my $self = shift;
my $method = Mojo::RabbitMQ::Client::Method->new(
client => $self->client,
channel => $self
);
weaken $method->{channel};
weaken $method->{client};
return $method->setup(@_);
}
sub declare_exchange {
my $self = shift;
return $self->_prepare_method(
'Exchange::Declare' => {
type => 'direct',
passive => 0,
durable => 0,
auto_delete => 0,
internal => 0,
@_, # exchange
ticket => 0,
nowait => 0, # FIXME
},
'Exchange::DeclareOk' => sub {
warn "-- Exchange::DeclareOk\n" if DEBUG;
}
);
}
sub declare_exchange_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->declare_exchange(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub delete_exchange {
my $self = shift;
return $self->_prepare_method(
'Exchange::Delete' => {
if_unused => 0,
@_, # exchange
ticket => 0,
nowait => 0, # FIXME
},
'Exchange::DeleteOk' => sub {
warn "-- Exchange::DeleteOk\n" if DEBUG;
}
);
}
sub delete_exchange_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->delete_exchange(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub declare_queue {
my $self = shift;
return $self->_prepare_method(
'Queue::Declare' => {
queue => '',
passive => 0,
durable => 0,
exclusive => 0,
auto_delete => 0,
no_ack => 1,
@_,
ticket => 0,
nowait => 0, # FIXME
},
'Queue::DeclareOk' => sub {
warn "-- Queue::DeclareOk\n" if DEBUG;
}
);
}
sub declare_queue_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->declare_queue(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub bind_queue {
my $self = shift;
return $self->_prepare_method(
'Queue::Bind' => {
@_, # queue, exchange, routing_key
ticket => 0,
nowait => 0, # FIXME
},
'Queue::BindOk' => sub {
warn "-- Queue::BindOk\n" if DEBUG;
}
);
}
sub bind_queue_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->bind_queue(@_);
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub unbind_queue {
my $self = shift;
return $self->_prepare_method(
'Queue::Unbind' => {
@_, # queue, exchange, routing_key
ticket => 0,
},
'Queue::UnbindOk' => sub {
warn "-- Queue::UnbindOk\n" if DEBUG;
}
);
}
sub unbind_queue_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->unbind_queue(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub purge_queue {
my $self = shift;
return $self->_prepare_method(
'Queue::Purge' => {
@_, # queue
ticket => 0,
nowait => 0, # FIXME
},
'Queue::PurgeOk' => sub {
warn "-- Queue::PurgeOk\n" if DEBUG;
}
);
}
sub purge_queue_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->purge_queue(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub delete_queue {
my $self = shift;
return $self->_prepare_method(
'Queue::Delete' => {
if_unused => 0,
if_empty => 0,
@_, # queue
ticket => 0,
nowait => 0, # FIXME
},
'Queue::DeleteOk' => sub {
warn "-- Queue::DeleteOk\n" if DEBUG;
}
);
}
sub delete_queue_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->delete_queue(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub publish {
my $self = shift;
return Mojo::RabbitMQ::Client::Method::Publish->new(
client => $self->client,
channel => $self
)->setup(@_);
}
sub publish_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = Mojo::RabbitMQ::Client::Method::Publish->new(
client => $self->client,
channel => $self
);
weaken $method->{client};
weaken $method->{channel};
$method->setup(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub consume {
my $self = shift;
my $method = $self->_prepare_method(
'Basic::Consume' => {
consumer_tag => '',
no_local => 0,
no_ack => 1,
exclusive => 0,
@_,
ticket => 0,
nowait => 0
},
'Basic::ConsumeOk' => sub {
warn "-- Basic::ConsumeOk\n" if DEBUG;
}
);
weaken $self;
$method->on(
success => sub {
my $this = shift;
my $frame = shift;
my $tag = $frame->method_frame->consumer_tag;
$self->consumer_cbs->{$tag} = $this;
}
);
return $method;
}
sub cancel {
my $self = shift;
my $method = $self->_prepare_method(
'Basic::Cancel',
{
@_, # consumer_tag
nowait => 0,
},
'Basic::CancelOk' => sub {
warn "-- Basic::CancelOk\n" if DEBUG;
}
);
weaken $self;
$method->on(
success => sub {
my $this = shift;
my $frame = shift;
delete $self->consumer_cbs->{$frame->method_frame->consumer_tag};
}
);
return $method;
}
sub get {
my $self = shift;
my $method = $self->_prepare_method(
'Basic::Get',
{
no_ack => 1,
@_, # queue
ticket => 0,
},
[qw(Basic::GetOk Basic::GetEmpty)]
);
weaken $self;
$method->on(
success => sub {
warn "-- Basic::GetOk|GetEmpty\n" if DEBUG;
my $this = shift;
my $frame = shift;
$this->emit(empty => $frame)
if $frame->method_frame->isa('Net::AMQP::Protocol::Basic::GetEmpty');
$self->_push_read_header_and_body(
'ok', $frame,
sub {
$this->emit(message => $frame, @_);
},
sub {
$this->emit(error => 'Failed to get messages from queue');
}
);
}
);
return $method;
}
sub get_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->get(@_);
weaken $self;
$method->on('message' => sub { shift; $promise->resolve($self, @_) });
$method->on('empty' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub ack {
my $self = shift;
my %args = ();
if (ref($_[0]) eq 'HASH') {
if (defined $_[0]->{ok}) {
$args{delivery_tag} = $_[0]->{ok}->method_frame->delivery_tag;
} elsif (defined $_[0]->{deliver}) {
$args{delivery_tag} = $_[0]->{deliver}->method_frame->delivery_tag;
}
} else {
%args = @_;
}
die "ack requires delivery_tag in arguments" unless defined $args{delivery_tag};
return $self->_prepare_method(
'Basic::Ack' => {
delivery_tag => 0,
multiple =>
(defined $args{delivery_tag} && $args{delivery_tag} != 0 ? 0 : 1),
%args,
}
);
}
sub ack_p {
my $self = shift;
my $promise = Mojo::Promise->new;
my $method = $self->ack(@_);
weaken $self;
$method->on('success' => sub { shift; $promise->resolve($self, @_) });
$method->on('error' => sub { shift; $promise->reject($self, @_) });
$method->deliver;
return $promise;
}
sub qos {
my $self = shift;
return $self->_prepare_method('Basic::Qos',
{prefetch_count => 1, @_, prefetch_size => 0, global => 0,},
'Basic::QosOk');
}
sub recover {
my $self = shift;
return $self->_prepare_method('Basic::Recover' => {requeue => 1, @_,});
}
sub reject {
my $self = shift;
return $self->_prepare_method(
'Basic::Reject' => {delivery_tag => 0, requeue => 0, @_,});
}
sub select_tx {
my $self = shift;
return $self->_prepare_method('Tx::Select', {}, 'Tx::SelectOk');
}
sub commit_tx {
my $self = shift;
return $self->_prepare_method('Tx::Commit', {}, 'Tx::CommitOk');
}
sub rollback_tx {
my $self = shift;
return $self->_prepare_method('Tx::Rollback', {}, 'Tx::RollbackOk');
}
sub _push_read_header_and_body {
my $self = shift;
my ($type, $frame, $cb, $failure_cb) = @_;
my $response = {$type => $frame};
my $body_size = 0;
$self->content_queue->get(
sub {
my $frame = shift;
return $failure_cb->('Received data is not header frame')
if !$frame->isa('Net::AMQP::Frame::Header');
my $header_frame = $frame->header_frame;
return $failure_cb->('Header is not Protocol::Basic::ContentHeader'
. 'Header was '
. ref $header_frame)
if !$header_frame->isa('Net::AMQP::Protocol::Basic::ContentHeader');
$response->{header} = $header_frame;
$body_size = $frame->body_size;
}
);
my $body_payload = "";
my $next_frame;
$next_frame = sub {
my $frame = shift;
return $failure_cb->('Received data is not body frame')
if !$frame->isa('Net::AMQP::Frame::Body');
$body_payload .= $frame->payload;
if (length($body_payload) < $body_size) {
# More to come
$self->content_queue->get($next_frame);
}
else {
$frame->payload($body_payload);
$response->{body} = $frame;
$cb->($response);
}
};
$self->content_queue->get($next_frame);
return $self;
}
sub DESTROY {
my $self = shift;
$self->close() if defined $self;
return;
}
1;
=encoding utf8
=head1 NAME
Mojo::RabbitMQ::Client::Channel - handles all channel related methods
=head1 SYNOPSIS
use Mojo::RabbitMQ::Client::Channel;
my $channel = Mojo::RabbitMQ::Client::Channel->new();
$channel->catch(sub { warn "Some channel error occurred: " . $_[1] });
$channel->on(
open => sub {
my ($channel) = @_;
...
}
);
$channel->on(close => sub { warn "Channel closed" });
$client->open_channel($channel);
=head1 DESCRIPTION
L<Mojo::RabbitMQ::Client::Channel> allows one to call all channel related methods.
=head1 EVENTS
L<Mojo::RabbitMQ::Client::Channel> inherits all events from L<Mojo::EventEmitter> and can emit the
following new ones.
=head2 open
$channel->on(open => sub {
my ($channel) = @_;
...
});
Emitted when channel receives Open-Ok.
=head2 close
$channel->on(close=> sub {
my ($channel, $frame) = @_;
...
});
Emitted when channel gets closed, C<$frame> contains close reason.
=head1 ATTRIBUTES
L<Mojo::RabbitMQ::Client::Channel> has following attributes.
=head2 id
my $id = $channel->id;
$channel->id(20810);
If not set, L<Mojo::RabbitMQ::Client> sets it to next free number when channel is opened.
=head2 is_open
$channel->is_open ? "Channel is open" : "Channel is closed";
=head2 is_active
$channel->is_active ? "Channel is active" : "Channel is not active";
This can be modified on reception of Channel-Flow.
=head2 client
my $client = $channel->client;
$channel->client($client);
=head1 METHODS
L<Mojo::RabbitMQ::Client::Channel> inherits all methods from L<Mojo::EventEmitter> and implements
the following new ones.
=head2 close
$channel->close;
Cancels all consumers and closes channel afterwards.
=head2 declare_exchange
my $exchange = $channel->declare_exchange(
exchange => 'mojo',
type => 'fanout',
durable => 1,
...
)->deliver;
Verify exchange exists, create if needed.
This method creates an exchange if it does not already exist, and if the
exchange exists, verifies that it is of the correct and expected class.
Following arguments are accepted:
=over 2
=item exchange
Unique exchange name
=item type
Each exchange belongs to one of a set of exchange types implemented by the server. The
exchange types define the functionality of the exchange - i.e. how messages are routed
through it. It is not valid or meaningful to attempt to change the type of an existing
exchange.
=item passive
If set, the server will reply with Declare-Ok if the exchange already exists with the same
name, and raise an error if not. The client can use this to check whether an exchange
exists without modifying the server state. When set, all other method fields except name
and no-wait are ignored. A declare with both passive and no-wait has no effect. Arguments
are compared for semantic equivalence.
=item durable
If set when creating a new exchange, the exchange will be marked as durable. Durable exchanges
remain active when a server restarts. Non-durable exchanges (transient exchanges) are purged
if/when a server restarts.
=item auto_delete
If set, the exchange is deleted when all queues have finished using it.
=item internal
If set, the exchange may not be used directly by publishers, but only when bound to other exchanges.
Internal exchanges are used to construct wiring that is not visible to applications.
=back
=head2 declare_exchange_p
Same as L<declare_exchange> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->declare_exchange_p(
exchange => 'mojo',
type => 'fanout',
durable => 1,
...
)->then(sub {
say "Exchange declared...";
})->catch(sub {
my $err = shift;
warn "Exchange declaration error: $err";
})->wait;
=head2 delete_exchange
$channel->delete_exchange(exchange => 'mojo')->deliver;
Delete an exchange.
This method deletes an exchange. When an exchange is deleted all queue bindings on the exchange
are cancelled.
Following arguments are accepted:
=over 2
=item exchange
Exchange name.
=item if_unused
If set, the server will only delete the exchange if it has no queue bindings. If the exchange has
queue bindings the server does not delete it but raises a channel exception instead.
=back
=head2 delete_exchange_p
Same as L<delete_exchange> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->delete_exchange_p(
exchange => 'mojo'
)->then(sub {
say "Exchange deleted...";
})->catch(sub {
my $err = shift;
warn "Exchange removal error: $err";
})->wait;
=head2 declare_queue
my $queue = $channel->declare_queue(queue => 'mq', durable => 1)->deliver
Declare queue, create if needed.
This method creates or checks a queue. When creating a new queue the client can
specify various properties that control the durability of the queue and its contents,
and the level of sharing for the queue.
Following arguments are accepted:
=over 2
=item queue
The queue name MAY be empty, in which case the server MUST create a new queue with
a unique generated name and return this to the client in the Declare-Ok method.
=item passive
If set, the server will reply with Declare-Ok if the queue already exists with the same
name, and raise an error if not. The client can use this to check whether a queue exists
without modifying the server state. When set, all other method fields except name and
no-wait are ignored. A declare with both passive and no-wait has no effect.
Arguments are compared for semantic equivalence.
=item durable
If set when creating a new queue, the queue will be marked as durable. Durable queues
remain active when a server restarts. Non-durable queues (transient queues) are purged
if/when a server restarts. Note that durable queues do not necessarily hold persistent
messages, although it does not make sense to send persistent messages to a transient queue.
=item exclusive
Exclusive queues may only be accessed by the current connection, and are deleted when
that connection closes. Passive declaration of an exclusive queue by other connections are
not allowed.
=item auto_delete
If set, the queue is deleted when all consumers have finished using it. The last consumer
can be cancelled either explicitly or because its channel is closed. If there was no consumer
ever on the queue, it won't be deleted. Applications can explicitly delete auto-delete queues
using the Delete method as normal.
=back
=head2 declare_queue_p
Same as L<declare_queue> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->declare_queue_p(
queue => 'mq',
durable => 1
)->then(sub {
say "Queue declared...";
})->catch(sub {
my $err = shift;
warn "Queue declaration error: $err";
})->wait;
=head2 bind_queue
$channel->bind_queue(
exchange => 'mojo',
queue => 'mq',
routing_key => ''
)->deliver;
Bind queue to an exchange.
This method binds a queue to an exchange. Until a queue is bound it will
not receive any messages. In a classic messaging model, store-and-forward
queues are bound to a direct exchange and subscription queues are bound
to a topic exchange.
Following arguments are accepted:
=over 2
=item queue
Specifies the name of the queue to bind.
=item exchange
Name of the exchange to bind to.
=item routing_key
Specifies the routing key for the binding. The routing key is used for
routing messages depending on the exchange configuration. Not all exchanges
use a routing key - refer to the specific exchange documentation. If the
queue name is empty, the server uses the last queue declared on the channel.
If the routing key is also empty, the server uses this queue name for the
routing key as well. If the queue name is provided but the routing key is
empty, the server does the binding with that empty routing key. The meaning
of empty routing keys depends on the exchange implementation.
=back
=head2 bind_queue_p
Same as L<bind_queue> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->bind_queue_p(
exchange => 'mojo',
queue => 'mq',
routing_key => ''
)->then(sub {
say "Queue bound...";
})->catch(sub {
my $err = shift;
warn "Queue binding error: $err";
})->wait;
=head2 unbind_queue
$channel->unbind_queue(
exchange => 'mojo',
queue => 'mq',
routing_key => ''
)->deliver;
Unbind a queue from an exchange.
This method unbinds a queue from an exchange.
Following arguments are accepted:
=over 2
=item queue
Specifies the name of the queue to unbind.
=item exchange
The name of the exchange to unbind from.
=item routing_key
Specifies the routing key of the binding to unbind.
=back
=head2 unbind_queue_p
Same as L<unbind_queue> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->unbind_queue_p(
exchange => 'mojo',
queue => 'mq',
routing_key => ''
)->then(sub {
say "Queue unbound...";
})->catch(sub {
my $err = shift;
warn "Queue unbinding error: $err";
})->wait;
=head2 purge_queue
$channel->purge_queue(queue => 'mq')->deliver;
Purge a queue.
This method removes all messages from a queue which are not awaiting acknowledgment.
Following arguments are accepted:
=over 2
=item queue
Specifies the name of the queue to purge.
=back
=head2 purge_queue_p
Same as L<purge_queue> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->purge_queue_p(
queue => 'mq',
)->then(sub {
say "Queue purged...";
})->catch(sub {
my $err = shift;
warn "Queue purging error: $err";
})->wait;
=head2 delete_queue
$channel->delete_queue(queue => 'mq', if_empty => 1)->deliver;
Delete a queue.
This method deletes a queue. When a queue is deleted any pending messages
are sent to a dead-letter queue if this is defined in the server configuration,
and all consumers on the queue are cancelled.
Following arguments are accepted:
=over 2
=item queue
Specifies the name of the queue to delete.
=item if_unused
If set, the server will only delete the queue if it has no consumers. If the queue
has consumers the server does does not delete it but raises a channel exception instead.
=item if_empty
If set, the server will only delete the queue if it has no messages.
=back
=head2 delete_queue_p
Same as L<delete_queue> but auto-delivers method and returns a L<Mojo::Promise> object.
$channel->delete_queue_p(
queue => 'mq',
if_empty => 1
)->then(sub {
say "Queue removed...";
})->catch(sub {
my $err = shift;
warn "Queue removal error: $err";
})->wait;
=head2 publish
my $message = $channel->publish(
exchange => 'mojo',
routing_key => 'mq',
body => 'simple text body',
);
$message->deliver();
Publish a message.
This method publishes a message to a specific exchange. The message will be
routed to queues as defined by the exchange configuration and distributed to
any active consumers when the transaction, if any, is committed.
Following arguments are accepted:
=over 2
=item exchange
Specifies the name of the exchange to publish to. The exchange name can be empty,
meaning the default exchange. If the exchange name is specified, and that exchange
does not exist, the server will raise a channel exception.
=item routing_key
Specifies the routing key for the message. The routing key is used for routing
messages depending on the exchange configuration.
=item mandatory
This flag tells the server how to react if the message cannot be routed to a queue.
If this flag is set, the server will return an unroutable message with a Return method.
If this flag is zero, the server silently drops the message.
All rejections are emitted as C<reject> event.
$message->on(reject => sub {
my $message = shift;
my $frame = shift;
my $method_frame = $frame->method_frame;
my $reply_code = $method_frame->reply_code;
my $reply_text = $method_frame->reply_text;
});
=item immediate
This flag tells the server how to react if the message cannot be routed to a queue consumer
immediately. If this flag is set, the server will return an undeliverable message with a
Return method. If this flag is zero, the server will queue the message, but with no guarantee
that it will ever be consumed.
As said above, all rejections are emitted as C<reject> event.
$message->on(reject => sub { ... });
=back
=head2 consume
my $consumer = $channel->consume(queue => 'mq');
$consumer->on(message => sub { ... });
$consumer->deliver;
This method asks the server to start a "consumer", which is a transient request for messages from a
specific queue. Consumers last as long as the channel they were declared on, or until the client cancels
them.
Following arguments are accepted:
=over 2
=item queue
Specifies the name of the queue to consume from.
=item consumer_tag
Specifies the identifier for the consumer. The consumer tag is local to a channel, so two clients can use the
same consumer tags. If this field is empty the server will generate a unique tag.
$consumer->on(success => sub {
my $consumer = shift;
my $frame = shift;
my $consumer_tag = $frame->method_frame->consumer_tag;
});
=item no_local (not implemented in RabbitMQ!)
If the no-local field is set the server will not send messages to the connection that published them.
See L<RabbitMQ Compatibility and Conformance|https://www.rabbitmq.com/specification.html>
=item no_ack
If this field is set the server does not expect acknowledgements for messages. That is, when a message
is delivered to the client the server assumes the delivery will succeed and immediately dequeues it.
This functionality may increase performance but at the cost of reliability. Messages can get lost if
a client dies before they are delivered to the application.
=item exclusive
Request exclusive consumer access, meaning only this consumer can access the queue.
=back
=head2 cancel
$channel->cancel(consumer_tag => 'amq.ctag....')->deliver;
End a queue consumer.
This method cancels a consumer. This does not affect already delivered messages, but
it does mean the server will not send any more messages for that consumer. The client
may receive an arbitrary number of messages in between sending the cancel method and
receiving the cancel-ok reply.
Following arguments are accepted:
=over 2
=item consumer_tag
Holds the consumer tag specified by the client or provided by the server.
=back
=head2 get
my $get = $channel->get(queue => 'mq')
$get->deliver;
Direct access to a queue.
This method provides a direct access to the messages in a queue using
a synchronous dialogue that is designed for specific types of application
where synchronous functionality is more important than performance.
This is simple event emitter to which you have to subscribe. It can emit:
=over 2
=item message
Provide client with a message.
This method delivers a message to the client following a get method. A message
delivered by 'get-ok' must be acknowledged unless the no-ack option was set
in the get method.
You can access all get-ok reply parameters as below:
$get->on(message => sub {
my $get = shift;
my $get_ok = shift;
my $message = shift;
say "Still got: " . $get_ok->method_frame->message_count;
});
=item empty
Indicate no messages available.
This method tells the client that the queue has no messages available for the
client.
=back
Following arguments are accepted:
=over 2
=item queue
Specifies the name of the queue to get a message from.
=item no_ack
If this field is set the server does not expect acknowledgements for messages. That is, when a message
is delivered to the client the server assumes the delivery will succeed and immediately dequeues it.
This functionality may increase performance but at the cost of reliability. Messages can get lost if
a client dies before they are delivered to the application.
=back
=head2 ack
$channel->ack(delivery_tag => 1);
Acknowledge one or more messages.
When sent by the client, this method acknowledges one or more messages
delivered via the Deliver or Get-Ok methods. When sent by server, this
method acknowledges one or more messages published with the Publish
method on a channel in confirm mode. The acknowledgement can be for
a single message or a set of messages up to and including a specific
message.
Following arguments are accepted:
=over 2
=item delivery_tag
Server assigned delivery tag that was received with a message.
=item multiple
If set to 1, the delivery tag is treated as "up to and including", so
that multiple messages can be acknowledged with a single method. If set
to zero, the delivery tag refers to a single message. If the multiple
field is 1, and the delivery tag is zero, this indicates acknowledgement
of all outstanding messages.
=back
=head2 qos
$channel->qos(prefetch_count => 1)->deliver;
Sets specified Quality of Service to channel, or entire connection. Accepts following arguments:
=over 2
=item prefetch_size
Prefetch window size in octets.
=item prefetch_count
Prefetch window in complete messages.
=item global
If set all settings will be applied connection wide.
=back
=head2 recover
$channel->recover(requeue => 0)->deliver;
Redeliver unacknowledged messages.
This method asks the server to redeliver all unacknowledged messages
on a specified channel. Zero or more messages may be redelivered.
=over 2
=item requeue
If this field is zero, the message will be redelivered to the original
recipient. If this bit is 1, the server will attempt to requeue the
message, potentially then delivering it to an alternative subscriber.
=back
=head2 reject
$channel->reject(delivery_tag => 1, requeue => 0)->deliver;
Reject an incoming message.
This method allows a client to reject a message. It can be
used to interrupt and cancel large incoming messages, or
return untreatable messages to their original queue.
Following arguments are accepted:
=over 2
=item delivery_tag
Server assigned delivery tag that was received with a message.
=item requeue
If requeue is true, the server will attempt to requeue the message.
If requeue is false or the requeue attempt fails the messages are
discarded or dead-lettered.
=back
=head2 select_tx
Work with transactions.
The Tx class allows publish and ack operations to be batched into atomic units of work.
The intention is that all publish and ack requests issued within a transaction will
complete successfully or none of them will. Servers SHOULD implement atomic transactions
at least where all publish or ack requests affect a single queue. Transactions that cover
multiple queues may be non-atomic, given that queues can be created and destroyed
asynchronously, and such events do not form part of any transaction.
Further, the behaviour of transactions with respect to the immediate and mandatory flags
on Basic.Publish methods is not defined.
$channel->select_tx()->deliver;
Select standard transaction mode.
This method sets the channel to use standard transactions. The client must use this method
at least once on a channel before using the Commit or Rollback methods.
=head2 commit_tx
$channel->commit_tx()->deliver;
Commit the current transaction.
This method commits all message publications and acknowledgments performed in the current
transaction. A new transaction starts immediately after a commit.
=head2 rollback_tx
$channel->rollback_tx()->deliver;
Abandon the current transaction.
This method abandons all message publications and acknowledgments performed in the current
transaction. A new transaction starts immediately after a rollback. Note that unacked messages
will not be automatically redelivered by rollback; if that is required an explicit recover
call should be issued.
=head1 SEE ALSO
L<Mojo::RabbitMQ::Client>, L<Mojo::RabbitMQ::Client::Method>, L<Net::AMQP::Protocol::v0_8>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2015-2017, Sebastian Podjasek and others
Based on L<AnyEvent::RabbitMQ> - Copyright (C) 2010 Masahito Ikuta, maintained by C<< bobtfish@bobtfish.net >>
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
=cut
|