Interesting links (week 3 2016)
Interesting links for week 3 2016:
Interesting links for week 3 2016:
The logging framework SLF4J supports Mapped Diagnostic Context (MDC). With MDC we can use a logging context that can be identified by something unique. This is useful, because then we can distinguish log messages from a big logging stream by something unique. Normally MDC is implemented on a per thread basis, but that is not useful in a Ratpack application. Ratpack provides the MDCInterceptor
class to use SLF4J's MDC support in a Ratpack application. We must register an instance of MDCInterceptor
with the registry. We can use the static method instance
to create a new instance. With the method withInit
we can define an action to be executed for the initialisation of the instance. An Execution
parameter is used with the action and we can use it to check for objects in the registry.
In the following example we initialise the MDCInterceptor
and check if there is a RequestId
object in the registry (as described in the Javadoc of MDCInterceptor
). If the RequestId
object is available we set the MDC logging context variable requestId
with the value of the RequestId
object. Later in our logging configuration we can use this value so we can distinguish all logging statements belonging to a single request (with the given identifier).
Ratpack has the ratpack.server.Service
interface with the methods onStart
and onStop
. If we write an implementation class for the Service
interface and register it with the Ratpack registry, then Ratpack will invoke the onStart
method when the application starts and the onStop
method when the application stops. The methods take an event object as argument and we can use the event object to access the registry if we need to. Writing an implementation for the Service
interface can be useful for example to bootstrap the application with initial data or do other things.
In the following example implementation we log when the application starts and stops. In the onStart
method we also display a Ratpack banner on the console.
In Ratpack we can use the byContent
method on the Context
object to send different responses based on the requested MIME type from the client. There is already support for application/json
, application/xml
, text/plain
and text/html
MIME types with corresponding methods of the ByContentSpec
object that is passed as argument to the byContent
method. We can match on a custom MIME type with the method type
and specify the MIME type. If the type matches we can create a response.
In the following example application we have a custom renderer for a User
object:
When we use the render
method in our Ratpack application then Ratpack will use the type of the object we want to render to find an appropriate renderer. Some renderers are built-in, like a Promise
or CharSequence
renderer. We can write our own renderers by implementing the ratpack.render.Renderer
interface. Next we must register our renderer in the Ratpack registry.
In our example application we have a very simple User
class:
Ratpack has the class ratpack.handling.ReponseTimer
which adds a header with the name X-Response-Time
to the response. The value is the time spent in code from when the request comes in and the response is sent out. ResponseTimer
is a handler we can add in our application. Alternatively we can use the static method decorator
to get a handler decorator. With a handler decorator we can use the registry to add handler logic in our application.
First we use the ResponseTimer
as a handler:
Russel Hart has a nice example on how to extend the Ratpack Groovy DSL in the hands on ratpack project on Github. We can use the Groovy extension module feature to add new methods to arbitrary classes. If we use this to add new methods to the GroovyChain
class we can use those methods in the DSL of our Groovy Ratpack application.
A Groovy extension module is just a class with a definition of the new methods we want to add to a class. The first argument of the method is the class the method is added to and the remaining arguments can be used by the implementation. We also need to create a supporting file org.codehaus.groovy.runtime.ExtensionModule
with some information about our extension class. This supporting file needs to be in the classpath in the directory META-INF/services
.
In a previous post we have seen how to use regular expressions for path tokens. We can also name the path token for which the regular expression applies. This makes it easier to get the value in our handler code: we can just refer to the name. Also we can add a question mark to the name to make the token optional.
In the following example we use the name conferenceName
for the path token with the regular expression Gr\w+
:
One of the strengths of Ratpack is the asynchronous execution model. An important class is the Promise
class. An instance of the class will represent a value that is available later. We can invoke several operations that need be applied to a value when a Promise
is activated. Usually the activation happens when we subscribe to a Promise
using the then
method. We can use the route
method for a Promise
to have a different action when a certain predicate is true. The action will stop the flow of operations, so methods that are executed after the route
method are not executed anymore if the predicate is true. If the predicate is false then those methods are invoked.
The Promise
class has a method onNull
as a shorthand for the route
method where the predicate checks if the value is null
. For example we could have a service in our application that returns a Promise
. If the value is null
we want some special behaviour like sending a 404
status code to the client. With the following code we could achieve this:
We can change the font size in our editor using shortcut keys in IntelliJ IDEA. But we can also use our mouse wheel to do this. We must enable this option in the settings of IntelliJ IDEA. We select the Preferences and then General | Editor. Here we select the option Change font size (Zoom) with Command+Mouse Wheel:
If we have a action method in our controller and we want to create a corresponding GSP we can press Alt+Enter when the cursor is on the action method. IDEA shows the intention actions and one of them is Create view (GSP page).
Normally in a Grails application we have classes that are related to each other, but are located in different directories. For example a controller with several views. Or a Grails service with corresponding specifications. In IntelliJ IDEA we can use Choose Target and IDEA will show classes, files and methods that are relevant for the current file we are editing. The keybinding on my Mac OSX is Ctrl+Cmd+Up, but can be different on your computer and operating system. We can also choose the menu option Navigate | Related symbol....
In the following example we are editing the file MessagesController
. We select the action Choose Target, IntelliJ IDEA shows a popup menu with the views for this controller and the specification class: