Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Tuesday, May 3, 2016

You're Killing Me Spring: "Singleton" Scope

Introduction

I generally try to avoid traditional Gang of Four Design Pattern Singletons.  In Java they are only guaranteed to be singular per running Java Virtual Machine, don't scale to a clustered environment, can be serialized / deserialized to create a duplicate even in the same JVM and are generally the OOP version of global variables.
However, in a given project I had an exception to this rule.  I had an object that was using a single TCP port going into an environment where only one instance would be deployed per machine so I thought to myself that I would use the Singleton pattern.

Background

Created at imgflip, original image copyright Dos Equis.
I have had problems with Spring before.  Specifically I was working with Spring Web Flow 3 years ago and had some configuration (including setters) in my beans.xml file, in my Spring Web Flow xml file I imported the (Singleton) beans defined in the beans.xml file.  However, instead of importing the bean it created a new instance of it, and on top of that it didn't call any of the initialization configuration in the beans.xml file leading to null pointer exceptions.  It took me three weeks to track this down.  I was working for myself (e.g. that time was on my own dime) and I ragequit Spring in favor of Google Guice.  For this project I was forced into using Spring so I had the following Hypothesis.

Hypothesis

"Verify that Spring is treating Singletons correctly because it's been unreliable, surprising and buggy in the past."  In code-form it was a standard Spring @Service in the default Singleton scope with a check in the constructor (see Further Reading for other reasons to do this) to blow up if the constructor is called twice.  In addition, Spring makes you have a public constructor even for your Singletons so blowing up can avoid programmer error of someone later trying to just call new ExampleSingleton.
@Service
// implied Spring Singleton Scope
public class ExampleSingleton {

    // should only be called once by Spring
    public ExampleSingleton() 
    {
        if (instance != null) {
            throw new IllegalStateException("Singleton constructor called twice!");
        }
    }
}    

Results

