30 July 2013

Simplest iterative algorithm Post order traversal

public class POIterateSimple {
public static void main(String[] args) {
    pfIterate(PFIterate.NODE);
}
private static void pfIterate(Node root) {
    Node prev = null;
    Stack stack = new Stack<>();
    stack.push(root);
    while (!stack.isEmpty()) {
        root = stack.pop();
        if (root.left == null && root.right == null) {
            System.out.print(root.data + " ");
            prev = root;
        } else if (prev == root.left || prev == root.right) {
            System.out.print(root.data + " ");
            prev = root;
        } else {
            stack.push(root);
            if (root.right != null)
                stack.push(root.right);
            if (root.left != null)
                stack.push(root.left);
        }
    }
 }
}

24 July 2013

How to inject multiple endpoint in SEI using Camel's @EndpointInject

By default you can inject annotation on single method of an interface like:

public interface MyListener {

  @EndpointInject(uri="activemq:foo.bar")

    String sayHello(String name);

}

what if you need multiple methods like this with @EndpointInjection happening over them for each endpoint e.g.:

public interface MyListener {

  @EndpointInject(uri="direct:foo")

    String sayHelloFoo(String name);

@EndpointInject(uri="direct:bar")

    String sayHelloBar(String name);

}

The simple solution i have working involved spring's FactoryBean implementation.

It need following steps:

<!-- Define route for which you need injection to happen -->

<bean id="r" class="MyListener" />

<!-- Define producer template having that route  -->  

<camelContext  xmlns="http://camel.apache.org/schema/spring">

        <template id="producerTemplate" />

        <routeBuilder ref="r"/>

    </camelContext>

 <!-- define FactoryBean for proxy instance --> 

<bean id="definedCamel" class="CamelFactoryBean">

        <constructor-arg index="0" value="MyListener" />

        <constructor-arg index="1" ref="producerTemplate" />

</bean>

The FactoryBean will create instance of custom InvocationHandler and return it which will invoke the endpoint on the ProducerTemplate instance.



27 February 2013

Use recipientList for dynamic routes

I was writing the route in which "to" endpoint was need to be configured dynamically with the URL coming in the body.

I was trying to use simple expression language (e.g. ${in.header.test} where test is the property set in the in-message header) also with header method (e.g. header(test)). The routes for the same were:

from("direct:test").to("${in.header.test}");

&

 from("direct:test").to(header("test"));
 
after literally every thing i could have. I figured out recipientlist can do the trick e.g.

from("direct:test").recipientList("${in.header.test}");

&

 from("direct:test").recipientList(header("test"));

Hope this works for you too.

20 February 2013

Spring: implements interface not working in @Controller

Guys,  I had a @Controller in which I was trying to implement an interface. But i was getting the following error message:

PageNotFound WARN  No mapping found for HTTP request with URI [/test/form] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'


where "/test" (at class level) and "/form" (at method level) where my @RequestMapping args.

 The issue is described at following link:

http://forum.springsource.org/showthread.php?92303-Spring-Servlet-MVC-RequestMapping-breaks-with-AOP-Advice




Now you may not find anything wrong with your spring.xml but should check all the xml used by used in the project in out case metrics was causing the problem. 

The aop config was overridden by metrics here:

https://github.com/ryantenney/metrics-spring#xml-config

 By default JDK proxy are created using interface and if controller implements an interface the RequestMapping annotation gets ignored as the targetClass is not being using.

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-proxying

 So, fix required i my case is:

proxy-target-class="true" ... />

Hope this helps.

20 September 2012

60 days at expedia India

I am about to complete 60 days in expedia and till now the ride has been fun.

I joined expedia hoping challenging work, participating in building a good team and workplace full of geniuses. To be frank everything is as close to reality as i thought.

The experience is amazing, people are awesome, the new office is great, parties are crazy, but the most important think is freedom to express yourself and asking questions. And people around you listen and have enough patience to make you feel comfortable ;) .

Work wise finishing user stories (part of on-boarding process), building a team by interviewing brightest mind in the industry and ideating about new ideas to make it a better workplace. And to emphasis working on one of the biggest ecommerce platform is not easy. Its a great earning opportunity and even bigger to contributing in one of the fastest growing industry.

I hope this answers whether you should join here or not.

Looking forward to see on board ;).

18 July 2012

Java references in nutshell

Java developers struggle to use java references and i don't blame them. The topic seldom covered by text books and even code reviews rarely give emphasis on usage of references. Why the topic is important at all? and if everybody (atleast people i have come across) is coding without it why the hell you need it?

Since you are reading this, either you are already aware of them or for the interview. Anyways, knowing references helps you manage memory better because you are able to define the behavior of the object when GC (garbage collector) is run.

Type of references:

1. Strong Reference: The normal reference in the java code is strong reference and the object referenced by strong reference is eligible for GC as soon as scope ends or reference started pointing to null in the code.
E.g. Person p = new Person();
                p = null; // will make object initialized above eligible for GC
2. Weak Reference: As name suggest these reference are weaker than the strong references.  Their are couple of ways to use them one via WeakReference call which can wrap your object and you can access the object through WeakReference.get API. Get API will be returning null id the object is GCed. (You can also use WeakHashMap)


Usage: These objects stay in the memory till the time they are referenced from any strong reference in the memory. In other words as soon as they loose accessibility from strong reference they become eligible for GC.

3. Soft Reference: Soft reference is similar to weak reference however weaker than weak references. They put the additional check on there GC eligibility, which is if GC needs space.  So basically they sits in memory still the time memory is available. Use the SoftReference API for this type of references.

Usage: These find good usage in building cache because if memory is available they don't discard the object and hence saves you the additional reload.

4. Phantom Reference: This type of reference is used to track the object if it has been garbage collected. PhantomReference get API always returns null. Object cannot be obtained from it unlike finalize() API.


Usage: So, if your object needs to be freed only when object using it becomes garbage collected than you should be using this API. Another use case is before loading  large object you want to check if the other larger object currently in memory is GCed which will help you avoid the out of memory exception.

Hope this helps :)



  

29 March 2012

CXF-RS adding custom response code



Following snippet show how to return the custom Response code as http response code while returning the response:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;


@Path("/")
@Consumes("application/xml")
@Produces("application/xml")
public class Test  {

public  Test () {
}

@POST
public Response test() {
Response response = Response.status(4xx).entity("Testing").build();
return response;
}
}