Wednesday, September 23, 2015

Scala telescoping constructors

Wednesday, May 6, 2015

Weaving AOP

Problem:
Quite often you end up with a 3rd Party server being down and here is how you can use AOP to stub the requests and ensure that your code is performing well.

You can use Load time weaving(LTW) to weave during run time.




Define your aop.xml



Define your Aspect


Ensure that the class your are cross cutting(com.sample.ThirdParty.call) is in classpath

When you are trying to integrate it with a custom server, please note that you need to have -javaagent


for eg



Call vs. Execution

The rule of thumb is that if you want to pick a join point that runs when an actual piece of code runs (as is often the case for tracing), use execution, but if you want to pick one that runs when a particular signature is called (as is often the case for production aspects), use call.

While its fairly easy to run the LTW, quite often I ended up having issues with loading it and this might help.

How do I get load-time weaving to work in my chosen application server?

You have two choices based on how wide you want the weaving to take effect: application-server wide and application-specific weaving. You choose between the two by loading aspect artifacts--aspects, associated types, and aop.xml--through the right classloader. The aop.xml must be in the META-INF directory on the classpath for the chosen classloader. In either case, you modify the startup script to specify the -javaagent:path-to/aspectjweaver.jar option to the Java virtual machine. Note that it is not essential that all the artifacts be placed in a single jar.

For application-server wide weaving, you make aspect artifacts accessible to the server's classloader. Typically, you achieve such access by putting these artifacts in the server's lib directory. For example, for Tomcat, you will place the aspect artifacts in the TOMCAT_HOME/lib directory.

For application-specific weaving, you make aspect artifacts accessible to application classloader by bundling them along with application's classes. For example, for a web application, you will place the aspect artifacts in the MY_APP/WEB-INF/lib and/or MY_APP/WEB-INF/classes directory.

We recommend that you start with application-specific weaving. Note that you have an additional option if your application is based on the Spring framework. If you deploy in one of the supported web servers or application servers, you can avoid modifications to the startup script. Please see http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-aj-ltw-spring for more details.

Reproduced content from http://eclipse.org/aspectj/doc/released/faq.php#q:ltwAppServers

When you don't know if your weaving agent is loaded or not, use the below code in your flow. If you get any of the exceptions, you know your agent is either not loaded or not instrumenting.



Thursday, March 7, 2013

Synchronize and lock api

Can you mix Synchronize and Lock API

I was recently trying out mixing both synchronize and manual lock using lock apis. Interestingly, Synchronize locks the object but when you use Reentrant lock using API, it locks up the object too. Its generally not recommended to mix as Synchronize is a implicit way of locking. But you have to be extra careful when dealing this way. Refer to API : Docs
Snippet I tried.
 
import java.util.concurrent.locks.ReentrantLock;

public class Threads {
 static X x = new X();

 public static void main(String arg[]) {
  Thread tA = new Thread(new Runnable() {
   public void run(){
     x.addA();
   }
  },"T1" );
  tA.start();
  
  Thread tB = new Thread(new Runnable() {
   public void run(){
     x.addB();
   }
  },"T2");
  
  tB.start();
  Thread tC = new Thread(new Runnable() {
   public void run(){
    x.callMe();    
   }
  },"T3");
  tC.start();
 }
}


class X {
 private final ReentrantLock lock = new ReentrantLock();
 public  void addA() {
  synchronized(this) {
   System.out.println("A :"+Thread.holdsLock(this));
   System.out.println(lock.tryLock());
   
   
  }
 }

 public void addB()  {
  System.out.println("B :"+Thread.holdsLock(this));
 }

 public void callMe() {
  System.out.println("C :"+Thread.holdsLock(this));
 }
}
 

Friday, January 18, 2013

GIT Crash course


The below are primary commands of GIT and doesn't explore in detail and are just to remember and quickly go through them, perhaps bit more than a cheat sheet.
I have tried these in Ubuntu but they would be almost similar in all OS'es.

Installing in Ubuntu
 apt-get install git-core 


You can use them along with SVN too. Use
 sudo apt-get install git-svn

Setting up your identity

Sets your intial configuration at a global level. You can use /etc/gitconfig file for making these changes
     git config --global user.name   " Your Name " 
     git config --global user.email  "someone@domain.com" 
     git config --global core.editor  emacs
     git config --global merge.tool  meld
     git config --global user.email  "someone@domain.com"
     git config --list
 

Start with existing github.com

Sign up with github.com and you can start working on these command rightaway.
    git clone git://github.com/path/myproject.git 
The above url uses git protocol but you can use https if you are using remote repository like github.com

Basic commands

    git remote add github git@github.com:dddd/dd.git ( Adds the remote url so that you can checkin)
    git add .         ( Add the files or folder which sets the stage ) 
    git commit -m "commit comments" ( commits them locally )  
    git push ( pushes into the server)
    git diff 
    git tag
    git pull
When you add a file GIT stores them locally (rather STAGES them). Commit will commit them locally. Push will push to the server.

Branching and Merging

    git branch { branch_name }  ( Creates branches ) 
    git merge { branch_name }  (Merges files from the mentioned branch 
                   to current branch where you are firing this command) 
    git rebase { branch_name }  
    git checkout -b { branch_name } ( creates a branch and checks out. 
                               Helps switching branch too)  
    git log 
    git status  ( Shows branch names and staged files) 
    git cherry-pick rev ( For commiting a file to be merged from a 
                          different branch) 
The best part of GIT is the git checkout which helps switching branches almost instantaneously.

Stashing

Stashing helps in not committing to the repository but still save a copy of the changes you have made and switch to another branch. Get back to this branch and you can use Stash command to get back the changes you didn't commit earlier. List of commands associates are
 
     git stash  ( Stores a local copy of the changes without losing
                     the changes but doesn’t commit) 
     git stash list  
     git stash branch {branch_name} 
     git stash apply 
     git stash apply --index 
     git stash drop stash@{0} 

 

Tuesday, May 29, 2012

Quartz scheduler with Tomcat

How do you use Quartz Scheduler with Tomcat ?

I initially planned to use Quartz Scheduler as a standalone batch. But the bigger problem turned out to be UI for the scheduler. While there are some like Citrine and myscheduler available, I didn't quite feel comfortable with it. Here is how I designed a simple UI. I referred to one of the blogger neopatel to create this sample.

Steps as below
1) Create a JobRegistry which extends QuartzInitializerServlet. Override the doGet and schedule your job.
2) Create a web.xml with entry as below

<servlet>
        <servlet-name>QuartzInitializer</servlet-name>
        <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
        <init-param>
            <param-name>shutdown-on-unload</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

<servlet>
        <description></description>
        <display-name>JobRegistery</display-name>
        <servlet-name>JobRegistery</servlet-name>
        <servlet-class>com.amazon.tool.main.RegisterJob</servlet-class>
    </servlet>

2) Create a JSP for front end to access the various jobs.
            List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
            String jobName = null;
            Object groupName1 =null;
            for (JobExecutionContext jobCtx : currentJobs) {
                jobName = jobCtx.getJobDetail().getKey().getName();
                groupName1 = jobCtx.getJobDetail().getKey().getGroup();
             }

That's it. You can design a nice UI with the sample above. Please note that you have to hit the JobRegistry servlet once to schedule the job. If you don't want to manually hit it, which will typically be the case, you can register your scheduler during startup by having it in web.xml
Here is the link for the source.

I used Tomcat 6 and Quartz scheduler 2.1 for dev

Enjoy