Recursion Depth - Java

Recently, in a programming competition, I made a big mistake by using recursion for solving one of the problems, as it resulted in 'StackOverflow' error and lost the chance to solve the problem for a larger input dataset. I realized it immediately and implemented a normal solution, but there wasn't much time to provide the solution.

In Java, the JVM stores the method information (Variables, Return Address, etc) and its parameters in the Stack. So, when you use Recursion, they are constantly pushed to the stack memory, until the result of that method is returned. Usually, Stack has limited memory and is much less than the Heapsize.

Below is a Java program, from which you can find the 'Recursion Depth' of the JVM that you are using:

The current 'Recursion Depth' for the JVM on my System is: 9372

The 'Recursion Depth' depends on the present state of the Stack Memory usage, on your JVM. So, it might change, everytime you run it.


1) My own post on StackOverflow site: Basic Java Recursion Method

RESTFul Web Service - Seam Component Injection

If you have to incorporate a RESTFul webservice into an existing (or new) Web application, which uses SEAM as one of the technologies in it's tech-stack, then you probably want to use Seam component injections in your RESTFul webservice.

If your RESTFul service's URI's are something like:

a) GET /rest/api/get?id=1&user=test
b) POST /rest/api/create
c) PUT /rest/api/update
d) PUT /rest/api/delete

Then, for the Seam injections to work, you need to add a 'Seam Web Context filter' in your Webapp's Seam components.xml file, as shown below:

<web:context-filter url-pattern="/rest/api/*"/>

An alternative to adding web:context-filter is to add a 'Seam Servlet Filter' in web.xml of your web application, as shown below:

    <filter-name>Seam Servlet Filter</filter-name> 
    <filter-name>Seam Servlet Filter</filter-name> 

The reason you need to add these filters is to make Seam intercept your request, initialize itself and then allow the injections to work. Similarly, EJB interceptors are needed for injections to work in EJB's. As for JSF pages, Seam uses EL Resolvers for dependency injections. These EL Resolvers are installed by default, when Seam's libraries (jars) are included in the classpath.

BTW, depending on how I use the Seam component, instead of using @In, I prefer using Component.getInstance("seamComp");

If you are using multiple URL patterns (different URI's) for which you want Seam injections to work, you can use "regex-url-pattern". For example, if you have want to create a new SOAP webservice, along with your RESTFul service...and you want the Seam injections to work in both the services, you can simply use the 'regex-url-pattern' attribute for 'web:context-filter' tag.

SOAP Service URI : TestSOAPService (Enpoint Address URI: webappContext/TestSOAPService)
REST API URI's : /rest/api/*

Then, your 'web:context-filter' tag in Seam components.xml file should look like:

<web:context-filter regex-url-pattern="/rest/api/.*|/TestSOAPService"/>