Automated tests for Spring Boot WebSocket server

Developing WebSocket server for your Spring Boot app is fairly simple and well described and documented. However when it comes to making sure that it 'actually works' is done manually in most cases. Below I will show how I do the automated integration tests for Websocket server using Spring's StompClient. I assume that you are … Continue reading Automated tests for Spring Boot WebSocket server

Spring: Securing REST API with BasicAuth

The simpliest, yet effective way to secure Spring REST API is to use Basic Auth. I'm going to show how to do it with Spring Security. Note Basic Auth is stateless (no need to manage sessions) and simple to implement. However it should be used with https only if outside of a trusted network. Also … Continue reading Spring: Securing REST API with BasicAuth

Spring JMX: Manage beans in runtime

JMX (Java Management Extensions) allows to change the bean field values or invoke bean methods on the fly at runtime. It is in opposite to DI where you configure application when it starts, using profiles or classpath. With JMX you can tune, monitor or configure your application anytime when it is running. At the heart … Continue reading Spring JMX: Manage beans in runtime

Java equals() vs. compareTo() contract

The definitions Object's equals(Object obj) method: Indicates whether some other object is "equal to" this one compareTo(T t) is an abstract method from Comparable interface and it: Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater … Continue reading Java equals() vs. compareTo() contract

Java 8 StringJoiner demo

Finally Java has convenient and intuitive API for joining strings with delimiters! Since Java 8 there is StringJoiner class. It is an API that you may know from Guava Joiner classes (see my post: https://looksok.wordpress.com/2015/10/17/guava-joiner-join-all-strings-in-an-array-or-map/). Here is a short StringJoiner demo. Basic String joins The most basic usage is to create StringJoiner instance with delimiter … Continue reading Java 8 StringJoiner demo

Asynchronous Producer Consumer with BlockingQueue in Java

What is it Producer Consumer pattern is used when one module (producer) produces events, messages or any other kind of data with various pace and the other module (consumer) processes it when the data occur. The asynchronous version means that the producer does not wait until consumer processes each item - just sends it to … Continue reading Asynchronous Producer Consumer with BlockingQueue in Java

Java 7 NIO.2 Paths to make path handling easy for you

Java 7 introduced a big change in file and directory structure manipulation API. Now you have: Path that represents locacation in a filesystem (filesystem can be Windows, *nix or a zip archive) Files to deal with directory trees (walk directory tree, iterate over files in directory, filter files, perform recursive move, copy, delete) WatchService to … Continue reading Java 7 NIO.2 Paths to make path handling easy for you

Java 7: Try-With-Resources block that closes the resources for you

Working with resources (like files stream, byte stream, network stream) in Java requires to close() the resource after you're done. How often did you forget to close it in your finally block? Even if you didn't - Java creators proved that 75% of close() implementations got bugs in it. Since Java 7 all resources you … Continue reading Java 7: Try-With-Resources block that closes the resources for you