Wednesday, October 17, 2012

@Memoize in Java using Spring and Aspects

Recently, while writing an application in service oriented architecture, I had a couple of places in the code where I needed to make a service call to get the details by a particular application key. I was wondering about duplicate calls, i.e. multiple service calls made to get the details for the same application key. As we all know, service calls are costly with all the marshalling, unmarshalling, cache lookups, database lookups involved, I wanted to cache the results of a service call by application key or keys and this is a clear cross-cutting concern. Having programmed in Perl and Python that have the memoize option, I thought a Memoize aspect would be most appropriate here. As always, I was looking for Spring's Aspect Oriented Programming APIs and though it was tricky to get the set up right, I ended up with a very small amount of code that does the basic memoization. Since the set up was tricky, I thought I would document here for reference:


  1. Set up Spring, AspectJ, CGLib and ASM for the application.
    1. The following Spring jars are needed:
      1. spring-aop (v3.3.1)
      2. spring-aspects (v3.3.1)
      3. spring-beans (v3.3.1)
      4. spring-context (v3.3.1)
      5. spring-context-support (v3.3.1)
      6. spring-core (v3.3.1)
      7. spring-test (v3.3.1)
      8. spring-expression (v3.3.1)
      9. spring-asm (v3.3.1)
    2. The following AspectJ jars are needed for load-time-weaving and aspectj runtime.
      1. aspectj-rt (v1.6.9)
      2. aspectj-weaver (v1.5.4)
      3. aop-alliance (v1.0)
    3. The following CGLib jars are needed for code generation
      1. cglib (v2.2.3)
    4. The following ASM jars are needed for byte-code manipulation
      1. asm (v3.3.1)
    5. Commons logging jar needed by Spring
      1. commons-logging (v1.1.1)
  2. The CGLib and ASM libraries should be compatible with each other. So, if you choose a different version of any of these jars, make sure to select the compatible other one. Otherwise, you will end up with a runtime error (NoSuchMethodError).
  3. Enable aspects in your spring application using the following configuration:
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                ">
    
        <context:component-scan base-package="com.vikdor.aspects" />
        <aop:aspectj-autoproxy proxy-target-class="true" />
    </beans>
    
    
  4. Define the annotation that is going to be used in the advice:
    
    import java.lang.annotation.Retention;
    
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Memoize
    {
    
    }
    
    
  5. Write a simple Aspect class, annotate it with @Aspect and define an @Around advice for all the methods that use the @Memoize annotation, as follows:
    
    @Aspect
    @Component
    public class MemoizeAspect
    {
        private WeakHashMap<Object[], Object> cache = new WeakHashMap<Object[], Object>();
    
        @Around("@annotation(com.vikdor.aspects.Memoize)")
        public Object handledMemoize(ProceedingJoinPoint pjp) throws Throwable
        {
            System.out.println("Handling memoize");
            Object[] args = pjp.getArgs();
    
            if (args != null)
            {
                Object response = cache.get(Arrays.asList(args));
                if (response != null)
                {
                    return response;
                }
            }
            Object obj = pjp.proceed();
            if (args != null)
            {
                cache.put(Arrays.asList(args), obj);
            }
            return obj;
        }
    }
    
    
    
Now, add @Memoize to any method whose request and response should be cached and it just works. BTW, this is a very naive Memoize implementation and is not production ready at all ;-)

Sunday, October 7, 2012

Connecting to SQL Server Express through JDBC

I came across this question in a decent number of forums where the subtleties of connecting to an instance of SQL Server Express using JDBC are missed out. So, thought it would be good to just document how I approached it, here for my reference and for others, if they find useful.
Once you install the SQL Server Express edition, you need to configure the TCP/IP settings for the JDBC drivers to connect to the SQLEXPRESS database instance.

Configuring the TCP/IP:

  1. Configure TCP/IP communication with SQL Express
    1. Open SQL Server Configuration Manager.
    2. Go to SQL Server Network Configuration -> Protocols for SQLEXPRESS
    3. Set the status of TCP/IP protocol to "Enabled" (if it is already not).
    4. Open Properties window for TCP/IP, go to IP Addresses section.
    5. Go to the bottom of this property page and set the TCP Port under `IPAll` to 1433.
  2. Connect to the SQLExpress instance using `Microsoft's JDBC driver for SQL Server`
    1. JDBC URL: `jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>`

