Stock Ticker - A Sample Web Application for Event Publishing

 

 

Purpose

 

The purpose of the Stock Ticker web application is to demonstrate the use of event publishing in a business scenario.

 

 

Software Prerequisite

 

The following software is required for running the Stock Ticker web application:

  1. Q Capture
  2. xmlPubTk
    This XML Publication Toolkit is shipped as a separate sample in the xmlpubtk directory.
  3. DB2 UDB 7.2 or higher
  4. WebSphere MQ 5.3 or higher
  5. JDK 1.4 or higher
    http://java.sun.com/j2se/
  6. Tomcat 5.0 or higher
    http://jakarta.apache.org/tomcat/
  7. Ant 1.6 or higher
    http://ant.apache.org/
  8. Web browser

 

For installation details of all software above please refer to their corresponding documentation.

 

Tips:

  1. When installing WebSphere MQ on Windows please choose custom installation and include the MQSeries classes for Java and Java Message Service since the default installation does not include those.
  2. Please make sure that after the installation of all Java-based software the CLASSPATH and PATH environment variable are updated accordingly.

 

 

One-Time Setup of xmlPubTk, Tomcat, WebSphere MQ, and Ant

 

The following tasks need to be done first before running the Stock Ticker web application:

  1. After the installation of Tomcat, add a username with a manager role into the tomcat-users.xml file that is located under Tomcat’s conf/ directory.
    In the example below, the username tomcat with the password tomcat and manager role has been added to file (marked in blue) and the file content looks as below:

    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
      <role rolename="tomcat"/>
      <role rolename="role1"/>
      <role rolename="manager"/>
      <user username="tomcat" password="tomcat" roles="manager"/>
      <user username="role1" password="tomcat" roles="role1"/>
      <user username="both" password="tomcat" roles="tomcat,role1"/>
    </tomcat-users>


  2. After the installation of xmlPubTk, copy the xmlPubTk.jar file from to Tomcat’s shared/lib/ directory.

  3. After the installation of WebSphere MQ, copy all JAR (Java Archive) files from its Java/lib/ directory to Tomcat’s shared/lib/ directory.

  4. After the Ant installation, copy the catalina-ant.jar file from Tomcat’s server/lib/ directory to Ant’s lib/ directory.

 

 

Preparing Q Capture

 

After Q Capture’s installation the following tasks need to be completed. The script files used below are located under Stock Ticker’s script/ directory.

 

  1. Create the QCAPSAMP database

    1. Make sure DB2 UDB has been started

    2. Create the database with:

      db2 –tvf createdb.sql

  2. Create the control tables

    1. If you are not connected to QCAPSAMP database, connect to it with:
       
         
      db2 connect to QCAPSAMP

    2. Create the control tables with:

         
      db2 –tvf asnqcap.udb

      The control tables’ schema name is
      ASN.

      Note:
      The
      asnqcap.udb file is shipped as a sample script file.

    3. Add Q Capture parameter to the CAPPARMS control table with:

         
      db2 –tvf capparms.sql

      The queue manager name is
      QMSAMP, administration queue name is ASN.ADMINQ and the restart queue name is ASN.RESTARTQ.

  3. Create Stock Ticker’s application tables

    1. If you are not connected to QCAPSAMP database, connect to it with:
       
         
      db2 connect to QCAPSAMP

    2. Create the application tables with:

         
      db2 –tvf stocks.sql

      Three tables with the schema
      ASN will be created. They are the CURRENCIES, STOCKS, and STOCK_PRICES table.

  4. Create the publication queue and the XML publication

    1. If you are not connected to QCAPSAMP database, connect to it with:
       
         
      db2 connect to QCAPSAMP

    2. Create the publication queue and XML publication with:

         
      db2 –tvf stockxpub.sql

      A publication queue
      PUBQ1 with the send queue Q1 and an XML publication STOCK_PRICES_SUB will be created. The source table of the XML publication is the STOCK_PRICES table and only a subset of this table’s columns will be published. This should demonstrate Q Capture’s column sub-setting feature. Also a search condition will be specified in the XML publication. Only stock prices whose opening and last trading price that differ more than one unit of currency will be published. This should show Q Capture’s row sub-setting feature.

 

 

Preparing WebSphere MQ

 

The step below need to be done:

 

 

 

 

Content of asnqwxml.zip

 

The Stock Ticker’s script files, source and binary code, as well as the XML Publication Toolkit are compressed in asnqwxml.zip. You need to uncompress it using compression software such as WinZip or PkZip. The content of asnqwxml.zip is organized as follows:

 

stockticker/
   dist/

      stockticker.war

   scripts/
      capparms.sql

      createdb.sql

      stockdata_dlt.sql

      stockdata_init.sql

      stockdata_ins.sql

      stockdata_upd.sql

      stocks.mq

      stocks.sql

      stockxpub.sql

   src/

      com/

         ibm/

            db2/

               samples/

                  repl/

                     publication/

                        StockPrice.java

                        StockPriceListener.java

   web/

      WEB-INF/

         web.xml

      stockticker.jsp

   build.xml

   README.htm

 

 

Deploying Stock Ticker to Tomcat

 

You need to install the Stock Ticker’s binary files to Tomcat before running it. Its binary files are archived in the WAR (Web Archive) file stockticker.war under Stock Ticker’s dist/ directory.

 

