Asynchronous event publishing with Spring Boot

Ladies & gents, I’ve created sample Spring Boot app that demonstrates useful scenario of asynchronous publish / subscribe model. It could be useful in many ways. Idea is that, for example, you have some API handling some web requests, and apart from functionality it provides, it also publishes some event, so that some other thread can react upon it, with main functionality still being processed with request handling thread. Or even shorter – request handling thread will do it’s job, and publishes some event (using Java annotation), and some other background thread will subscribe to event and process it. You can use it for scenario you register user with your API, and publish user registered event, and in background you can subscribe to it by sending out e-mail to end user.

You can checkout source code from GitHub

Easiest way to start your application could be running it via spring-boot:run Maven goal:

mvn spring-boot:run

That will start our demo app on standard 8080 port.

Demo app contains two APIs, get user and update user. Those are just dummy implementations. It also has User domain model class.

You can then, using command line and curl tool, issue the get user request (I’m just doing pipe to jq tool  for nicer response rendering – you can omit that part):

curl -s 'http://localhost:8080/users/1' | jq .
{
 "id": 1,
 "age": 33,
 "name": "Some name"
}
2017-04-23 11:46:59.940 INFO 7421 --- [nio-8080-exec-1] de.odalinho.async.PublishingController : PublishingController::Thread.currentThread().getName() = http-nio-8080-exec-1
2017-04-23 11:46:59.941 INFO 7421 --- [pool-1-thread-3] d.o.a.m.UserNotificationPublisher : UserNotificationPublisher::Thread.currentThread().getName() = pool-1-thread-3
2017-04-23 11:46:59.941 INFO 7421 --- [pool-1-thread-3] d.o.a.m.UserNotificationPublisher : UserNotificationPublisher::event.getMessage() = User(id=1, age=33, name=Some name)

Continue reading

Using regular expressions for querying MongoDB documents

Ladies and gents, here’s one fairly short hint for anyone wondering how to query documents in mongodb using regular expressions. Let’s get straight to the point:

Let’s start by inserting couple of documents (using mongodb shell) which we’ll use for querying afterwards:

db.developers.insertMany([
    { "name" : "John", "languages" : ["java", "php", "javascript"] },
    { "name" : "Johnny", "languages" : ["java", "c", "c++"] },
    { "name" : "Jim", "languages" : ["node", "java"] }
]);

Continue reading

Querying Mongo documents for array properties

Ladies and gents, I’m just posting one short reminder to myself and anyone keen to find out how do we query array type fields in mongodb.

Let’s start by inserting couple of documents (using mongodb shell) which we’ll use for querying afterwards:

db.developers.insertMany([
    { "name" : "John", "languages" : ["java", "php", "javascript"] },
    { "name" : "Jack", "languages" : ["java", "c", "c++"] },
    { "name" : "Jim", "languages" : ["node", "java"] }
]);

Continue reading

Asserting your method get valid arguments can’t be easier than using …

Just wanted to help myself and anyone interested in topic simplify asserting my methods receive valid arguments before ever executing main logic.
One easy way to do so is using Java’s assert keyword.

So, say you have method:

     private static int populateMatrixSortedAscending() {
        int counter = 0;
        final int totalRows = matrix.length;
        for (int i = 0; i < totalRows; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                int newEntry = newEntry();
                while (!isValidNewEntry(newEntry, i, j)) {
                    newEntry = newEntry();
                }
                matrix[i][j] = newEntry;
            }
        }
        return counter;
    }

Continue reading

Interesting way to assert thrown exceptions in Spring Boot and AssertJ

You’re probably used to testing code that throws exceptions in either one of the following ways:

Using expected attribute of JUnit @Test  annotation, such as:

 @Test(expected = UserService.NoSuchUserException.class)
    public void deletesExistingUser() {
        final User existingUser = spy(new User());
        given(userRepository.findOne(eq("123"))).willReturn(existingUser);
        given(userRepository.save(eq(existingUser))).willReturn(existingUser);

        userService.deleteUser("123");

        verify(existingUser).setState(eq(User.State.DELETED));
    }

Continue reading

Make your first days as developer easier

I was thinking lately I should share some thoughts with my younger colleagues making some first professional steps as software engineers. I’ll go straight ahead without any further ado.

Any first job is – good job

Instead of picking just the right job to start with, based on what I experienced – a lot better is to just take the first opportunity that comes across. Nothing adds on top of your qualities as working experience.

Get the job offered and enjoy raising you quality. Continue reading

Https in a nutshell

One of the topics I demystified recently was question of how HTTPS works in general. What happens when you type in your browser address bar URL starting with HTTPS?

HTTPS uses SSL (Secret Socket Layer) encryption for HTTP protocol, which gives HTTPS name.

As of the moment your browser issues HTTPS request to remote server, SSL handshake process triggers. Idea is that browser will request that from then on all the data send to the server and received back from is encrypted, so that no one can tamper the data during client / server communication.

During SSL handshake process client will exchange couple of messages with the server, like: Continue reading