As you may have guessed from the image, Spring was attempting to create the "Singleton" repeatedly and blowing up with the IllegalStateException.  From doing some research I found that @ComponentScan was behaving strangely.  It turns out that if you have multiple Spring @Configuration classes and if they overlap on a package (e.g. @ComponentScan(basePackages = {"com.yourcompany"}) and another with @ComponentScan(basePackages = {"com.yourcompany.utils"}), Spring will (re)create all of the Singletons in the overlap (in this example com.yourcompany.utils) twice.  I found this surprising and very strange.  In addition, the same link mentions that Spring only promises that a Singleton will happen once per ApplicationContext, and Servlets usually have more than one.

Conclusion

I concluded that you can't trust Spring to honor the @Singleton contract and to manage my instances myself.  After doing more research I found that in Java the best way is to have a SingletonFactory.  I ended up having a base class called SpringSingleton, which has the documentation on why all of this is necessary and a protected constructor that keeps track of which sub-classes have been instantiated with a Set<SpringSingleton> that is accessed in a synchronized block.  In addition the @Configuration classes have an @Autowired SingletonFactory and @Bean definitions that get the beans from the SingletonFactory.  All in all it still isn't foolproof for Serializable Singletons (see below) but does a great job otherwise.  There is more complexity (4 classes for the simple case) but new Singletons end up being easy to implement and work correctly.  I would post the code but I made it at work so the company has copyright.

Too bad I couldn't just have @Component on a class with the default scope and have it work right.

Further Reading

You can abuse an enum in Java to ensure a "serializable" Singleton can't be easily duplicated but that has some drawbacks.  Given that you can invoke even a private constructor via the Java reflections API you will still want to verify that your Singletons are only being called once (or at least post a warning in the logs).

PS

There was an issue with the site CSS where black lines would appear over an image. This has been fixed.

Tuesday, March 22, 2016

Spring Annotations: Using Constructor Arugments with @Autowired


Introduction

I've struggled for weeks with wanting to use a constructor with arguments along with Spring Annotations (in particular @Autowired) but most blog posts and tutorials say that this isn't possible or needs to use Spring Expressions.

Hypothesis

There has got to be some way to do this.  The alternative I have been using was to have an empty object autowired in and then manually run a bunch of setters to inject the required configuration.  This ends up being error prone and verbose to verify (e.g. every public method has to check that the required fields are not null).

Results

After searching the web for a long time I finally found that you CAN use multi-argument constructors if you refrain from using the @Component or @Service annotations and instead create the classes in a @Configuration class with @Bean annotations.  After creating a class (manually, with new) you can autowire it by using AutowireCapableBeanFactory.autowireBean(Object) method.

Conclusion

My code is a lot more readable and simple now, and let me find a few subtle bugs that did not turn up in the unit tests.  I ended up with complex object creation logic, but it is out in the @Configuration classes instead of inside of the core objects.  I am much happier with this situation.

Tuesday, February 16, 2016

Are these Images Similar? Simple Machine Vision with a Perceptual Hash

From https://evelyngarone.wordpress.com/2011/11/17/cute-and-funny-cats/

Background

At my job I had an actual requirement to tell if a given image was similar to a previous image (to detect a bad video input).  That was it, no background or suggestions on how to go about it.  This sounded a lot like Machine Vision to me, which is somewhere under the Artificial Intelligence (AI) umbrella.

I promptly started to freak-out (see image at right).

Intro

I quickly came across the concept of a Perceptual Hash during my first Google searches.  This led to finding first a C (++?) open-source library called pHash.  This prompted a further search for a Java open source library, which led to a Stack-Overflow question and a small Java class to take care of the heavy lifting.  The docs of this further linked to another source of inspiration on Hacker Factor for that author.

Hypothesis

This class, ImagePHash by Elliot Shepherd, will work as-is without me having to delve into the gory details too much (the Hacker Factor link provided an excellent overview).

Results

It worked really well!  I just put in some tweaks to Springify it (see Java Papers tutorial on Spring annotations for details) and use the logging framework the project uses instead of System.out calls and I was getting back the "distance" between two perceptual hashes in no time.

I still had to interpret these results because my code base needed a yes-no answer.  So I downloaded about 5 images from the Internet that were similar to my original image, and cropped the original image as well.  I found that a distance of 8 was a good cut-off for sameness, e.g. for a distance of 8 or below I would consider the images the same.  This would count the cropped image as the same, but not similar but looks different to me images.

I would be more specific but it was done on company time so I can't go into details too much (see also: Non-disclosure Agreements).

Conclusion

The whole loop took about a day and wasn't too scary once I go into it.  I'm glad that I didn't try to re-invent the wheel and that the class worked!

Wednesday, June 6, 2012

NOW I Get It: Aspect Oriented Programming

After going through the Aspect related chapter of Spring in Action the concept of Aspects finally "clicked" for me.  This is after reading the Wikipedia on it, the Spring documentation and various other articles as well.

To summarize:

  • Some things (like logging) need to be executed everywhere, even in modules who's primary purpose isn't logging.  Aspects address this issue.
  • An Aspect is just a bit of code (with Spring it can even be a POJO) that is configured (typically with an annotation or XML) to run before, after, or around a join point (for now, assume it's a fancy word for method).
  • For the canonical example of logging, you can create an aspect to log something like "Calling method x with parameters x, y and z" before a method runs and "Returning from method x with return value y" afterwards.  
    • This separates the logged class from the logging system entirely and results in a second class logging the first one.  
    • If you want logging statements in the middle of a method, too bad, refactor to call a helper method (thus creating a new join point) and log there.
Hopefully presenting the gist of it with concrete examples will help everyone's understanding.

Wednesday, May 30, 2012

Testing, Testing and Testing

You hear it in real estate: location, location, location.  You hear it (not as much) in software: test, test, test.  However, in software the tests are all different.

A recent project that I'm on involves a venerable app with as much technical debt as the US Government... and we need to add a feature.  Solution: tests, tests and tests.  More specifically if you don't know the different kinds of tests:

  • Good old Unit Testing, easy to implement and understand, unless...
    • You are getting used to mock objects (e.g. JMock or Mockito)
    • You are testing database functionality and are new to DBUnit
    • You are testing a web app or Servlet and are new to HttpUnit
    • You're confusing unit testing (ONE module) with integration testing (multiple modules)
    • Example: testing your Data Access Layer (DAL) with a dummy DB, like a local HSQL
  • Integration Testing, usually implemented with a framework that ends in "Unit" so it's easily confused with unit testing.  Integration testing integrates two or more components, everything from two classes to the entire app with a dummy DB.  Due to this you can have multiple levels of integration tests, each testing more modules together.  Examples include:
    • Testing a servlet in HttpUnit's ServletUnit instead of your actual web container
    • Testing your DAL with an "actual" database (whatever actual means to your project)
    • Testing a fully created Spring bean with the real beans injected instead of mocks
  • System Testing, this is tests of the entire system
    • QA usually conducts manual system tests
    • There can be automated ones as well with things like Selenium
    • As an example for a web app, using a supported browser to use the app in the final web container and using the real database.
There is of course, much more to software testing, but these three are usually a good start.  Heck, in many organizations / projects even using unit testing is new to people.  Like Regan said: trust, but verify.

PS: There's a good post at EvilTester with a more philosophical approach to testing.

Friday, February 17, 2012

Spring and null ExternalContexts

After brief consternation of getting null values for Spring's ExternalContextHolder.getExternalContext() and RequestContextHolder.getRequestContext().getExternalContext() I realized that I was calling this code outside of Spring entirely!

From double-checking the docs I guess it seems that this is considered a "common sense" feature and isn't mentioned.

My only solace in the dreaded NullPointerException is that even the inventor doesn't like them.

Friday, December 9, 2011

SpEL in JSF

I've been having an awful time getting SpEL to register in my JSF .xhtml pages.  I just want to do a simple

...

However, my project would give me an error saying that '(' was unexpected. This was despite the Spring Documentation saying that SpEL is the default expression language.

I made some head way when I put the following into my spring configuration xml:






This should work with JSF 2.0, but my project uses JSF 1.2 so I got a cast exception since JSF 1.2 uses the org.springframework.binding package instead.
The following should work with JSF 1.2 (I'll test this soon).




  
    
    
  
  
  

If you're using Spring Web Flow (SWF) then you'll need a different expression parser; use org.springframework.faces.webflow.FacesSpringELExpressionParser with the same constructor-args.
Note how it wraps the new SpelExpressionParser.

I've done thorough Google searches on this topic and found nothing, so as far as I can tell this is unique information!

Thursday, November 17, 2011

Not as Easy as it Looks, Part 2

I also tried upgrading from OGNL to SpEL on a Spring project recently and was surprised.

At first I thought that SpEL wasn't really a drop-in solution when I got some initial errors after switching, but SpEL (in depth intro at JavaBeat) actually caught a bug at compile time that OGNL (wiki) would only notice at runtime (and thus wasn't found before).

At least configuring Spring Web Flow for this is easy!

Thursday, November 3, 2011

GRASP in Spring

TheServerSide has a very interesting link to an article on JavaDepend about how Spring is designed internally (go open source!)

The gist of it is the Spring is designed well internally, the JavaDepend tool looks really useful and that if you're a developer and don't know what GRASP is, you should read the Wikipedia on it.