Monday, November 25, 2013

Patterns

Fire and Forget: Redirect load balancing request and let load balanced server take care of the rest
Map Reduce: Find top 10 web users across all locations
Fork And Join: Broadcast through a switch
Producer and Consumer: Buyers and Sellers

http://jz13.java.no/presentation.html?id=2cfe0083

Sunday, July 15, 2012

Control M characters in Windows

%s/.$// shall be used to remove control M characters in windows file. Edit the file using Vim and execute this to remove ^M characters.

Saturday, November 5, 2011

Memories !!!!

How will you feel when you meet a long missing friend?
How will you feel when you find a missing book?

I felt the same happiness when I came to be coimbatore after 11 years and 4 months for job. Yes I did my engineering in coimbatore and I left coimbatore in 2010. And I came back here at 2011 and never imagined that I will get a job here. But life is full of surprises and currently enjoying job and coimbatore. We all know the beauty of coimbatore !!! yes, its the sweet language.

Thursday, August 11, 2011

Working with Java Sound API

Recently had a chance to work with Java Sound API where I am able to play and record sound through my laptop speaker and microphone.

I used below code to play a wav clip.

public class PlaySound
{
     private void playClip()
    {
        try {
            File file = new File("Z:\\songs\\song.wav");
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
            AudioFormat format = audioStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class,format);
            Clip clip = (Clip) AudioSystem.getLine(info);   
            clip.addLineListener(this);
            clip.open(audioStream);

            clip.loop(5);
        } catch (LineUnavailableException ee) {
            ee.printStackTrace();
        }
        catch(IOException ee)
        {
            ee.printStackTrace();
        }
        catch(Exception ee)
        {
            ee.printStackTrace();
        }
    }


    public static void main(String a[])
   {
          new PlaySound().playClip();
   }

}

My bad, it was not working. After struggling to find the reason, finally found that 
Clip clip = (Clip) AudioSystem.getLine(info); 

is creating a new Sound Dispatcher daemon thread which expects some non daemon thread to run in the jvm. Finally added some Thread.sleep(5000) in the main method to keep jvm running and it started working.


Tuesday, July 26, 2011

Difference between Jsp:forward and jsp:include

When you do jsp:forward, pageContext.forward() method is invoked with the given jsp or html resource. When you do jsp:include, JspRuntimeLibrary.include() method is getting invoked. jsp:forward means, browser will show only the resource given as an input to jsp:forward. No new HttpServletRequest and HttpServletResponse objects are created hence all parameters/attributes in HttpServletRequest are passed to the new resource as well. jsp:forward does not change the Url in the browser but HttpServletResponse.sendRedirect() changes the url as well since the request is redirected to the browser and coming back to the servlet/action class.

Monday, June 27, 2011

Debugging a JBoss application

Recently had a chance to debug a Jboss application through eclipse. Below are the steps I did.

1. JBoss version is 6.0.0.
2. edited <JBossHome>/bin/run.conf.bat and added the below line
set "JAVA_OPTS=%JAVA_OPTS% -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
3. Started jboss server and got the below print
Listening for transport dt_socket at address: 8000
4. Then in the eclipse, select the project and then Run ----> Debug Configurations-----> Remote Java Application. On the right side, select the project, and set the connection type as Standard (Socket Attach), Host as localhost and give port as 8000. Click on Debug.
5. Then select Window ----> Show View ---> Debug, you will be able to see the stack traces.
6. Most of the standalone debugging operations i.e setting a break point, adding a watch would work in remote debugging also.

Friday, June 24, 2011

Dead Lock Example

How to create dead lock?  Here is the fine example.
Lets talk about money transfer from one account to another account. Money has to be debited from one account and it has to be credited into another account.


public class Transfer extends Thread
{
           private Credit credit  = null;
           private Debit debit = null;
           private boolean odd = false;

           public Transfer(Credit credit, Debit debit,boolean odd)
           {
                       this.credit=credit;
                       this.debit=debit;
                      this.odd=odd;
            }

            public void run()
            {
                       if(odd)
                      {
                                transferMethod1();
                       }
                       else
                       {
                                 transferMethod2();
                        }
             }

            //first lock credit and then do debit
           public void transferMethod1()
           {
                 credit.credit();
                 //thread.sleep() can be added just to ensure that dead lock is happening
                 debit.debit();
           }

          //first lock debit and then do credit
          public void transferMethod2()
          {
                   debit.debit();
                   //thread.sleep() can be added just to ensure that dead lock is happening  
                  credit.credit();
          }

          public static void main(String a[])
          {
                      Credit c = new Credit();
                      Debit d  = new Debit();
                      Transfer t1 = new Transfer(c,d,true);                     
                      Transfer t2 = new Transfer(c,d,false);
          }
}

class Credit
{
public synchronized credit()
{
//credit implementation
}
}

class Debit
{
public synchronized debit()
{
  //debit implementation
}

When thread t1 calls transferMethod1(), credit object is locked and its waiting for debit object. At the same time, when thread t2 calls transferMethod2(), debit object is locked and its waiting for credit object. t1 can proceed when t2 releases debit object and t2 can proceed when t1 releases credit object. Hence program hangs. One way to avoid this deadlock is, combine those two resources. i.e synchronize both these credit and debit operations. i.e in other words synchronize methods in Transfer object. Make transferMethod1() and transferMethod2() methods synchronized.