In a previous post we saw how can use the Spock configuration file to include or exclude specifications based on annotations. Instead of using annotations we can also use classes to include or exclude specifications. For example we could have a base specification class DatabaseSpecification
. Other specifications dealing with databases extend this class. To include all these specifications we use the DatabaseSpecification
as value for the include
property for the test runner configuration.
Because Java (and Groovy) doesn't support real multiple inheritance this might be a problem if we already have specifications that extends a base class, but the base class cannot be used as filter for the include
and exclude
runner configuration. Luckily we can also use an interface as the value for the inclusion or exclusion. So we could simple create a marker interface and implement this interface for these specifications we want to include or exclude from the test execution.
Continue reading →
One of the lesser known and documented features of Spock if the external Spock configuration file. In this file we can for example specify which specifications to include or exclude from a test run. We can specify a class name (for example a base specification class, like DatabaseSpec
) or an annotation. In this post we see how to use annotations to have some specifications run and others not.
The external Spock configuration file is actually a Groovy script file. We must specify a runner
method with a closure argument where we configure basically the test runner. To include specification classes or methods with a certain annotation applied to them we configure the include
property of the test runner. To exclude a class or method we use the exclude
property. Because the configuration file is a Groovy script we can use everything Groovy has to offer, like conditional statements, println
statements and more.
Continue reading →
Logback is a SLF4J API implementation for logging messages in Java and Groovy. We can configure Logback with a Groovy configuration file. The file is a Groovy script and allows for a nice an clean way (no XML) to configure Logback. If we want to show the logging configuration and see how Logback is configured we must add a StatusListener
implementation in our configuration. The StatusListener
implementation prints out the configuration when our application starts. Logback provides a StatusListener
for outputting the information to system out or system error streams (OnConsoleStatusListener
and OnErrorConsoleStatusListener
). If we want to disable any status messages we use the NopStatusListener
class.
In the following example configuration file we define the status listener with the statusListener
method. We use the OnConsoleStatusListener
in our example:
Continue reading →
When we use Logback as SLF4J API implementation in our code we can have our logging output send to our console. By default the standard output is used to display the logging output. We can alter the configuration and have the logging output for the console send to standard error. This can be useful when we use a framework that uses the standard output for communication and we still want to see logging from our application on the console. We set the property target
for the ConsoleAppender
to the value System.err
instead of the default System.out
. The following sample Logback configuration in Groovy send the logging output to the console and standard error:
appender("SystemErr", ConsoleAppender) {
// Enable coloured output.
withJansi = true
encoder(PatternLayoutEncoder) {
pattern = "%blue(%-5level) %green(%logger{35}) - %msg %n"
}
// Redirect output to the System.err.
target = 'System.err'
}
root(DEBUG, ["SystemErr"])
Continue reading →
Gradle introduced the continuous build feature in version 2.5. The feature is still incubating, but we can already use it in our daily development. The continuous build feature means Gradle will not shut down after a task is finished, but keeps running and looks for changes to files to re-run tasks automatically. It applies perfectly for a scenario where we want to re-run the test
task while we write our code. With the continuous build feature we start Gradle once with the test
task and Gradle will automatically recompile source files and run tests if a source file changes.
To use the continuous build feature we must use the command line option --continuous
or the shorter version -t
. With this option Gradle will start up in continuous mode. To stop Gradle we must use the Ctrl+D key combination.
Continue reading →
Saturday May 30th was the first NextBuild developer's conference in Eindhoven at the High Tech Campus. The conference is free for attendees and offers a variety of subjects presented by colleague developer's. This meant all talks were very practical and covered subjects encountered in real projects. This adds real value for me to attend a talk. Although it was on a weekend day there were about 150 attendees present. The location was very nice and allowed for a nice, informal atmosphere with a lot of opportunities to catch up.
The day started with a key note talk by Alex Sinner of Amazon. He looked into microservices and explained the features of AWS and especially the container support and the new AWS Lambda service. With the AWS Lambda service we can deploy functions that are executed on the Amazon infrastructure and we only pay when such a function needs to be executed. After the keynote the conference tracks were separated into five rooms, so sometimes it was difficult to choose a track. I went to the talk by my JDriven colleague Rob Brinkman about a Westy tracking platform he built with Vert.x, Groovy, AngularJS, Redis, Docker and Gradle. For those that don't know, but a Westy is a Volkswagen van used for camping trips. Rob has build a platform where a (cheap) tracker unit communicates to a Vert.x module the location of the Westy. This is all combined with other trip details and information in a web application. Every works with push events and the information is updated in real time in the web application. The talks was very interesting and really shows also the power and elegance of Vert.x. Also the architecture provided is a like a blueprint for Internet of Things (IoT) applications.
Continue reading →
To work with data in a concurrent environment can be complex. Groovy includes GPars, yes we don't have to download any dependencies, to provide some models to work easily with data in a concurrent environment. In this blog post we are going to look at an example where we use dataflow variables to exchange data between concurrent tasks. In a dataflow algorithm we define certain functions or tasks that have an input and output. A task is started when the input is available for the task. So instead of defining an imperative sequence of tasks that need to be executed, we define a series of tasks that will start executing when their input is available. And the nice thing is that each of these tasks are independent and can run in parallel if needed.
The data that is shared between tasks is stored in dataflow variables. The value of a dataflow variable can only be set once, but it can be read multiple times. When a task wants to read the value, but it is not yet available, the task will wait for the value in a non-blocking way.
In the following example Groovy script we use the Dataflows
class. This class provides an easy way to set multiple dataflow variables and get their values. In the script we want to get the temperature in a city in both Celcius and Fahrenheit and we are using remote web services to the data:
Continue reading →
Grails has a data binding mechanism that will convert request parameters to properties of an object of different types. We can customize the default data binding in different ways. One of them is using the @DataBinding
annotation. We use a closure as argument for the annotation in which we must return the converted value. We get two arguments, the first is the object the data binding is applied to and the second is the source with all original values of type SimpleMapDataBindingSource
. The source could for example be a map like structure or the parameters of a request object.
In the next example code we have a Product
class with a ProductId
class. We write a custom data binding to convert the String
value with the pattern {code}-{identifier}
to a ProductId
object:
Continue reading →
With Grails 3 we also get Spring Boot Actuator. We can use Spring Boot Actuator to add some production-ready features for monitoring and managing our Grails application. One of the features is the addition of some endpoints with information about our application. By default we already have a /health
endpoint when we start a Grails (3+) application. It gives back a JSON response with status UP. Let's expand this endpoint and add a disk space, database and url health check indicator.
We can set the application property endpoints.health.sensitive
to false
(securing these endpoints will be another blog post) and we automatically get a disk space health indicator. The default threshold is set to 10MB, so when the disk space is lower than 10MB the status is set to DOWN. The following snippet shows the change in the grails-app/conf/application.yml
to set the property:
Continue reading →
We can let Grails log some extra information when the application starts. Like the process ID (PID) of the application and on which machine the application starts. And the time needed to start the application. The GrailsApp
class has a property logStartupInfo
which is true
by default. If the property is true than some extra lines are logged at INFO and DEBUG level of the logger of our Application
class.
So in order to see this information we must configure our logging in the logback.groovy
file. Suppose our Application
class is in the package mrhaki.grails.sample.Application
then we add the following line to see the output of the startup logging on the console:
Continue reading →
Since Grails 3 we can borrow a lot of the Spring Boot features in our applications. If we look in our Application.groovy
file that is created when we create a new Grails application we see the class GrailsApp
. This class extends SpringApplication
so we can use all the methods and properties of SpringApplication
in our Grails application. Spring Boot and Grails comes with the class ApplicationPidFileWriter
in the package org.springframework.boot.actuate.system
. This class saves the application PID (Process ID) in a file application.pid
when the application starts.
In the following example Application.groovy
we create an instance of ApplicationPidFileWriter
and register it with the GrailsApp
:
Continue reading →
When we want to explain in our documentation which keys a user must press to get to a function we can use the keyboard macro in Asciidoctor. The macro will output the key nicely formatted as a real key on the keyboard. The syntax of the macro is kbd:[key]
. To get the desired output we must set the document attribute experimental
otherwise the macro is not used.
In the next Asciidoctor example file we use the keyboard macro:
Continue reading →