Thursday, April 2, 2009

Smooks+JBoss ESB applied : Getting Quotes from YahooFinance

I've been doing some researching for creating a cool sample using CEP into JBoss, while I am still working on it, I decided put a simple sample using Smooks for transforming CSVs from Yahoo Finance into XMLs, which is a format much easier for integrating with everything else.

Getting Stock Symbols and Quotes from Yahoo Finance in CSV Format


Yahoo offers an URL that gives you information about the quotes, it is not a WebServices, "REST Service" or nothing especial or too complex, it is basically an HTTP URL, that you may pass some additional info, and you can see the Quotes information by a glimpse into a CSV output. Look the following URL format:

http://finance.yahoo.com/d/quotes.csv?s= a BUNCH of STOCK SYMBOLS separated by "+" &f=a bunch of special tags

These special tags you can get more information here

I decided use this URL: http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT+ORCL+JAVA&f=snb3pt1d1 , and what does these parameters means?

a) s= The symbols, in that case: Red Hat, Microsoft, Oracle and Sun Microsystems.

b) f= It means which information I wanna in my "report", I decided put:
  • s = symbol s - symbol
  • n- companyName
  • b- bid
  • 3p - last bid
  • t1 - time
  • d1 - TradeDate
The result will give me the following information:

[java] "RHT","RED HAT INC",17.90,17.68,"4:04pm","4/2/2009"
[java] "MSFT","Microsoft Corpora",19.33,19.31,"4:00pm","4/2/2009"
[java] "ORCL","Oracle Corporatio",18.83,18.58,"4:00pm","4/2/2009"
[java] "JAVA","Sun Microsystems,",8.38,8.00,"4:00pm","4/2/2009"

Time to make it available into my JBoss ESB.

Using Smooks

The only thing that I am doing for testing is send the data obtained from the Yahoo URL to my Service JMS Quee Gateway, you may use Commons Http Client if you want something more sophisticated, othewise you may create a simple code as you can see in the following code-listing:


public static void main(String args[]) throws Exception
{
SendJMSMessage sm = new SendJMSMessage();
sm.setupConnection();
StringBuilder quotesData = new StringBuilder();
try {
// Create a URL for the desired page
URL url = new URL("http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT+ORCL+JAVA&f=snb3pt1d1");

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {

quotesData.append(str + "\n");


}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

System.out.println("Sending:\n" + quotesData.toString());
sm.sendAMessage(quotesData.toString());
sm.stop();

}





This is a basic JMS client (use the quickstarts as a template).

Now, the only I thing I have to do is configure my smooks-res.xml file as well as my "Smooks Actions" in jboss-esb.xml, in the following image, you can see on the left side the jboss-esb.xml and smooks-res:


The only thing I had done was editing some XML, and everything is ready.

This is one more practical example of JBoss ESB and its transformation engine.

Download the solution

You can download the solution from here