Java 15 > – Optional if present and AtomicReference – Variable outside Lambda expression HowTo

Sometimes I want to use the Optional’s feature with if/else plausibility check and discover that an outside variable would come in handy.
There are different ways of doing it but here is mine:

AtomicReference<Optional<Foo>> outsideVar = new AtomicReference<>(Optional.empty());
optional.ifPresentOrElse(filledOptional -> {
  LOGGER.info("Something happened: {}", filledOptional);
  outsideVar.set(Optional.of(new Foo(filledOptional)));
 }, () -> {
   LOGGER.error("Empty value!");
 }
);
        

Intellij „No tests were found“

Today my IntelliJ did not want to start otherwise correctly running JUnit Tests. Maven went well, but IntelliJ insisted that there was no test.

I add these dependencies and then, it worked:

<dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>1.6.0</version>
        </dependency>

How to install Open JDK on Mac OS

Use SDK Man: https://sdkman.io/install

$ curl -s "https://get.sdkman.io" | bash

Follow the instructions on the screen.

$ source "$HOME/.sdkman/bin/sdkman-init.sh"

Press ENTER.

$ sdk version
#should respond with sdkman 5.x ...

To install a JDK with it (this is OpenJDK 15):

$ sdk list java
$ sdk install java 15.0.1-open
...
$ sdk default java 15.0.1-open
$ sdk use java 15.0.1-open

You should use OpenJDK because by using Oracle you will have to pay a commercial fee, at least in commercial environments. This will eventually cause companies to abandon Java in worst case.

Webflux Reactive Webclient call with Flux Response and AtomicReference

/**
* This blocking call will return an AtomicReference which contains the response, the first which is received.
* You could also return via your Websocket, by directly passing on the FLux response!
*
* @param client - The Webclient, in this case Netty
* @param input - A String, arbitrary text message (I prefer JSON)
* @return AtomicReference, containing the response
*/
private AtomicReference<List<String>> callFooService(ReactorNettyWebSocketClient client, Flux<String> input) {
AtomicReference<List<String>> actualRef = new AtomicReference<>();

client.execute(getUrl(), session ->
session.send(input.map(session::textMessage))
.thenMany(session.receive().take(1).map(WebSocketMessage::getPayloadAsText))
.collectList()
.doOnNext(actualRef::set)
.then())
.block(TIMEOUT);
assertThat(actualRef.get()).isNotNull();
return actualRef;
}

WebEx Pair Programming Feature

In a Webex meeting both Keyboard and Mouse can be shared.

Anyone can directly control what is on the screen in realtime.
I just tried it with a current customer and it is a tremendous feature:

Top Bar -> Assign+ -> “Pass Keyboard and Mouse Control”

Java equals and hash code Contract

If two objects are equal, the hash code must be the same.

But the same hash code does not guarantee that two objects are equal.

Code example for a proper implementation:

 @Override
    public final boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MyClassOrInterface)) {
            //some do prefer #getClass but I don't
            return false;
        }
        MyClassOrInterface that = (MyClassOrInterface) o;
        return Objects.equals(this.hashCode(), that.hashCode()) &&
                Objects.equals(name, that.name) &&
                Objects.equals(sessionId, that.sessionId);
    }

    @Override
    public final int hashCode() {
        return Objects.hash(name, sessionId);
    }

Java – How to verify EQUALS and HASH code contracts

Use this:

<!-- Maven -->
<dependency>
    <groupId>nl.jqno.equalsverifier</groupId>
    <artifactId>equalsverifier</artifactId>
    <version>3.0.3</version>
    <scope>test</scope>
</dependency>

Write your test:

@Test
public void onEqualsTrueTheHashCodeHasToBeTheSame() {
    EqualsVerifier.forClass(Foo.class).verify();
}

NGINX configure Websocket in NginX – how to make it available

In the proxy.conf file, at the top, write:

upstream websocket {
        server name_of_my_docker_container:8080;
}

map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}

server {...

Why this? The Websocket has a constantly open connection with the client. The communication lies above the http protocol. This why we need to enable an „upgrade“ of the connection.

This example addresses a name of a Docker container. It would work with any other url too.

server {
      location / {...
      }
      ...	
      location name_of_my_docker_container {
		rewrite name_of_my_docker_container/(.*) /$1  break;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-NginX-Proxy true;
		proxy_ssl_session_reuse on;
		proxy_set_header Host $http_host;
		proxy_cache_bypass $http_upgrade;
		proxy_redirect off;
		proxy_set_header X-Forwarded-Proto "http";
		proxy_pass http://name_of_my_docker_container:8080;
		
                // These headers need to be set in order 
                // to enable the communication:
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";

		proxy_set_header Connection '';
		proxy_http_version 1.1;
		chunked_transfer_encoding off;
		proxy_buffering off;
		proxy_cache off;
	}