Using Windows Integrated Authentication:
  1. Create a windows account for the application that would be used to run your programs. This account's credentials will be used to connect to the server.
  2. Get Microsoft JDBC Driver for SQL Server from here.
  3. Configure the JDBC URL as follows:

    jdbc:sqlserver://<hostname>;databaseName=<DBName>;integratedSecurity=true
  4. Configure the launcher that run the Java programs from command line to include the following JVM parameter:

    -Djava.library.path="<jdbc driver dll location>"

    where the location is the directory where the JDBC driver downloaded earlier is installed or extracted. It was C:\Program Files\sqljdbc_4.0.2206.100_enu\sqljdbc_4.0\enu\auth\x64 in my case. The dll should be picked based on the JVM used for running these programs.

With the above configuration, the connection established to SQL Server would use the Windows Authentication Credentials of the domain user running the java program/process.

Sample Code:

 

public class SQLServerEx
{
    static enum Drivers
    {
        MICROSOFT(
                  SQLServerDriver.class.getName(),
                  "jdbc:sqlserver://"
                      + "localhost;"
                      + "instance=SQLEXPRESS;databaseName=vikdor_test;user=vikdor_user;password=vikdor_user"),
        MICROSOFT_INTEG(
                        SQLServerDriver.class.getName(),
                        "jdbc:sqlserver://"
                            + "localhost;"
                            + "instance=SQLEXPRESS;databaseName=vikdor_test;integratedSecurity=true");

        private String driverClass;
        private String driverURL;

        private Drivers(String driverClass, String driverURL)
        {
            this.driverClass = driverClass;
            this.driverURL = driverURL;
        }

        public String getDriverClass()
        {
            return driverClass;
        }

        public String getDriverURL()
        {
            return driverURL;
        }
    }

    public static void main(String[] args) throws Exception
    {
        Class.forName(Drivers.MICROSOFT_INTEG.getDriverClass());
        Connection con =
            DriverManager.getConnection(Drivers.MICROSOFT_INTEG.driverURL);
        PreparedStatement ps =
            con.prepareStatement(
                "insert into auto_table(name, role) values(?, ?)",
                Statement.RETURN_GENERATED_KEYS);
        ps.setString(1, "vikdor");
        ps.setString(2, "sde");
        ps.executeUpdate();
        ResultSet rs = ps.getGeneratedKeys();
        while (rs.next())
        {
            System.out.println(rs.getInt(1));
        }
        rs.close();
        ps.close();
        con.close();
    }
}


Saturday, October 6, 2012

Using Collection Merging in Spring to re-use a list

Recently, I came across a requirement where I had a set of rules to be run on various sub-types of a business object--Invoice. All sub-types shared a common set of rules and then each sub-type has some exclusive set of rules to be run along with those in the common set. I was using Spring for dependency injection and did it as follows, initially:

<bean id="rule1" class="com.vikdor.rules.Rule1" />
<bean id="rule2" class="com.vikdor.rules.Rule2" />
<bean id="rule3" class="com.vikdor.rules.Rule3" />
<bean id="rule4" class="com.vikdor.rules.Rule4" />
<bean id="rule5" class="com.vikdor.rules.Rule5" />

<util:list id="normalInvRules">
    <ref bean="rule1" />
    <ref bean="rule3" />
    <ref bean="rule5" />
    <ref bean="rule4" />
</util:list>

<util:list id="prepaidInvRules">
    <ref bean="rule1" />
    <ref bean="rule3" />
    <ref bean="rule5" />
    <ref bean="rule2" />
</util:list>
This obviously was not going to scale in my use case where I have good number of rules to be run and the number of sub-types are also more. I was wondering if there is a way to define the common list and then re-use it while defining the actual list of rules for a given type of invoice and then I came across Collection Merging concept in Spring where we can define a parent element and then have child elements inherit the properties from the parent element and override them (i.e. when there is a conflict, the child property definition takes precedence) and modified my configuration as follows and it works great!
<bean id="rule1" class="com.krovi.rules.Rule1" />
    <bean id="rule2" class="com.krovi.rules.Rule2" />
    <bean id="rule3" class="com.krovi.rules.Rule3" />
    <bean id="rule4" class="com.krovi.rules.Rule4" />
    <bean id="rule5" class="com.krovi.rules.Rule5" />

    <bean id="commonList"
        class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
            <list>
                <ref bean="rule1" />
                <ref bean="rule2" />
                <ref bean="rule3" />
            </list>
        </property>
    </bean>
    <bean id="firstExecutorRules" parent="commonList"
        class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
            <list merge="true">
                <ref bean="rule4" />
            </list>
        </property>
    </bean>
    <bean id="secondExecutorRules" parent="commonList"
        class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
            <list merge="true">
                <ref bean="rule5" />
            </list>
        </property>
    </bean>