| 1 | package Eixo::Queue::RabbitDriver; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Eixo::Base::Clase; | ||
| 5 | |||
| 6 | use Eixo::Queue::RabbitMessage; | ||
| 7 | |||
| 8 | use Net::AMQP::RabbitMQ; | ||
| 9 | |||
| 10 | has( | ||
| 11 | |||
| 12 | host=>'localhost', | ||
| 13 | |||
| 14 | port=>5672, | ||
| 15 | |||
| 16 | vhost=>undef, | ||
| 17 | |||
| 18 | user=>undef, | ||
| 19 | |||
| 20 | password=>undef, | ||
| 21 | |||
| 22 | __ch => undef, | ||
| 23 | |||
| 24 | __mq=>undef, | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub DESTROY{ | ||
| 28 | $_[0]->terminar; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub terminar{ | ||
| 32 | |||
| 33 | if($_[0]->{__mq}){ | ||
| 34 | |||
| 35 | $_[0]->__mq->channel_close(1) if($_[0]->{__ch}); | ||
| 36 | |||
| 37 | $_[0]->__mq->disconnect(); | ||
| 38 | } | ||
| 39 | |||
| 40 | $_[0]->{__mq} = $_[0]->{__ch} = undef; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub publicar :Sig(self, s, s, s){ | ||
| 44 | my ($self, $mensaje, $intercambio, $enrutado, $opciones, $props) = @_; | ||
| 45 | |||
| 46 | $self->__abrirCanal; | ||
| 47 | |||
| 48 | $self->__mq->exchange_declare( | ||
| 49 | |||
| 50 | 1, | ||
| 51 | |||
| 52 | $intercambio, | ||
| 53 | |||
| 54 | { | ||
| 55 | durable=>1, | ||
| 56 | } | ||
| 57 | |||
| 58 | ) if($intercambio); | ||
| 59 | |||
| 60 | my $opts; | ||
| 61 | |||
| 62 | $self->__mq->publish( | ||
| 63 | |||
| 64 | 1, | ||
| 65 | |||
| 66 | $enrutado, | ||
| 67 | |||
| 68 | $mensaje, | ||
| 69 | |||
| 70 | $opts = { | ||
| 71 | |||
| 72 | exchange=> $intercambio, | ||
| 73 | |||
| 74 | %{$opciones || {}} | ||
| 75 | }, | ||
| 76 | |||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub suscribirse :Sig(self, s, s, CODE){ | ||
| 81 | my ($self, $intercambio, $enrutado, $callback) = @_; | ||
| 82 | |||
| 83 | $self->__abrirCanal; | ||
| 84 | |||
| 85 | $self->__mq->exchange_declare( | ||
| 86 | |||
| 87 | 1, | ||
| 88 | |||
| 89 | $intercambio, | ||
| 90 | |||
| 91 | { | ||
| 92 | durable=>1, | ||
| 93 | } | ||
| 94 | ); | ||
| 95 | |||
| 96 | my $queue = $self->__mq->queue_declare(1, ""); | ||
| 97 | |||
| 98 | $self->__mq->queue_bind(1, $queue, $intercambio, $enrutado); | ||
| 99 | |||
| 100 | $self->__mq->consume(1, $queue, { | ||
| 101 | |||
| 102 | no_ack=>0 | ||
| 103 | |||
| 104 | }); | ||
| 105 | |||
| 106 | my $f; | ||
| 107 | |||
| 108 | $f = sub { | ||
| 109 | |||
| 110 | print "Esperando mensaje\n"; | ||
| 111 | |||
| 112 | my $rv; | ||
| 113 | |||
| 114 | REINTENTAR: | ||
| 115 | |||
| 116 | unless($self->__mq->is_connected()){ | ||
| 117 | die "ERROR: RABBIT_CONNECTION_LOST"; | ||
| 118 | } | ||
| 119 | |||
| 120 | $self->__mq->heartbeat(); | ||
| 121 | |||
| 122 | $rv = $self->__mq->recv(5000); | ||
| 123 | |||
| 124 | if($rv){ | ||
| 125 | |||
| 126 | $callback->( | ||
| 127 | |||
| 128 | Eixo::Queue::RabbitMessage->new( | ||
| 129 | |||
| 130 | driver=>$self, | ||
| 131 | |||
| 132 | message=>$rv | ||
| 133 | ), | ||
| 134 | |||
| 135 | sub { $f->() }, | ||
| 136 | |||
| 137 | sub { goto SALIR } | ||
| 138 | |||
| 139 | ); | ||
| 140 | |||
| 141 | } | ||
| 142 | else{ | ||
| 143 | goto REINTENTAR; | ||
| 144 | } | ||
| 145 | }; | ||
| 146 | |||
| 147 | $f->(); | ||
| 148 | |||
| 149 | return; | ||
| 150 | |||
| 151 | SALIR: | ||
| 152 | |||
| 153 | $self->terminar(); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub mensajeRecibido{ #:Sig(self, s){ | ||
| 157 | my ($self, $tag) = @_; | ||
| 158 | |||
| 159 | $self->__mq->ack(1, $tag); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub __abrirCanal{ | ||
| 163 | |||
| 164 | return if($_[0]->__ch); | ||
| 165 | |||
| 166 | $_[0]->__abrirConexion; | ||
| 167 | |||
| 168 | $_[0]->{__ch} = 1; | ||
| 169 | |||
| 170 | $_[0]->__mq->channel_open(1); | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | sub __cerrarCanal{ | ||
| 175 | |||
| 176 | return unless($_[0]->__ch); | ||
| 177 | |||
| 178 | $_[0]->__mq->channel_close(1); | ||
| 179 | |||
| 180 | $_[0]->{__ch} = 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub __abrirConexion{ | ||
| 184 | |||
| 185 | return if($_[0]->{__mq}); | ||
| 186 | |||
| 187 | $_[0]->{__mq} = Net::AMQP::RabbitMQ->new; | ||
| 188 | |||
| 189 | $_[0]->{__mq}->connect( | ||
| 190 | |||
| 191 | $_[0]->host, | ||
| 192 | |||
| 193 | { | ||
| 194 | port=>$_[0]->port, | ||
| 195 | |||
| 196 | user=>$_[0]->user || "guest", | ||
| 197 | |||
| 198 | password=>$_[0]->password || "guest", | ||
| 199 | |||
| 200 | vhost=>$_[0]->vhost || "/", | ||
| 201 | |||
| 202 | timeout=>1, | ||
| 203 | |||
| 204 | heartbeat=>1 | ||
| 205 | } | ||
| 206 | |||
| 207 | ); | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | 1; |