The installation steps are:

 

  1. Start Tomcat  with:

    1. Change directory to Tomcat’s bin/ directory.
      This step is not required if you have included the Tomcat’s
      bin/ directory in your PATH environment variable.

    2. Run

          startup

  2. Install Stock Ticker

    1. Change directory to StockTicker’s directory.

    2. Run

         ant deploy

 

Initializing Stock Price

 

Before you run Stock Ticker web application you need to initialize the Stock Ticker’s application tables with:

 

    db2 –tvf stockdata_init.sql

 

The script file is located under Stock Ticker’s scripts/ directory and contains database transactions that populate the CURRENCIES and STOCKS table with initial data and clean the STOCK_PRICES table content.

 

 

Running Stock Ticker

 

Follow the steps below to run the Stock Ticker web application:

 

  1. Start DB2 UDB if it has not been started with:

       
    db2start

  2. Start the queue manager QMSAMP if it has not been started with:

      
    strmqm QMSAMP

  3. Start MQ listener on QMSAMP on the default port 1414 if it has not been started with:

      
    runmqlsr –m QMSAMP –t tcp –p 1414

  4. Start Q Capture if it has not been started with:

      
    asnqcap QCAPSAMP capture_schema=ASN startmode=cold

  5. Start Tomcat if it has not been started with

      
    startup

  6. Install Stock Ticker if it has not been installed. See previous section “Installing Stock Ticker”.

  7. Open a web browser.

  8. Enter the following address:

      
    http://localhost:8080/stockticker/stockticker.jsp


The Stock Ticker’s web page will be displayed. No stock price information will be shown because no stock price data has been entered yet.

 

 

Activating and Deactivating the Stock Price XML Publication

 

On the bottom part of the Stock Ticker’s web page there are two buttons labeled with “Activating XML Publication” and “Deactivating XML Publication”

 

The activation or deactivation of the stock price XML publication will be carried out by selecting the corresponding button.

 

Below the two buttons there is a message that tells the status of the stock price XML publication. The status can be either ‘Active’ or ‘Inactive’.

 

The activation/deactivation of the stock price XML publication work as follows:

  1. When the activation or deactivation button has been selected, Stock Ticker will process this request by generating an Activate Subscription or Deactivate Subscription message and put it onto the Q Capture’s administration queue.

  2. Stock Ticker will wait for five seconds to give Q Capture the chance to process its request.

  3. Stock Ticker will retrieve messages from the receive queue:

    1. If a Subscription Schema message has been found then the stock price XML publication status will be set to ‘Active’.

    2. If a Subscription Deactivated message has been found then the stock price XML publication status will be set to ‘Active’.

    3. If no message has been found the stock price XML publication status remains unchanged. Wait a couple second and simply refresh the Stock Ticker’s web page by pressing F5.

 

Publishing Stock Price Data

 

In order to publish stock price data we need to do the following:

1.       Activate the stock price XML publication if it is not active and make sure that the XML publication is in active state.

2.       Execute database transactions against the STOCK_PRICES table. There are sample script files stockdata_ins.sql, stockdata_ins.sql, and stockdata_ins.sql  that contains such database transactions.
Insert first some stock price data with

   db2 –tvf stockdata_ins.sql

3.       The Stock Ticker’s web page will automatically be refreshed every 5 seconds. You will see then the stock price information displayed on the web page. Please note that only stocks that have price change greater than 1 unit of currency will be displayed.

4.       Make some updates on the stock price data inserted previously with:

   db2 –tvf stockdata_upd.sql

The displayed stock price information will be updated automatically.

5.       Delete stock price data with:

   db2 –tvf stockdata_dlt.sql

 The displayed stock price  information will be updated automatically.

You also can try to deactivate the XML publication and make sure that the XML publication’s state becomes inactive. Then execute some database transactions that update the stock price data. The stock price data should remain unchanged. This is because no Transaction messages were published by Q Capture since the XML publication had been deactivated.

 

 

Other Tasks

 

 

 

Working with Ant

 

With Ant and the delivered sample build file build.xml located under Stock Ticker home directory you can accomplish other tasks such as:

 

1.       Building Stock Ticker web application. Let suppose you want to modify the Stock Ticker web application by updating the Java source files. After you made your changes you can re-compile them Stock Ticker by simply running:

   ant compile

The binary files will be then put under Stock Ticker’s build/ directory.

2.       Once you are ready to distribute the updated Stock Ticker web application you can create its binary distribution with:

   ant dist

Ant will create the WAR file stockticker.war that will be put under dist/ directory.

3.       If a clean build is desired then run:


   ant cbuild

Ant will remove old build/ and dist/ directory, compile the source codes, put the newly generated object files into the build/ directory, re-create the binary distribution and put it to the dist/ directory.

4.       If you want to install/remove the Stock Ticker web application to/from Tomcat then run:


   ant [deploy | undeploy]

5.       If you want to have a clean installation of the Stock Ticker web application to then run:

   ant cdeploy

Ant will make a clean build, a new distribution file, and finally install the distribution file on Tomcat.

6.       If you have installed Stock Ticker and simply want Tomcat to recognize that you have updated Java classes then run:

   ant reload

7.       If you want Tomcat to list all installed web applications then run:

   ant reload

This use useful to determine whether the Stock Ticker has been installed on Tomcat or not.