Java / Jakarta Messaging Service (JMS) on ...Microsoft Azure--airhacks.fm podcast

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #111 airhacks.fm episode with Ashish Chhabria (@ashishc1) about:
algorithms, math in motion, passion with standards, JMS 2.0 on Microsoft's Azure Service Bus and JMS vs. Kafka
is available for

Java, Vaadin, Web and Vanilla Web Components--airhacks.fm podcast

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #110 airhacks.fm episode with Alejandro Duarte (@alejandro_du) about:
programming games and the "Apocalypse 2040", C, Java, Enterprise Java, Vaadin, Vaadin Web Components and vanilla Web Components
is available for

Joining Strings with Java 1.8+

The static String's method join, introduced in Java 1.8, joins a String array with a passed separator:


import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class StringJoinTest {
    @Test
    public void joinStrings() {
        var commaSeparated = String.join(",", "hello", "world");
        assertEquals("hello,world", commaSeparated);
    }
}    

Extracting Fragments from JSON Documents with JSON-P

With Jakarta JSON Processing API (JSON-P) available in all Jakarta EE and MicroProfile runtimes, you can parse a JSON object:


import static org.junit.jupiter.api.Assertions.assertEquals;

import static javax.json.Json.*;
import java.io.StringReader;
import javax.json.JsonObject;
import javax.json.JsonReader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class JSONPPointerTest {

    JsonObject json;

    @BeforeEach
    public void init() {
        var raw = this.getJSON();
        try(JsonReader reader = createReader(new StringReader(raw))){
            this.json = reader.readObject();
        }
    }

    String getJSON() {
        return """
                {
                    "base": {
                        "cpu.systemLoadAverage": 4.14208984375,
                        "thread.count": 45,
                        "gc.time;name=G1 Young Generation": 16
                        },
                    "references":["MP metrics","JSON-P","rfc6901","Jakarta EE","microprofile"]
                }
                """;
    }
    
    
...and use a JSON Pointer (rfc6901) to extract values from nested objects:
            
    @Test
    public void extractValueFromObject() {
        var pointer = createPointer("/base/thread.count");
        var fragment = pointer.getValue(this.json);
        assertEquals(createValue(45), fragment);
    }
    
...as well as, JSON arrays:

    @Test
    public void extractValueFromArray() {
        var pointer = createPointer("/references/1");
        var fragment = pointer.getValue(this.json);
        assertEquals(createValue("JSON-P"), fragment);
    }

}    
    
The JSON-P implementation already ships with all Jakarta EE 8 / Java EE 8 / MicroProfile runtimes. The single, test-scoped, dependency is only required to run the Unit Tests:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1.4</version>
    <scope>test</scope>
</dependency>
    

Also checkout: "Manipulating JsonObjects with JsonPatch"

Startup Hook / Initialization Logic with CDI

The Initialized qualifier is fired when a context is initialized, i.e. ready for use., and can be used to listen for the ApplicationScoped to be "ready".

Now you can use Initialized together with Observes to implement startup logic:


import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;

@ApplicationScoped
public class InitializerOnStart {

    public void onStart(@Observes @Initialized(ApplicationScoped.class) Object pointless) {
        System.out.println("InitializerOnStart.onStart() ");
    }
}

...the same functionality with EJBs:


import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class InitializerOnStart {

    @PostConstruct
    public void onStart() { }
}
(EJB example used in: A Java EE 7+ Alternative To EJB Timers)

Java, Agents, ODATA, Serverless and Cloud Events--airhacks.fm podcast

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #109 airhacks.fm episode with Klaus Deissner (@kdeissner) about:
Java agents, semantic web, OData, ABAP and Java, serverless and Cloud Events
is available for

Run Java Source as Shell Scripts

JEP-330: "Launch Single-File Source-Code Programs" allows direct execution of Java source files without compilation. Now, you can also use Java as an interpreter program (instead of e.g. bash or zsh).

To execute the main method you will have to declare a specific shebang first (#!/[PATH]/bin/java --source [VERSION]), then a java class with a main method:


#!/usr/bin/java --source 11
public class App{
    public static void main(String ...args){
        System.out.println("hello, java (like script)");
    }
}

The entire script can be now located, in an executable file with an arbitrary name like e.g.: hellojavascript.sh.

Executing: hellojavascript.sh generates the following output: hello, java (like script)

Java: The First Monday of the Month

The following snippets calculates the next first Monday of the month:


import java.time.DayOfWeek;
import java.time.LocalDate;
import static java.time.temporal.TemporalAdjusters.*;

public class FirstMondayOfTheMonth {

    public static void main(String[] args) {
        var todayInAMonth = LocalDate.now().plusMonths(1);
        var firstMondayOfTheMonth = todayInAMonth.
                with(firstDayOfMonth()).
                with(next(DayOfWeek.MONDAY));
        System.out.printf("See you at next airhacks.tv: %s 8pm CET (UTC +1)",firstMondayOfTheMonth);
    }
}

Because its Java, you don't even have to compile it (Java 11 / JEP-330).

Just run: java FirstMondayOfTheMonth.java and you will get: "See you at next airhacks.tv: 2020-11-02 8pm CET (UTC +1)"

WebSockets, JWT, Monoliths, Quarkus, Jakarta EE, MicroProfile, WebComponents, JDKs--79th airhacks.tv

The 79th airhacks.tv live questions and answers show is available with the following topics:

"WebSocket authentication, viper code review, Quarkus and monoliths, automated container builds, portable datasources, Jakarta EE / MicroProfile configuration, hot reloading configuration, Redux with local and session store, Web Components styling, Quarkus and CDI, and various JDKs"

Any questions left? Ask now: https://gist.github.com/AdamBien/c832d9c1226b84df5591d5bebc6e822b and get the answers at the next airhacks.tv.

JAX-RS Error Serialization, Viper, Quarkus and Monoliths, Container Builds, WebSockets and JWT--or 79th airhacks.tv

Topics, questions and topics (https://gist.github.com/AdamBien/50822397755deac85c9f5bd39b334b57) for the 79th airhacks.tv:

  1. Code review feedback: error serialization, headers vs. body, threal local (...)
  2. Viper review -- extending the MicroProfile config
  3. What about using Quarkus for monoliths?
  4. Is it suitable to use wad.sh for container builds?
  5. Portable installation of datasources
  6. Including build related information into an application
  7. How to convince your clients not to use a JS framework
  8. How to use bulma for all web components?
  9. WebSockets and JWT authentication

See you every first Monday of the month at https://airhacks.tv 8pm CET (UTC+1:00). Show is also announced at: meetup.com/airhacks.

Online Workshops
...the last 150 posts
...the last 10 comments
License