The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

version 3.0

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 } ].

If a server requires TLS, you can do [ { hostname => $hostname, port => $port, ssl =>1 } ].

If a server requires authentication, you can pass the credentials in the connect_headers slot here: [ { hostname => $hostname, port => $port, connect_headers => { login => $login, passcode => $passcode } } ].

If all servers require the same authentication, you can instead set the credentials in the "connect_headers" attribute.

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.

If all servers require the same authentication, you can set the credentials here: { login => $login, passcode => $passcode }. If different servers require different credentials, you should set them in the "servers" attribute instead.

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.