Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Reference Applications > HelloWorld

HelloWorld Enabling Notifications

This topic describes a workflow for receiving the "data change notifications". The data change notifications are events that are delivered by the Junos Space platform whenever an entity is added, modified, or deleted in the database.

The following section describes how the user can provide support for data notification support in the HelloWorld Application. For this purpose the addCountry() method explains how to generate data notifications whenever a country and/or a state is added within a country. These notifications are pushed on to the respective JMS topic that is subscribed to receive them.

To enable a client to receive notifications:

  1. Write an MDB extending "GuiNotificationMDBean"
  2. Provide "JmpNotification" annotation on the respective managed object.
  3. Configure hornetq-jms.xml to add the JMS topic.
  4. Create REST interfaces using Wizard
  5. Build and Deploy the HelloWorld Application

Step 1: Write an MBD extending "GuiNotificationMDBean"

This class is responsible for listening to the JMS messages received on the JMS topic. This will also have the "activationconfig" that will specify the properties like "destinationType" and "destination" for the type of destination(queue/Topic) and the name of this destination.

For example,the following code snippet shows the HelloWorldGuiNotificationMDB.

HelloWorldGuiNotificationMDB.java

@MessageDriven(name = "HelloWorldGuiNotificationMDB", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/HelloWorldDatabaseChange"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "NonDurable") })

    /* Support for Data Notification */

    public class HelloWorldGuiNotificationMDB extends GuiNotificationMDBean {
        Logger logger = Logger.getLogger(HelloWorldGuiNotificationMDB.class.getName());
}

Note: If an application has its own topic, you need to restart JBoss for the message to come to an MDB that is registering with that topic. To restart JBoss, log in to the JSVA console and enter the following at the command line: service jboss restart.

Step 2: Provide a "JmpNotification" annotation on the respective managed object.

The following code snippet shows how you can use the "JmpNotification" annotation usage for the HelloWorld application over the "Country" managed object. The "destination" is the same name as given in the MDB for the JMS topic "destination" property. For example "topic/HelloWorldDatabaseChange".

Country.java

@JmpNotification(destination = "topic/HelloWorldDatabaseChange")
@XmlAccessorType(XmlAccessType.NONE)
public class Country extends AbstractManagedObject implements Serializable {

  private static final long serialVersionUID = -7193689193493067352L;

  @XmlElement
  private int id = 0;
  @XmlElement
  private String name;
  @XmlElement
  private Integer population;
  @XmlElement
  private String text;
  @XmlElement
  private boolean leaf;
  @XmlElement
  private Collection states;

  public Country() {
  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  public boolean getLeaf() {
    return leaf;
  }

  public void setLeaf(boolean leaf) {
    this.leaf = leaf;
  }

  public Country(String name) {
    this.name = name;
  }

  @Sortable(entityAttrName = "id")
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @Filterable(entityAttrName = "country")
  @Sortable(entityAttrName = "country")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Sortable(entityAttrName = "population")
  public Integer getPopulation() {
    return population;
  }

  public void setPopulation(Integer population) {
    this.population = population;
  }

  @XmlTransient
  private String moid = null;

  @Override
  public String getMOID() {
    return moid;
  }

  @Override
  public void setMOID(String arg0) {
    this.moid = arg0;
  }

  public Collection getStates() {
    return states;
  }

  public void setStates(Collection states) {
    this.states = states;
  }
}

Step 3: Configure "hornetq-jms.xml" to add the JMS topic.

The following configuration entry is to be added in hornetq-jms.xml to indicate the JMS topic.

hornetq-jms.xml

<topic name="HelloWorldDatabaseChange">
  <entry name="topic/HelloWorldDatabaseChange">
</topic>

HelloWorldImpl.java

/*
* Description: This method adds a country to the country table in database
* @param: ctx reference of ApiContextInterface
* @param: country reference of Country
* @exception: Exception
* @return: Country reference to Country
*/

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Country addCountry(ApiContextInterface ctx, Country country) throws Exception {

  if ((country != null) && (country.getPopulation() == null
      || country.getPopulation() > 0 || country.getName()
      .trim().equals("")))
    throw new IllegalArgumentException("Invalid population or invalid country name");

  CountryEntity countryEntity = fromCountry2CountryEntity(country);
  EntityManagerWrapper qm = new EntityManagerWrapper(manager);
  qm.persist(countryEntity, ctx);
  Collection >State< states = country.getStates();

  if (states != null && !states.isEmpty()) {
    for (State s : states) {
      logger.log(Level.INFO, "Adding state with Id-" + s.getId());
      addStateToCountry(ctx, s, countryEntity.getId());
    }
  }
  return getCountry(countryEntity.getCountry());
}

Step 4: Create REST interfaces using Wizard

The user must now create the REST classes using the "REST Interface wizard". The RESTified 'addCountry' method should have InternalPagingContext passed as an argument.

HelloWorldWebSvc/HelloWorldImpl.java

/**
 * Method:addCountry
 *
 * Description: This is an  auto generated method with stub
 * implementation which uses the
 *
 *      vnd.jssdk.helloworld.HelloWorld
 *
 * EJB bean and exposes its method with the REST Web services interface.
 *
 * @param  Country
 * @return vnd.jssdk.helloworld.rest.v1.Country
 */
public Country addCountry(Country param0) {

  vnd.jssdk.helloworld.Country country = null;

  try {
    country = new vnd.jssdk.helloworld.Country();
    copyRestToEjbForCountry(country, param0);
    country = getBean()
    .addCountry(new InternalPagingContext(), country);
    vnd.jssdk.helloworld.rest.v1.Country toReturn = new vnd.jssdk.helloworld.rest.v1.Country();
    copyEjbToRestForCountry(toReturn, country);
    param0 = toReturn;

  } catch (EntityExistsException e) {
    throw new WebApplicationException(
        Response.status(400)
        .entity("Unable to add, country already exists in the database.")
        .build());

  } catch (Exception e) {
    e.printStackTrace();
    throw new WebApplicationException(
        Response.status(400)
        .entity("Invalid request, unable to add country in the database.")
        .build());
  }
  return param0;
}

Step 5: Build and Deploy the HelloWorld Application

Build and deploy the HelloWorld application using the following options (icons) on the Eclipse tool bar:

For more information, see Building and Deploying HelloWorld.