Guide - What is a Block Listener?

Introduction

A Phoenix Application consists of Blocks. Blocks can depend on the services of other Blocks. However there is circumstances in which relationships between Blocks other than dependency relationships should exist in an application.

For example, you may have a Block that is capable of exporting other Blocks as SOAP services. The Blocks that wish to be exported as SOAP services may need to export a service interface that extends SOAPClient.

What is a Block Listener?

A BlockListener is a component that is created before any Blocks are created in an Application. It receives notification after each Block is created and setup for Application. The listener also receives notification when a Block is about to be shutdown.

The relationship discussed above (between SOAPServer and SOAPClients) could be modelled as dependencies but that would mean that each application would need to modify the SOAPServer so that it depended on a particular number of SOAPClients that was specific to application. A better approach to modelling these relationships would be to use a BlockListener to "wire" together the SOAPClient services in Blocks to the SOAPServer service. As soon as any Block is detected that implements a SOAPClient service it could be registered with the SOAPServer.

How to make a block listener

Like normal blocks, a block listener can be LogEnabled and take configuration via Configurable. The special feature is that it must implement BlockListener and the four methods that are a consequence of that. Those methods illustrate blocks being added and removed etc and come with a BlockEvent argument. The following section in assembly.xml causes instantiation of the block listener:

        <listener class="pkg.MyBlockListener" name="a-suitable-name" />
      

Guide Contents

  1. What is a block?
  2. What is a block listener?
  3. What is an application listener?
  4. How do I create a block?
  5. How do I make my components phoenix-compatible?
  6. BlockInfo specification
by Phoenix Documentation Team