Building a stock quote application using IBM Workload Scheduler for Bluemix

Posted by on in Uncategorized
  • Font size: Larger Smaller
  • Hits: 681
  • 0 Comments
  • Subscribe to this entry
  • Print

Workload Scheduler for IBM® Bluemix™ provides a flexible job scheduling service for both one-time and recurring tasks.

image

"By integrating Workload Scheduler with Bluemix, you can schedule based on a recurring schedule, or at specific time and use the functionalities of an advanced scheduling product. You can use Workload Scheduler to define both basic and advanced scheduling rules using a set of simple APIs."

You can find the Workload Scheduler service in the Web and Application section of the Bluemix catalog.

The sample application described in this article demonstrates how easily you can write an app with the Workload Scheduler service which will periodically check the IBM stock value and send an sms to a specified mobile phone number when the stock value is greater than a specified threshold.

What you'll need for your application

  • Register on IBM Bluemix
  • Obtain a Twilio account
  • Provision an instance of the Workload Scheduler service

Provisioning an instance of the Workload Scheduler Service

  1. From the Bluemix Dashboard, click ADD A SERVICE. Select the Workload Scheduler service, which adds Workload Scheduler to your application.
  2. In the Add Service section, ensure that the App field is set to Leave unbound. Click CREATE.
  3. Click the Workload Scheduler service instance to open it.
  4. From the Workload Scheduler service page, you can click on “Learn” to have more details about the service.

Stock quote application

The application creates and schedules two steps to run every 30 minutes for retrieving the IBM stock value and sends an sms using the Twilio.

Step 1. Downloading of the application

  1. Download the sample data
  2. Import your application into your development environment (for example eclipse) and open the main class StockValueAlertExample
  3. Replace the Twilio constants with your Twilio account data

image

        4. Right click on build.xml and click on “Run As”-> “Ant build

image

Step 2.  Run the applicationimage

  1. Install the cf command-line tool
  2. Connect to Bluemix cf api https://api.ng.bluemix.net
  3. Log into Bluemix

          cf login -u <your_username

          cf target -o <your_username> -s dev

4. Deploy your app

           cf push <your_app_name> -p StockValueAlertExample.war

5. Bind the Workload Scheduler service

6.  Click on the link to start the application

image

Step 3.  Monitor the application

To monitor the application, go in the dashboard and click on “Launch Application Lab”:

image

Looking at the code

The java project is organized into two main sections: Java section and web section.

In the web section there are two simple JSPs: index.jsp and start.jsp: the first one is a simple html page with a form that calls start.jsp.

image

The web section calls the Java part and passes it all the required  information (mobile number and alert value) and checks if these are correct. If the values are correct, the application calls three java methods:

  • schedulerAppConnect , which connects it
  • schedulerAppCreate, which creates the artifacts on Workload Scheduler
  • schedulerAppRun, which runs the application

scheduleAppConnect

This method connects the application to the Workload Scheduler on Bluemix by parsing the content of VCAP_SERVICES system property.

To build a connection with the Workload Scheduler instantiate a new WorkloadService object passing the url retrieved from the VCAP_SERVICES variable.

scheduleAppCreate

This method interacts with Workload Scheduler service to create all artifacts required by the application.

(For details see docs page: http://www.ng.bluemix.net/docs/#services/WorkloadScheduler/index.html#workloadschedulerjava )

imageCreate a process that is a container that includes a number of steps to be performed based on a schedule.

Several steps are available:

  • REST call
  • Simple command
  • File Transfer
  • Database
  • Remote Command
  • WebSphere MQ

In this example, a Rest call and a simple command steps are used.

The rest call step is very simple and queries yahoo finance API to obtain the IBM stock value:

image

To define the step, a RestFulStep object is instantiated by passing agentName, url to connect to, url details (accepted and returned types) and HTTP method (get in this case).(*Note that you don’t need to install an agent, it is automatically installed when you bind the service.*)

This step is added to the process (last line of the above code): the steps in the process are run in the order in which they are add and you can pass in input to the next step the result  of the previous steps.

The next step is the CommandStep that performs a simple “cat” command: it cats the output of previous step and parses it to retrieve the IBM stock value.

image

As you can see, it is very simple to instantiate a CommandStep by specifying the command to run and the command parameters.

Define a Trigger:

Triggers are the main part of a scheduling service: they determine when a process runs.  Two types of triggers are supported:

  • Event-based: the process runs based on an event.
  • Schedule-based: the process runs based on a specified schedule.

In this example a schedule-based trigger using the cron syntax is used.

In this application, the job runs every 30 minutes, so the following cron syntax is defined:

image

Save the process on Workload Scheduler:

Now it is time to save the artifact to Workload Scheduler:

image

Pass the process p (remember that process keep inside steps and triggers) to the workloadservice object by calling the createAndEnableTask method: this returns a Task object that contains the taskId attribute that will be used later to retrieve process run instances from Workload Scheduler service.

scheduleAppRun

After the process is created, it is easy to schedule it running every 30 minutes.

It's time to gather run instances from Workload Scheduler in order to evaluate IBM stock value.

image

image

The application retrieves all completed instances: from the list of TaskHistoryInstance can you iterate to retrieve the log of CommandStep:

image

Just parse data returned from web and get stock value and the result is sent to your phone:

image

Authors:

Stefano Manocchio is currently a Staff Software Engineer and is working as developer for the IBM Workload Automation for distributed platforms product (on-premise & SaaS). Stefano has been working at IBM since 2002 as software developer for many products. He has a background in computer science technologies. Stefano is specialized in Workload scheduler integrations like Bluemix, TBSM, OSLC.

Liliana Pasceri is an Advisory Software Engineer in the Rome Tivoli Lab, and is working as developer for the IBM Workload Automation. In the past she also worked as a customer support engineer for the IBM Workload Automation.Reach her via twitter @LilianaPasceri or email francesca.liliana.pasceri@it.ibm.com.

0
Comments are not available for public users. Please login first to view / add comments.