Archive: 2016

Ratpacked: Using Spring As Component Registry

Posted on by  
Hubert Klein Ikkink

Usually in our Ratpack application we use a registry to store components that we want to use in our application code. The calling code for the registry doesn't need to know how the registry is implemented. Ratpack support Google Guice for example, but Spring is also supported. This means we can define the components for our registry using Spring and we only have to tell Ratpack where to look for the Spring configuration files. Ratpack provides for us the ratpack.spring.Spring class with the static method spring. This method returns a Ratpack registry implementation we can use in our application.

To enable Spring support we must add the Ratpack spring-boot dependency to our build file:

Continue reading →

Building your own self refreshing cache in Java EE

Posted on by  
Jeroen Resoort

If you have read my previous post about caching, The (non)sense of caching, and have not been discouraged by it, I invite you to build your own cache. In this post we will build a simple cache implementation that refreshes its data automatically, using Java EE features.

Let's describe the situation. We are building a service that uses an external resource with some reference data. The data is not frequently updated and it's allright to use data that's up to 1 hour old. The external resource is very slow, retrieving the data takes about 30 seconds. Our service needs to respond within 2 seconds. Obviously we can't call the resource each time we need it. To solve our problem we decide to introduce some caching. We are going to retrieve the entire dataset, keep it in memory and allow retrieval of specific cached values by their corresponding keys.

Continue reading →

Groovy Goodness: Creating Files And Directories With Nice DSL Using FileTreeBuilder

Posted on by  
Hubert Klein Ikkink

Groovy has a lot of nice and useful gems. One of them is the FileTreeBuilder class. With this class we can create directories and files using a nice DSL with a builder syntax. The code already reflects the hierarchy of the directory structure, which makes it so more readable. We can use an explicit way of referring to methods in the FileTreeBuilder class, but there is also support for a more dynamic version, where Groovy's dynamic nature comes to play.

In the first example we use the explicit method names. We can use the method dir to create directories and the method file to create a file. We can specify a name for the file and also contents:

Continue reading →

Grails Goodness: Running Tests Continuously

Posted on by  
Hubert Klein Ikkink

When we write software it is good practice to also write tests. Either before writing actual code or after, we still need to write tests. In Grails we can use the test-app command to execute our tests. If we specify the extra option -continuous Grails will monitor the file system for changes in class files and automatically compiles the code and executes the tests again. This way we only have to start this command once and start writing our code with tests. If we save our files, our code gets compiled and tested automatically. We can see the output in the generated HTML report or on the console.

Suppose we have our Grails interactive shell open and we type the following command:

Continue reading →

Grails Goodness: Use Random Server Port In Integration Tests

Posted on by  
Hubert Klein Ikkink

Because Grails 3 is based on Spring Boot we can use a lot of the functionality of Spring Boot in our Grails applications. For example we can start Grails 3 with a random available port number, which is useful in integration testing scenario's. To use a random port we must set the application property server.port to the value 0. If we want to use the random port number in our code we can access it via the @Value annotation with the expression ${local.server.port}.

Let's create a very simple controller with a corresponding integration test. The controller is called Sample:

Continue reading →

Gradle Goodness: Get Property Value With findProperty

Posted on by  
Hubert Klein Ikkink

Gradle 2.13 added a new method to get a property value: findProperty. This method will return a property value if the property exists or null if the property cannot be found. Gradle also has the property method to return a property value, but this method will throw an exception if the property is missing. With the new findProperty method and the Groovy elvis operator (?:) we can try to get a property value and if not found return a default value.

In the following example we have a task that tries to print the value of the properties sampleOld and sampleNew. We use the findProperty for sampleNew and the property method for sampleOld:

Continue reading →

Grails Goodness: Change Version For Dependency Defined By BOM

Posted on by  
Hubert Klein Ikkink

Since Grails 3 we use Gradle as the build system. This means we also use Gradle to define dependencies we need. The default Gradle build file that is created when we create a new Grails application contains the Gradle dependency management plugin via the Gradle Grails plugin. With the dependency management plugin we can import a Maven Bill Of Materials (BOM) file. And that is exactly what Grails does by importing a BOM with Grails dependencies. A lot of the versions of these dependencies can be overridden via Gradle project properties.

To get the list of version properties we write a simple Gradle task to print out the values:

Continue reading →

Mission to Mars follow up

Posted on by  
Jeroen Resoort

Last week I presented my talk 'MISSION TO MARS: EXPLORING NEW WORLDS WITH AWS IOT' at IoT Tech Day 2016 and it was great fun! In the presentation I showed how to build a small robot and control it over MQTT messaging via Amazons IoT platform. The room was packed and the demo went well too. I promised to share some info about it on my blog so here we are. I've composed a shopping list and a collection of useful links: Mission to Mars - Shopping list Mission to Mars - Useful links The original presentation is available here: Mission_to_Mars-Jeroen_Resoort-IoT_Tech_Day.pdf So what's next? I should publish my Pi robot and Mission Control Center web client code on github. Maybe I'll extend the python code for controlling the mBot over a serial connection and make a proper library for it.

Will keep you updated...

Continue reading →

Grasping AngularJS 1.5 directive bindings by learning from Angular 2

Posted on by  
Emil van Galen

In AngularJS 1.5 we can use attribute binding to allow easy use of input-only, output-only and two-way attributes for a directive or component. Instead of manually parsing, watching and modifying attribute values through code, we can simply specify an attribute binding by adding a property to the object hash of:

Continue reading →

Gradle Goodness: Source Sets As IntelliJ IDEA Modules

Posted on by  
Hubert Klein Ikkink

IntelliJ IDEA 2016.1 introduced better support for Gradle source sets. Each source set in our project becomes a module in the IntelliJ IDEA project. And each module has it's own dependencies, also between source sets. For example if we simply apply the java plugin in our project we already get two source sets: main and test. For compiling the test sources there is a dependency to the main source set. IntelliJ now knows how to handle this.

Let's create a sample Gradle build file with an extra custom source set and see what we get in IntelliJ IDEA. In the following example build file we add the source set api. This source set contains interfaces without implementations. The implementations for the interfaces are in the default main source set. Finally we have some tests in the test source set that depend on the classes generated by the api and main source sets.

Continue reading →

shadow-left