Mattias Severson

Mattias is a senior software consultant specialized in Java development. With a background in the hardware an embedded area, Mattias has shifted his focus to Java and the enterprise domain. He is a clean code proponent who appreciates Test Driven Development and Agile methodologies. Mattias collaborates well with others and has experience from many different environments, including everything between big international projects that last for years and solo, single day jobs. He is open-minded and curious about new technologies. Mattias believes in continuous improvement on a personal level as well as in the projects that he is working on.

About ExecutorServices

Dealing with asynchronous programming is not easy, at least not if you are a Java programmer. Not only do you have to take care of Callables, Runnables, Futures and likes, you also need to configure the details of how they should be executed by providing an ExecutorService. The Executors class provides some static factory methods that can be used, at the expense of being less configurable. Cached Thread Pool If you have short-lived asynchronous tasks, you may find the Executors.newCachedThreadPool()…

Spring @PropertySource

If you are a Spring developer and and you have created a Java based container configuration, you have probably come across the @PropertySource annotation for importing resources. It was first added as part of Spring 3.1, but Spring 4 has added some welcome improvements. Basics Let’s start with a simple example: When executed, properties will be imported from the application.properties file, located in the classpath root. classpath is the default location, and can thus be omitted: Alternatively, it is possible…

Spring and Autowiring of Generic Types

Spring 4 brings some nice improvements to how autowiring of generic types are handled. Before going into details, let’s look into the current state of affairs. Basics Consider the following example where you have one generic interface: And two concrete implementations of it: Now, the task is to create a FooService that autowires an instance of the FooDao class. Using current Spring versions, i.e. versions 3.x, one can either tie the implementation of the FooService class to the FooDao class:…

Improve Your Spring REST API, Part IV

Spring 4 is around the corner. Milestone releases has been available for a while, the first release candidate was released earlier today, and the final release is expected before the end of this year. @RestController One new feature that has been added is the @RestController annotation, which provide some syntactic sugar to your controller. Take a look at the following code snippet: Did you notice anything different? Ok, there is the new annotation on the class, an autowired dependency, a…

SpringOne 2GX 2013

Working Efficiently with Maven Modules

Getting started with Gradle

Did you know that there is a Java build system that does not use angle brackets? More and more projects are using Gradle as an integrated part of their development process. In short, Gradle provides a Groovy DSL for declarative builds based on build conventions. This blog post aims to give you a kickstart if you want to start exploring Gradle yourself by introducing some core concepts and commands. Installation If you have not used Gradle before, you must install…

Confess 2013 Conference Report

The four of us recently visited the Confess, CONference For Enterprise Software Solutions, in Vienna. It was located at the Messe Wien Exhibition & Congress Center which was a nice place for the conference. Here’s a summary of some of the sessions during the conference. Couchbase – Michael Nitschinger Michael gave an overview of Couchbase and in particular the new features of the 2.0 version. Couchbase combines the features of an in-memory data grid and a document database with secondary…

Scandinavian Developer Conference report

Improve Your Spring REST API, Part III

Improve Your Spring REST API, Part II

In the previous blog post, I explained how a custom @ExceptionHandler can be used to return feedback to REST API clients when they are submitting erroneous requests. However, the suggested implementation does not scale, because an @ExceptionHandler method is only applicable for a specific controller. In a real project, it is likely that we would like consistent error handling across multiple controllers. The easiest way to overcome this problem is to create a common super class that can be extended…

Improve Your Spring REST API, Part I

After watching Jonathan Dahl’s presentation about API design from the Øredev conference last year, especially the parts about Smart Validations, it is apparent that you can do much more to help your client developers than just returning HTTP status code 500 – Internal Server Error, when a request to your REST API fails. Spring WEB MVC The Spring Web MVC framework is a great tool for creating restful web services. It takes care of request mapping, validation, serialization and de-serialization…

Spring Controller Tests 2.0

