Gianni Ceccarelli

NAME

Net::Stomp::MooseHelpers::CanConnect - role for classes that connect via Net::Stomp

VERSION

version 2.9

SYNOPSIS

  package MyThing;
  use Moose; with 'Net::Stomp::MooseHelpers::CanConnect';
  use Try::Tiny;

  sub foo {
    my ($self) = @_;
    SERVER_LOOP:
    while (1) {
      my $exception;
      try {
        $self->connect();

        # do something

      } catch {
        $exception = $_;
      };
      if ($exception) {
        if (blessed $exception &&
            $exception->isa('Net::Stomp::MooseHelpers::Exceptions::Stomp')) {
          warn "connection died, trying again\n";
          $self->clear_connection;
          next SERVER_LOOP;
        }
        die "unhandled exception $exception";
      }
    }
  }

DESCRIPTION

This role provides your class with a flexible way to connect to a STOMP server. It delegates connecting to one of many server in a round-robin fashion to the underlying Net::Stomp-like library.

ATTRIBUTES

connection

The connection to the STOMP server. It's built using the "connection_builder" (passing "extra_connection_builder_args", all "servers" as hosts, and SSL flag and options). It's usually a Net::Stomp object.

is_connected

True if a call to /connect succeded. Net::Stomp::MooseHelpers::ReconnectOnFailure resets this when reconnecting; you should not care much about it.

connection_builder

Coderef that, given a hashref of options, returns a connection. The default builder just passes the hashref to the constructor of Net::Stomp.

extra_connection_builder_args

Optional hashref to pass to the "connection_builder" when building the "connection".

servers

A ServerConfigList, that is, an arrayref of hashrefs, each of which describes how to connect to a single server. Defaults to [ { hostname => 'localhost', port => 61613 } ].

connect_headers

Global setting for connection headers (passed to "connect" in Net::Stomp). Can be overridden by the connect_headers slot in each element of "servers". Defaults to the empty hashref.

METHODS

current_server

Returns the element of "servers" that the "connection" says it's connected to.

connect

Call the connect method on "connection", passing the generic "connect_headers" and the per-server connect headers (from "current_server", slot connect_headers). Throws a Net::Stomp::MooseHelpers::Exceptions::Stomp if anything goes wrong.

If the "connection" attribute is set, and "is_connected", returns without doing anything.

AUTHOR

Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Net-a-porter.com.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.