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

HelloWorld Implemention of Asynchronous Notifications

This topic describes how clients can implement a long running request (LRR) for asynchronous notifications in the HelloWorld Application. For this purpose the getCountriesScheduledLRR() method explains how a LRR works, whenever a read operation is performed to get all the countries and the states within those countries.

EJB Implementation

The following steps enable a client to implement an LRR according to the EJB Implementation:

Step 1: Create an EJB that supports the LRR

Step 2: Write the EJB implementation code

HelloWorld.java

public interface HelloWorld extends JobManagerCallerInterface3 {

  /*
   * Schedules a LRR job to get the list of countries 
   * @param scheduleCtx    reference of ScheduleContext
   * @exception  Exception
   * @return JobInfoTO     reference to JobInfoTO
   */

  public JobInfoTO getCountriesScheduledLRR(ScheduleContext  scheduleCtx)throws Exception;

}

HelloWorldImpl.java

/* EJB implementaion  for the Long Running Request */

@Stateless(name = "HelloWorldEJB")
@Remote(HelloWorld.class, JOBResultHandler.class)

public class HelloWorldImpl extends JobWorker implements HelloWorld, JOBResultHandler
{
        Logger logger = Logger.getLogger(HelloWorldImpl.class.getName());

        @PersistenceContext(unitName = "HelloWorldDBUnit")
        private static JobManager jobMgr = JxServiceLocator
                        .lookup("cmp.JobManagerEJB");
                        
  /*
  * Description: This method schedules a LRR job to get the list of countries
  * @param: scheduleCtx reference of ScheduleContext
  * @exception: Exception
  * @return: JobInfoTO reference to JobInfoTO
  */


 @Schedulable
 @TransactionAttribute(TransactionAttributeType.REQUIRED)
 public JobInfoTO getCountriesScheduledLRR(ScheduleContext scheduleCtx)
                throws Exception {
        // Using the ScheduleContext to schedule a job
        int jobInstanceId = -1;
        InternalScheduleContext iSctx = null;

        if (scheduleCtx != null) {
        iSctx = (InternalScheduleContext) scheduleCtx;
        jobInstanceId = iSctx.getJobInstanceId();
        } else {
        logger.log(Level.INFO, "ScheduleContext is null");
        return null;
 }

 PagingResult countryObject = getCountries(new InternalPagingContext());
 String resultData = getCountryAndStateData(countryObject);
 JobInfoTO jobinfo = new JobInfoTO();
 jobinfo.setId(jobInstanceId);
 if (countryObject != null) {
        logger.log(Level.INFO, "Got the countries and states successfully");
        jobMgr.setJobInstanceResult(jobInstanceId, resultData,
        JobStatus.SUCCESS, null);

 } else {
        logger.log(Level.INFO, "failed to retrieve the data");
        jobMgr.setJobInstanceResult(jobInstanceId,
        "Long Running Request Failed", JobStatus.FAILURE, null);
}

 return jobinfo;

 }
}
/*
 * Description: This method gets country's and states within those countries
 * as a JSON output
 * @param countryObject reference of PagingResult >Country<
 * @exception
 * @return String returns a string result
 */

public String getCountryAndStateData(PagingResult countryObject) {
        Countries countries = new Countries();
        countries.setCountries(countryObject);
        JettisonMappedContext context = new JettisonMappedContext(
        Countries.class);
        StringWriter writer = new StringWriter();
        Marshaller marshaller = null;
        try {
        marshaller = context.createMarshaller();
        marshaller.marshal(countries, writer);
        } catch (JAXBException e) {
        e.printStackTrace();
        }
        return writer.toString();
        }

/*
 * Description: This method is overridden method of JobResultHandler class which returns a string result
 * @param jobId
 * integer job id
 * @exception
 * @return String returns a string result
 */

@Override
public String getResultDataToPost(int jobId, Serializable jobData) {
        String result = jobData.toString();
        return result;
        }
}

HelloWorld.java

public interface HelloWorld extends JobManagerCallerInterface3 {

  public JobInfoTO getCountryScheduledLRR(ScheduleContext  scheduleCtx, int id) throws Exception;

}

The EJB implementation class extends "JobWorker".

@Schedulable
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public JobInfoTO getCountryScheduledLRR(ScheduleContext scheduleCtx) 
{
  int jobInstanceId = -1;

  if(scheduleCtx!= null) {

    // Get jobInstanceId using InternalSchedulecontext
    InternalScheduleContext iSctx = (InternalScheduleContext)scheduleCtx;
    jobInstanceId = iSctx.getJobInstanceId();
  }

  PagingResult countryObject = getCountries(new  InternalPagingContext());

  setCountryObject(countryObject);
  // Create a new JobInfoTo object

  JobInfoTO jobinfo = new JobInfoTO();

  //Set id of JobInfoTo object to jobInstanceId

  jobinfo.setId(jobInstanceId);
  // Set the job status
  if (countryObject != null) {

    jobMgr.setJobInstanceResult(jobInstanceId, resultData,
        JobStatus.SUCCESS, null);

  } else {
    logger.log(Level.INFO, "failed to retrieve the data");
    jobMgr.setJobInstanceResult(jobInstanceId,
        "Long Running Request Failed", JobStatus.FAILURE, null);
  }

  // Return object of JobInfoTO
  return jobinfo;
}

REST Implementation

The following steps enable a client in implementing an LRR according to the REST Implementation.

Step 1: Write a REST method declaration code for which the LRR should work.

You will have to write the REST level code manually to specify the "Path" to be hit on the REST client. The REST method type used in this case will be "POST" and the mime-type used in consumes and produces will be "application/xml".

HelloWorldWebSvc/HelloWorld.java

@Path("/run-countries-report")
@POST
public Task execLRRToGetAllCountries ();

Step 2: Write the REST implementation code.

The code below shows the REST method implementation for the execLRRToGetAllCountries () method in the HelloWorld application for getting a list of countries and states within those countries.

HelloWorldWebSvc/HelloWorldImpl.java

public Task execLRRToGetAllCountries ();

JobInfoTO jobInfoTo;
try {
  // Get the Schedule Context from RestProviderFactory

  ScheduleContext sCtx = ResteasyProviderFactory.getContextData(ScheduleContext.class);

  // Call the EJB method that create JobInfoTo instance

  jobInfoTo = getBean().getCountriesScheduledLRR(sCtx);

} catch (Exception e) {
  throw new WebApplicationException(e);
}

// Create a new "Task" 

Task task = new Task();
task.setId(jobInfoTo.getId());
task.setEjbName("HelloWorld/HelloWorldEJB/remote");
return task;
}