AnyEvent-RabbitMQ-PubSub

 view release on metacpan or  search on metacpan

lib/AnyEvent/RabbitMQ/PubSub/Publisher.pm  view on Meta::CPAN

package AnyEvent::RabbitMQ::PubSub::Publisher;
use Moose;

use AnyEvent;

has channel => (
    is => 'ro', isa => 'AnyEvent::RabbitMQ::Channel', required => 1
);
has exchange => (
    is => 'ro', isa => 'HashRef', required => 1
);
has routing_key => (
    is => 'ro', isa => 'Str', default => '#'
);
has default_header => (
    is => 'ro', isa => 'Maybe[HashRef]'
);

sub init {
    my ($self) = @_;

    my $cv = AnyEvent->condvar;

    $self->channel->declare_exchange(
        %{ $self->exchange },
        on_success => sub { $cv->send() },
        on_failure => sub { $cv->croak(@_) },
    );

    $cv->recv();
    return
}

sub publish {
    my ($self, %options) = @_;

    $self->channel->publish(
        exchange    => $self->exchange->{exchange},
        routing_key => $self->routing_key,
        on_inactive => sub { die 'Failed to publish: channel inactive' },
        %options,
        header      => $options{header} // $self->default_header,
        body        => $options{body} // '',
    );
}

1

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.366 second using v1.00-cache-2.02-grep-82fe00e-cpan-f73e49a70403 )