Spring has many tools in the toolbox to facilitate testing. However, when it comes to testing a Controller, the tools have been a bit blunt. To make life easier, Sam Brennan and Rossen Stoyanchev presented the spring-test-mvc framework during a session at SpringOne 2GX last year. It provides a Java DSL that enables white box Controller testing of request mappings, response codes, type conversions, redirects, view resolutions, and more. This post will briefly introduce how a Spring powered REST Controller…

Continuous Deployment, Versioning and Git

So you have heard about continuous delivery and continuous deployment and you are eager to try, but your manager is afraid that you will lose traceability of the project. How can frequent updates of your binaries be tracked in a rapidly progressive environment, where every commit is a potential release? Artifact Version vs. Commit The traditional Java community way of version handling with a three digit number, potentially followed by a build number or a qualifier, e.g. 1.2.3.RELEASE, works well…

Mockito and Dependency Injection

When writing your Java unit test you will soon need a way to handle the dependencies of your classes under test. Mockito have some nice features that simplify the creation and usage of mock objects that have improved gradually during the last couple of years. Class under test Imagine that you write an Example class that has a Delegate dependency. It may be implemented as a POJO with constructor injection: Alternatively, it could be rewritten using setter injection based on…

Spring Integration Tests, Part II, Using Mock Objects

In the previous post, I wrote how you can use Spring’s FactoryBean to facilitate the creation of mock objects for Spring integration tests. Now, it is time to use the EasyMockFactoryBean (in this post EasyMock has been used for creating mock objects, but a similar approach applies to Mockito as well. Start by looking at the MockitoFactoryBean). Next, imagine that the Facade class and the Delegate interface below are part of a bigger system: Create a matching easymock-test-config.xml (or mockito-test-config.xml)….

Spring Integration Tests, Part I, Creating Mock Objects

When writing integration tests with Spring, it can sometimes be convenient to mock one or more of Spring bean dependencies. However, during some circumstances strange things may happen… (In this post, Mockito has been used for creating mock objects, but the same problem applies to EasyMock as well. You can find the corresponding files if you follow the provided links.) Test setup In the following example the SomeClass needs a reference to an instance of the SomeDependency interface: When writing…

IntelliJ IDEA performance improvement

Working as a consultant, it is not unusual that I am referred to customer specific software environment with regard to computers, operating systems, networks and other configurations. However, since I work with Java, most tools are available online and they can easily be downloaded and installed on different platforms. IntelliJ IDEA is no exception, but before you start coding you should make sure you know how the home directory of your computer is setup and configure IntelliJ IDEA accordingly. From…

Dynamic FTP Client using Apache Camel and Spring

I was recently asked to develop an FTP client that could transmit files to various FTP servers as a part of a delivery system in a Java enterprise application. The requirements dictated a flexible implementation: Three different FTP protocols should be supported, namely FTP, FTPS and SFTP It should be possible to transmit different files to several different servers The files to be sent were generated in runtime, they had different file names and were stored in different directories Basically,…

Architectural Enforcement with Aid of AspectJ

After working some time within the software industry, you get a feeling for good software architecture. Or, to be more honest, you get a creeping feeling when the architecture is really bad. That is when the code is tangled like a Gordian knot. After some futile refactoring attempts, you consult the software architect at your company and you will be given a design document stating the architectural principles that should be obeyed during software development. It is a nifty piece…

Getting started with JavaME jUnit testing

Introduction Unit testing is a very powerful tool that should be included in every developer’s toolbox. Unfortunately, this has not always been the case, especially not among MIDlet developers. One reason is that JavaME projects usually are small (compared to Java SE projects), which implies that manual testing could be enough. However, as soon as you start developing production code you really should consider to automate your tests by developing your own test suite. Some of you may have played…

How to mock MIDP RecordStore

The challenge PowerMock is a mocking framework that claims to have almost supernatural powers. According to its documentation it is able to mock both static and private methods, final classes, and other nasty things that would be insurmountable obstacles for other mock frameworks. As a result, it has been stated that it should be able to mock the MIDP RecordStore, but so far I have not seen a working example. I accepted the challenge. Getting started I decided that writing…