- Even less readable!
- No more syntax checks with proper schemas!
- Go crazy fixing stupid indentation mistakes!
- ...
Friday, June 12, 2020
YAML vs XML
Thursday, January 30, 2020
The Modular Monolith
- Part 1: Modular Monolith: A Primer
- Part 2: Modular Monolith: Architectural Drivers
Starting with a well designed and internally modularised monolith does seem to be the way to go. When architectural drivers change, like different modules evolving at different rates, it comes natural to split up the monolith into a few modules that are life-cycled and deployed independently, essentially creating micro-services. Scalability concerns in a module is another example of an architectural driver that often leads to such a split up.
You've got several tools at your disposal to keep your monolith properly modularised.
- Designing proper module interfaces used by other parts of the system to interact with the module.
- Use of Java packages to encapsulate modules, while keeping an eye on cyclomatic complexity.
- The Java 9 module system. (Although I haven't used this myself yet.)
- Smart use of multi-module builds in Maven. If you get this right, extracting a module to become its own micro-service might be a relatively trivial matter.
Friday, August 2, 2019
Adding a JDBC driver to Spring Cloud Data Flow server
The Spring Cloud Data Flow documentation suggests rebuilding (!?!) when you need to add a custom JDBC driver. That seems a bit crazy to me so after a bit of Googling it turns out you can do it using the Spring Boot PropertiesLauncher:
java -cp spring-cloud-dataflow-server-2.2.0.RELEASE.jar -Dloader.path=lib \ org.springframework.boot.loader.PropertiesLauncher \ --spring.datasource.url=jdbc:mysql://localhost:3306/y9a?useSSL=false \ --spring.datasource.username=root \ --spring.datasource.password=password \ --spring.datasource.driverClassName=com.mysql.jdbc.DriverAnd you simply have to put the JDBC driver jar file in the lib/ directory:
spring-cloud-dataflow-server-2.2.0.RELEASE.jar lib/ mysql-connector-java-8.0.17.jarDisco!
Tuesday, January 8, 2019
Building a WebSocket application with Spring Boot
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ervacon.sb</groupId> <artifactId>websocket</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <!-- Add typical dependencies for a websocket application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Notice how we pull in the spring-boot-starter-websocket dependency. This will make sure the Java API for WebSocket (javax.websocket) is on the classpath, together with Spring's support for WebSocket. When Spring Boot starts it will detect these dependencies and auto-configure the embedded Servlet engine (Tomcat by default) to handle the WebSocket protocol. Next, we define our @SpringBootApplication class:
package com.ervacon.sb.websocket; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }The only special thing here is the fact that we register the ServerEndpointExporter, which will make sure our @ServerEndpoint annotated classes get registered with the WebSocket runtime. We can now define our server side WebSocket endpoint:
package com.ervacon.sb.websocket; import org.springframework.stereotype.Component; import javax.websocket.OnMessage; import javax.websocket.server.ServerEndpoint; @Component @ServerEndpoint("/socket") public class WebSocketEndpoint { @OnMessage public String echo(String message) { return "Echo: " + message; } }Nothing to surprising here: we're just using the Java API for WebSocket (JSR-356) to define a method that will handle incoming text messages by simply echoing them back to the client. That client is implemented using some basic JavaScript code in a simple HTML page (add this as index.html to src/main/resources/static in your Spring Boot application):
<html> <head> <title>WebSocket Client</title> <!-- based on https://www.nexmo.com/blog/2018/10/08/create-websocket-server-spring-boot-dr/ --> </head> <body> <div class="container"> <div id="messages" style="border: 1px solid gray; padding: 1em;"></div> <div class="input-fields"> <p>Type your message:</p> <input id="message"/> <button id="send">Send</button> </div> </div> </body> <script> const messageWindow = document.getElementById("messages"); const sendButton = document.getElementById("send"); const messageInput = document.getElementById("message"); const socket = new WebSocket("ws://localhost:8080/socket"); socket.onopen = function (event) { addMessageToWindow("Connected"); }; socket.onmessage = function (event) { addMessageToWindow(`Got Message: ${event.data}`); }; sendButton.onclick = function (event) { sendMessage(messageInput.value); messageInput.value = ""; }; function sendMessage(message) { socket.send(message); addMessageToWindow("Sent Message: " + message); } function addMessageToWindow(message) { messageWindow.innerHTML += `<div>${message}</div>` } </script> </html>We can now launch our Spring Boot application using a simple mvn spring-boot:run and test it by accessing http://localhost:8080/.
Monday, September 17, 2018
Software maturity
A new metaphor Evans introduced was comparing a large software system to a community garden. Looking past the obvious bounded contexts of people sharing space in the garden, he sees positive analogies to legacy systems, looking at the "abundance of maturity." Gardens are most valuable in late summer, when they are most productive. However, that is long past the stage when you can easily make changes to the garden, in early spring. Similarly, the most malleable phase for software is not when it is the most productive.This puts an interesting perspective on something I've noticed in my own career. Just like all developers, I love getting started on a new greenfield project: things are new and exciting and together with the team you're constantly inventing new ways of tackling problems and (re)factoring the system accordingly. Some time after the first go-live, which typically comes with a host of teething problems, the application stabilizes and slowly evolves into a mature system over the next several releases. You can no longer reinvent the wheel at this time: you're building on the foundations you established earlier.
I've found that I also get a great deal of satisfaction from this phase: shepherding a system to maturity, seeing it come to fruition and hopefully realizing it's full potential! It as this time users are most happy with what has been delivered: the software is well understood and quality is still high, making changes predictable and quick. Over time quality slowly deteriorates and accidental complexity grows. Changes become more difficult and risky, with more regressions slipping in. Ultimately the system is replaced with something new and better, bringing everything full circle, just like in a garden.
Wednesday, August 29, 2018
Consistent Error Handling in a Spring Boot REST Service
- Option 1 - Servlet API Error Handling - The Servlet API itself contains an error handling system configured via web.xml. You can use the <error-page> element to specify the response handler (a JSP, Servlet, ...) for certain exceptions and HTTP status codes. In practice this means that if a Servlet or JSP generates an exception or calls HttpServletResponse.sendError(), the corresponding <error-page> page will be sent back to the client.
- Option 2 - Spring MVC Error Handling - Spring MVC's DispatcherServlet comes with it's own error handling system, implemented using a HandlerExceptionResolver. In modern Spring applications this is typically setup using @ExceptionHandler @ControllerAdvice. When a request handler throws and exception, an appropriate exception handler is selected and tasked with resolving the error by generating an error response. Note that if an exception handler resolves the error, the exception never propagates up to the Servlet engine, meaning the defined Servlet error pages are not considered.
-
Option 3 - Spring Boot Error Handling - Spring Boot comes with a default error handling setup built on top of the Servlet API and Spring MVC error handling systems.
The default setup (see ErrorMvcAutoConfiguration) does three things:
- It configures a Servlet <error-page> directed at /error for all exceptions and response codes.
- It defines an ErrorController handling /error requests, forwarded to it because of the configured Servlet error page. The default ErrorController implementation is the BasicErrorController, which will try to resolve an "error" view used to render a model prepared by an ErrorAttributes implementation. If the client is requesting text/html, the default whitelabel error page (see below) will be used, otherwise the model will be rendered directly as response body (typically JSON).
- The whitelabel error page serves as a simplistic HTML "error" view detailing the error that occurred by rendering the model prepared by the ErrorAttributes implementation.
- Define a class for your own error structure, say ErrorInfo.
- Implement approprate @ExceptionHandlers returning ResponseEntity<ErrorInfo> objects, typically in a ResponseEntityExceptionHandler subclass.
- Implement an ErrorAttributes bean to return a Map (ugh!) corresponding with your ErrorInfo structure.
- Replace the whitelabel error page with one that can handle your ErrorInfo structure.
- Of course you still need to define your own ErrorInfo structure.
- And you'll also still need to implement appropriate @ExceptionHandlers returning ResponseEntity<ErrorInfo> objects.
- Now implement an ErrorController that handles /error requests, returning an appropriate ResponseEntity<ErrorInfo>.
- Finally, disable the whitelabel error page by setting the server.error.whitelabel.enabled property to false.