JBoss Community Archive (Read Only)

Portlet Bridge 3.2

Events

Send Event

Portlet Configuration

By default, the GenericFacesPortlet of the Bridge overrides the Event handling to dispatch all events to the Bridge. If you want to explicitly set this value, to prevent confusion, add the following to portlet.xml within its' <portlet> section:

<init-param>
  <name>javax.portlet.faces.autoDispatchEvents</name>
  <value>true</value>
</init-param>

When the entire portlet is written in JSF, this init-param must be set to true. Only set it to false when your portlet uses a mix of view technologies.

The portlet also needs to specify that it is able to publish an event to the portal by adding the below to portlet.xml within its' <portlet> section:

<supported-publishing-event>
  <qname xmlns:jbp="urn:jboss:portal:samples:event">jbp:BookingEvent</qname>
</supported-publishing-event>

This defines an event namespace and name to use when publishing the event.

Finally the portlet application needs to specify how the event namespace and name link to an actual type within the application. For the above publishing event definition, the portlet.xml would need the following:

<event-definition>
  <qname xmlns:jbp="urn:jboss:portal:samples:event">jbp:BookingEvent</qname>
  <value-type>org.jboss.example.booking.BookingEvent</value-type>
</event-definition>

The qname above is identical to that used earlier to create the link between a qname and class type

Event Type

To publish an event we need a type to represent the actual object that will be attached to the event. Here is an example event type:

@XmlRootElement
public class BookingEvent implements Serializable {

  private String id;
  public static final QName QNAME = new QName("urn:jboss:portal:samples:event", "BookingEvent");

  public BookingEvent(String id) {
      this.id = id;
  }

  public String getId() {
    return id;
  }
}

The type needs the @XmlRootElement annotation so that the event can be serialized into a JAXB object for publishing.

Dispatch Event

To dispatch the event to other portlets within the portal, you can do something similar to the below within a Bean method, such as a method triggered by an action:

Object response = FacesContext.getCurrentInstance().getExternalContext().getResponse();
  if (response instanceof StateAwareResponse) {
    String id = "an id";
    StateAwareResponse stateResponse = (StateAwareResponse) response;
    stateResponse.setEvent(BookingEvent.QNAME, new BookingEvent(id));
  }

Receive Event

Portlet Configuration

For a portlet to receive an event, it needs to specify the following within portlet.xml within its' <portlet> section:

<init-param>
  <name>javax.portlet.faces.bridgeEventHandler</name>
  <value>org.jboss.example.booking.BookingEventHandler</value>
</init-param>

The portlet also needs to specify that it is able to receive an event from the portal by adding the below to portlet.xml within its' <portlet> section:

<supported-processing-event>
  <qname xmlns:jbp="urn:jboss:portal:samples:event">jbp:BookingEvent</qname>
</supported-processing-event>

As when defining the portlet that can publish an event, the portlet that can receive that event needs to define the link between the qname and event type. The identical <event-definition> specified in Send Event can be used.

Event Handler

To process the Event within a portlet a handler, that was defined within portlet.xml as seen earlier, needs to be created with content similar to the following:

public class BookingEventHandler implements BridgeEventHandler {
  public EventNavigationResult handleEvent(FacesContext context, Event event) {
    // Process event payload as appropriate
  }
}

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 12:28:03 UTC, last content change 2012-07-31 21:01:16 UTC.