Thursday, June 4, 2015
Spark Cluster Computing - Part 1
Monday, June 30, 2014
Future in Java - Asynchronous Computation
A Future is a representation of the result of an asynchronous computation. A Future has methods to check if the computation has completed and retrieve the result of the task. You can also cancel a computation if you choose to, unless the computation has already completed.
- isDone() returns a boolean representing if the task is done. This method is used for polling to check if the task is done.
- Get() returns the final result of the Future,it is a blocking method which waits until the computation is done. This method take a timeout argument in milliseconds. Which specifies the maximum amount of time we wait for the computation to complete. If the task is not done in the specified time, a timeout exception will be thrown.
- Cancel() will abort the computation.
- Fire http request and get the Future representing the outcome(i.e no waiting for the request to finish).
- Do some random task
- Check if the response is ready. Here we use the get() method to block and retrieve the response.
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
<version>1.8.11</version>
</dependency>
Saturday, February 1, 2014
Using Node.js to build a site that returns JSON data
I am working on a project that integrates with multiple other vendors using Http endpoint. We send http requests with JSON payload and the vendors has to consume that and respond with a response with JSON payload. I wanted to have a simple dummy site that takes the request and responds with the same kind of data I expect from the vendors. The whole objective of having this is to mock the vendor and test the integration. Moreover, I wanted an easy and fast process to create, deploy and start multiple mock vendor sites.
Node.js can be used to achieve this. For those of you who are already familiar with javascript, the code needed to create and run these sites is very minimal.
Note: this post is not supposed to be a tutorial on Node.js. That would be a topic for another day.Once I am comfortable enough with the nitty gritty details of Node.js, I hope to come back with a post.
I am using a Windows machine and here are the steps I followed,
1. Install Node.js from here
- Node.js will install a console window that you can use as a playground or IDE. Here you can write and run any JavaScript code
- Node.js root directory is included in the PATH variable of system environment variables . Thus you can invoke the node command from your command prompt
Note: You can keep your node file at any directory you want.
node myhttpendpoint.js
Wednesday, June 26, 2013
IIS Servant - a web based management tool for IIS Manager
It saves you the trouble of remote desktop to your severs to open IIS manager and manage your web sites.
It allows you do all this management actions through a beautiful user interface.
I installed it on some of our web server boxes .The installation is very simple and you can install it and start using it in a matter of few minutes.
I had a CNAME ready to use for the iismanager site it is going to create and I used that as 'servant url' during installation.
You can download it from servant io
Here is a screen shot of one of my servant IIS configurations ...
Tuesday, April 23, 2013
The PRG /POST-REDIRECT-GET/ Pattern using ASP.NET MVC
If a post request is supposed to return a page, instead it should does its side effect operation(e.g. saving order) and redirect to a get request that returns the page. Thus result of the get request could be bookmarked and can be requested as needed with out a side effect(undesired additional orders).
The double submit problem is explained in depth in this article by Michael Jouravlev.
Using the ASP.Net MVC framework it is simple to implement the PRG pattern, below you will find a small
code snipnet that uses the RPG pattern.
Step1(POST) Controller action will make a redirect to Step 1(GET request) on error or if successful it will make a GET request to the next step 2(GET). Step 1(POST) doesn't build and return pages it self.
[HttpGet]
[ImportModelStateFromTempData]
public ActionResult Step1(int id)
{
var m = _orderService.GetOrder(id);
return View(m);
}
[HttpPost]
[ExportModelStateToTempData]
public ActionResult Step1(Order m)
{
//If the model is valid(satisfies all the validation requirements set for it)
//persist it and make a get request to the next step
//else make a redirect to GET request page
if (ModelState.IsValid)
{
// save
-orderService.Save(m);
// Go to the next step (Step 2 here is a HttpGet ActionResult)
return RedirectToAction("Step2", new { id = m.OrderId });
}
//redirect to the Get Request
return RedirectToAction("Step1",new {id =m.OrderId});
}
[HttpGet]
[ImportModelStateFromTempData]
public ActionResult Step2(int id)
{
var m = _orderService.GetOrder(id);
return View(m);
}
Friday, March 22, 2013
Lunch and Learn Session on SignalR .Net - Realtime for Web
Here is the power-point to the presentation I gave my colleagues a few weeks back on SignalR.
Tuesday, January 15, 2013
Extending the default Command Timeout in Nhibernate
There was a Nhibernate query that is taking more than 30 sec , 34 seconds to be exact. The query involves a view that joins so many views and tables. It started taking more than 30 seconds last week. Reviewing the execution plan and throwing indexes here and there didn't help. The next immediate solution was to extend the Nhibernate default timeout time (i.e.30 seconds).
I believe extending the timeout should be a last resort approach , should be used as immediate fix until you come up with a long term solution - like redesigning your data architecture , optimizing your queries and others.
Nhibernate allows you set the command timeout for query. This timeout is the length of time the query will be allowed to execute and finish. If the query takes more than this time,the query will be aborted and SQL timeout exception will be thrown.
Currently you can set the timeout on query if you are using ICriteria. The Nhibernate team is working to make this method (setting the timeout) available on QueryOver
I was using Nhibernate QueryOver
I will leave it like that until we completely remove that view and replace it with a roll up table in our next release.
Friday, December 28, 2012
SignalR and Interval Polling for Dashboard Update
B. SignalR
"~/signalr/hubs” – this is the path defined on your route. When the application is run, the signalR assemblies will use reflection to emit the javascript equivalent of the ReportHub class defined in the assembly.
Wednesday, September 5, 2012
Ghostery - Tracking the Trackers
Ghostery claims to work with more than a 1000 ad networks, behavioral data providers, web publishers, and other companies interested in your browser activity.
This browser plugin allows you to see what companies are tracking you (i.e. reading or writing tracking cookie) in your browser. This trackers are basically interested in your age,sex,shopping pattern, hobbies and interests so that they share that information to advertisers who want to run targeted ads.
Ghostery shows you the list of trackers with the information they track. You can only see the description of the company , what they do and links to their site etc. It also gives you the option to block advertisers, analytics and widgets etc. Thus putting you on the control of which companies you share your information with.
But beware that blocking trackers might produce an unexpected experience while browsing sites which rely on information availed through trackers to tailor your browsing experience.
You can install this plugin on the browser you are using . I tried it with Firefox and Google Chrome.
Click here to go to Ghostery site.
Tuesday, July 12, 2011
My experience with SmartGit
I was looking for a repository client for a Git repository. The first client I experimented with was TortoiseGit. I love tortoise products. I specifically used tortoiseHg in my last project. However, my experience with tortoiseGit was disastrous. I couldnot make it use the ignore patterns defined in .ignore or in the exclude file. Thus, after messing up with it for a while, I looked for other clients which are free for personal use and I stumpled upon on SmartGit, as you guessed it; It was love at first sight!
SmartGit is lovely, easy to use and has elegant user interface. I was able to install it and started using it in a heart beat.It reads and applies the ignore patterns listed in the .ignore file. However, it doesnot have the contextual menu to access repository commands by right clicking in a folder structure. You have to start the client in order to open the local repository to do your staging, commiting and other related stuff.
You can download and evaluate SmartGit here
Cheers!
Thursday, March 31, 2011
Cross domain page request using jquery
Have you ever wanted to display content from another domain into you site using client side scripts? At this moment you may already know it is not possible make ajax request to a different domain or subdomain using javascript, since it violates the same origin policy
Luckily, there is a work around to it.
You can achieve this by including a proxy page that is dedicated to making a server side request to the intended page in the other domain and return the contents retrieved as its own content. How cool is that? J
Then you can make ajax request to the proxy page using jquery and further filter the content using selectors and integrate it into your Page.
The code below demonstrates how to load content from www.example.com .
Proxy page: uses System.Net classes to make http request and receive the response .If the request is successful, i.e. Status Code is 200, and it will read the response stream from the cross domain request and return the content read in its response.
Proxy.aspx.cs:
using System;
using System.IO;
using System.Net;
namespace CrossDomainPageRequestSample
{
public partial class Proxy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// ‘loadUrl‘ is the address to the page in the other domain
var url = Request.QueryString["loadurl"];
// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
// make the request
var response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream
if (response.StatusCode == HttpStatusCode.OK)
{
var contentReader = new StreamReader(response.GetResponseStream());
Response.ContentType = response.ContentType;
Response.Write(contentReader.ReadToEnd());
}
}
}
}
Proxy.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Proxy.aspx.cs" Inherits="CrossDomainPageRequestSample.Proxy" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
div>
form>
body>
html>
DefaultPage:
This page makes Ajax request to the Proxy page and filters the content returned and appends to the div with id ‘otherSiteContent’.
$('#otherSiteContent').load('proxy.aspx?loadurl=http://www.example.com/ #header', function () { });
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="CrossDomainPageRequestSample._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js">
script>
<script type="text/javascript">
$(document).ready(function () {
// the script below loads the proxy page and further applies id selector #header and appends the content to
// the div #otherSiteContent
$('#otherSiteContent').load('proxy.aspx?loadurl=http://www.example.com/ #header', function () {
});
});
script>
asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
div>
Page Content from the other site goes below
<div id="otherSiteContent">
div>
asp:Content>
DefaultPage.aspx.cs
using System;
namespace CrossDomainPageRequestSample
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}