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.

Sunday, June 5, 2011

Regex to find Email

Wrote a regular expression to find e-mail which has below conditions.

1. User name must start with a character.
2. It can contain _-. characters but not two consequetive hypens (--). It can contain numbers.
3. Domain name must start with a character or a number it can have any number of subdomains i.e .co.in etc

(\b[a-zA-Z][\w\.^[--]]*)@([a-zA-Z][\w^[--]]*)(\.[\w^[--]]+)+

My idea is to group email as username,domainname. First lets take username. This can be identified by (\b[a-zA-Z][\w^[--]]*).

\b means it should start with a word boundary. It should start with a character, so the character class [a-zA-Z] confirms the same.  Then it could contain any number of characters but not two hypens which can be achieved by a word character \w (a-zA-Z0-9) and a character class ^[--].
Email can have any number of sub domains i.e .co.in. so I split it as (.\w) i.e a . followed by a word character. And the rest is self explanatory.