Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Catalyst::Model::RabbitMQ

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


jmo.mlists at gmail

Aug 18, 2012, 11:47 AM

Post #1 of 6 (299 views)
Permalink
Catalyst::Model::RabbitMQ

Hi all,

I'm working on a hobby project of mine where I need to use RabbitMQ. I
couldn't find any "standard" way of using it in Catalyst so I made the
attached module Catalyst::Model::RabbitMQ.

I can then have a simple model:
package MyApp::Model::MQ;
use Moose;
use namespace::autoclean;

extends 'Catalyst::Model::RabbitMQ';

__PACKAGE__->config(
queue_names => [ qw/user_changes/ ],
default_routing_key => 'user_changes',
);

And publish messages in my controllers like this:
$c->model('MQ')->publish ("", "user_changes", "sent from catalyst");
$c->model('MQ')->publish (undef, undef, "sent from catalyst without
key"); # using default_routing_key

Is there already a better way of doing this? Should I add some
documentation and try to get it to CPAN or should I just use someone else's
already more complete and robust way of sending messages?

/Jon
Attachments: RabbitMQ.pm (1.80 KB)


dakkar at thenautilus

Aug 18, 2012, 12:36 PM

Post #2 of 6 (285 views)
Permalink
Re: Catalyst::Model::RabbitMQ [In reply to]

I'm using a combination of CatalystX::ComponentsFromConfig and Net::Stomp::Producer. The first (t0m's code, even if released by me) allows you to avoid writing essentially empty model classes (and to apply roles via configuration file). The second is the one that actually deals with messaging. As the name implies it's using STOMP, but there's actually rather little in the interface that depends on it. It works like this:

- a "send" method takes a destination (queue name, whatever), a hashref of headers (including a 'type', that I imagine being similar to RabbitMQ's routing key), and a byte string for the message payload
- if the message payload is a reference, it is passed through a serialiser
- a "transform_and_send" method allows you to pass some internal representation, and having the logic to transform it into a message in a specialised class

We've been using something very similar at $work for a coulpe of years now, and it seems to be simple and flexible enough.
--
dakkar - mobilis in mobile

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


jmo.mlists at gmail

Aug 18, 2012, 1:36 PM

Post #3 of 6 (287 views)
Permalink
Re: Catalyst::Model::RabbitMQ [In reply to]

On Sat, Aug 18, 2012 at 9:36 PM, Gianni Ceccarelli
<dakkar [at] thenautilus>wrote:

> I'm using a combination of CatalystX::ComponentsFromConfig and
> Net::Stomp::Producer. The first (t0m's code, even if released by me) allows
> you to avoid writing essentially empty model classes (and to apply roles
> via configuration file). The second is the one that actually deals with
> messaging. As the name implies it's using STOMP, but there's actually
> rather little in the interface that depends on it. It works like this:
>
> - a "send" method takes a destination (queue name, whatever), a hashref of
> headers (including a 'type', that I imagine being similar to RabbitMQ's
> routing key), and a byte string for the message payload
> - if the message payload is a reference, it is passed through a serialiser
> - a "transform_and_send" method allows you to pass some internal
> representation, and having the logic to transform it into a message in a
> specialised class
>
> We've been using something very similar at $work for a coulpe of years
> now, and it seems to be simple and flexible enough.
>

Hmm, sounds like something which is definitely worth looking into, thanks!

Slightly off topic here:

Is there a sane way to send a message each time I do a DB insert/delete?
>From what I can see do I have to either:
- Wriggle $c in to my Schema module and override the insert/delete methods
- Create some wrapper method inside a Controller and call that, which in
turn calls db insert/delete and then sends the message
- Have the call next to my DB call in each function doing insert/delete

All three feels bad in different ways, is there a fourth (and/or fifth)
alternative I haven't thought of which makes this nicer?

/Jon


catalyst at iandocherty

Aug 18, 2012, 1:43 PM

Post #4 of 6 (287 views)
Permalink
Re: Catalyst::Model::RabbitMQ [In reply to]

On 18 August 2012 20:36, Gianni Ceccarelli <dakkar [at] thenautilus> wrote:
> I'm using a combination of CatalystX::ComponentsFromConfig and Net::Stomp::Producer. The first (t0m's code, even if released by me) allows you to avoid writing essentially empty model classes (and to apply roles via configuration file). The second is the one that actually deals with messaging. As the name implies it's using STOMP, but there's actually rather little in the interface that depends on it. It works like this:
>
> - a "send" method takes a destination (queue name, whatever), a hashref of headers (including a 'type', that I imagine being similar to RabbitMQ's routing key), and a byte string for the message payload
> - if the message payload is a reference, it is passed through a serialiser
> - a "transform_and_send" method allows you to pass some internal representation, and having the logic to transform it into a message in a specialised class
>
> We've been using something very similar at $work for a coulpe of years now, and it seems to be simple and flexible enough.
> --
> dakkar - mobilis in mobile
>

It's 'normal' not to create Catalyst specific models, but to create
your model outside of Catalyst (so you can use it in things like cron
jobs for example) in which case you just use a thin adaptor class. See
Catalyst::Model::Adaptor

If you do it that way you don't need to publish a cpan module at all.

- icydee

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


jmo.mlists at gmail

Aug 18, 2012, 2:19 PM

Post #5 of 6 (284 views)
Permalink
Re: Catalyst::Model::RabbitMQ [In reply to]

On Sat, Aug 18, 2012 at 10:43 PM, Ian Docherty <catalyst [at] iandocherty>wrote:

> It's 'normal' not to create Catalyst specific models, but to create
> your model outside of Catalyst (so you can use it in things like cron
> jobs for example) in which case you just use a thin adaptor class. See
> Catalyst::Model::Adaptor
>
> If you do it that way you don't need to publish a cpan module at all.
>
> - icydee
>
>
Was that for me or Gianni?

I wasn't aware of Catalyst::Model::Adaptor, seems like a useful module.
What is the general take on modules such as
Catalyst::Model::WebService::Solr if it's 'normal' to use C::M::Adaptor?
Isn't that pretty much the same as I was suggesting for RabbitMQ? Is it
thought of as depricated?

/Jon


darren at darrenduncan

Aug 18, 2012, 10:20 PM

Post #6 of 6 (288 views)
Permalink
Re: Catalyst::Model::RabbitMQ [In reply to]

jmo mlists wrote:
> On Sat, Aug 18, 2012 at 10:43 PM, Ian Docherty <catalyst [at] iandocherty> wrote:
>
> It's 'normal' not to create Catalyst specific models, but to create
> your model outside of Catalyst (so you can use it in things like cron
> jobs for example) in which case you just use a thin adaptor class. See
> Catalyst::Model::Adaptor
>
> If you do it that way you don't need to publish a cpan module at all.
>
> - icydee
>
> Was that for me or Gianni?
>
> I wasn't aware of Catalyst::Model::Adaptor, seems like a useful module.
> What is the general take on modules such as
> Catalyst::Model::WebService::Solr if it's 'normal' to use C::M::Adaptor?
> Isn't that pretty much the same as I was suggesting for RabbitMQ? Is it
> thought of as depricated?
>
> /Jon

Modules that don't use Catalyst::Model::Adaptor are either legacy modules doing
things the hard way or they have very special needs. You should use
Catalyst::Model::Adaptor even if you see other examples that don't. -- Darren Duncan

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.