Convenient http filtering with Servlet 4.0 and Java EE 8

HttpFilter was introduced with JSR 369 / Servlet 4.0 and Java EE 8.

The HttpServletRequest and HttpServletResponse parameters are directly passed to the HelloFilter#doFilter method:


import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class HelloFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, 
    FilterChain chain) throws IOException, ServletException {
        chain.doFilter(req, res);
        res.getWriter().print(" is great");
    }
}

The servlet does not know anything about the HelloFilter:


@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().print("Java EE 8");
    }

}

curl (...)/HelloServlet returns Java EE 8 is great.

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Fetching content reactively with JAX-RS 2.1 client and Java EE 8

Java EE 8 with JAX-RS 2.1 / JSR-370 introduced a JAX-RS Client API integrated with CompletionStage (aka reactive client).

Now an asynchronous request may return a Java 8 CompletionStage which allows a pipeline-style programming:


import java.util.concurrent.CompletionStage;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.junit.Before;
import org.junit.Test;


public class ReactiveClientTest {

    private WebTarget tut;

    @Before
    public void initClient() {
        this.tut = ClientBuilder.
        newClient().target("http://airhacks.com");
    }


    @Test
    public void reactive() throws InterruptedException {
        CompletionStage<Response> stage = this.tut.
                request().
                rx().
                get();
        stage.
                thenApply(req -> req.readEntity(String.class)).
                thenAccept(System.out::println);
        Thread.sleep(500);
    }

}

The JAX-RS 2.1 client requires the following dependencies:


<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>        
</dependency>

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Code review, reports, NoSQL, BCE, Async and State -- or 44th airhacks.tv

WebStandards, microservices, reports, dealing with state in asynchronous JAX-RS, interactive code reviews, NoSQL,BCE, or 44th airhacks.tv is available:

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

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Reuse, Tech Impact, Microservices, WebStandards -- a JavaONE 2017 interview

During JavaONE 2017 I was interviewed in the Developer Lounge about reuse, technology impact, microservices, my sessions and Java EE:

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Unskippable Maven Tests

The following maven plugin configuration introduces "unskippable" unit tests:

    
<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
            <skipTests>false</skipTests>
        </configuration>
    </plugin>
</plugins>
</build>


Even with the mvn clean install -DskipTests system property, all unit tests are still going to be executed.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Using Chrome Developer Tools as an IDE / Editor

The Chrome's Developer Tools can be not only used to inspect the DOM, CSS and JavaScript, but also as an IDE / editor with write access to local project files:

See you at Single Page Applications (SPAs) -- the "no frameworks, no migrations" approach, at Munich Airport, Terminal 2 or webstandards.training (online).


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

HTML 5 Progressbars, Code Reviews, JAXB deprecation, Virtual DOM speed, Java SE Release and EE4j, EJB Timers in Cluster or 44th airhacks.tv

Topics for the 44th airhacks.tv (http://www.ustream.tv/channel/adambien) show 6th November, 2017 CET:

  1. JavaONE talk: Java EE heavyweight or lightweight Mythbusters and the comparison to Spring
  2. How to build a progressbar with HTML 5
  3. JAXB deprecation in Java9
  4. Virtual DOM, vs createElement, vs (...) performance
  5. Reference Implementations of EE4J
  6. Issue with org.eclipse.ee vs. javax.*
  7. Impact of Java SE release strategy to EE4j or JavaEE
  8. EJB Timer synchronization in cluster
  9. DB Views vs. JPA for reports?
  10. Architectural considerations with REST and Redis
  11. Critique of the "Roller blogger engine" Java EE clone
  12. Accessing SFSB from SLSB
  13. SFSB and scalability
  14. State and async JAX-RS

Ask questions during the show via twitter mentioning me: http://twitter.com/AdamBien (@AdamBien) or using the hashtag: #airhacks. You can join the Q&A session live each first Monday of month, 6 P.M at airhacks.tv or http://www.ustream.tv/channel/adambien

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

A Conversation with Sebastian Daschner about CQRS, Java EE, EE4J, JavaONE

A conversation with Sebastian Daschner about Java EE, EE4J, JavaONE 2017, Microprofile, CQRS, JAX-RS analyzer and his: "Architecting Modern Java EE Applications book (amazon link)"

Subscribe to airhacks.fm podcast via: RSS iTunes or listen now.

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

System.Logger--the minimalistic logging interface in Java 9

Java 9 comes with a minimal logging interface: System.Logger with default implementation returned by System#getLogger:


@Test
public void log() {
    System.Logger LOG = System.getLogger(LoggerTest.class.getName());
    System.out.println("Default System.Logger implementation: " + LOG.getClass().getName());
    LOG.log(System.Logger.Level.INFO, "hello java 9 logging");
}

output:

    Default System.Logger implementation: sun.util.logging.internal.LoggingProviderImpl$JULWrapper
    Nov 01, 2017 5:56:46 AM com.airhacks.logging.LoggerTest log
    INFO: hello java 9 logging

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Java EE 8: Sending asynchronous CDI 2.0 events with ManagedExecutorService

Java EE 8 with CDI 2.0 introduced asynchronous CDI Events. The Event interface was extended with the fireAsync method and an optional NotificationOptions parameter.

Now you can send events asynchronously, also using the injected ManagedExecutorService instance:


import java.time.LocalTime;
import java.util.concurrent.CompletionStage;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.event.Event;
import javax.enterprise.event.NotificationOptions;
import javax.inject.Inject;

@Startup
@Singleton
public class EventSource {

    @Inject
    Event<String> fireAlarm;
    
    @Resource
    ManagedExecutorService threadPool;

    @Schedule(second = "*/5", minute = "*", hour = "*")
    public void send() {
        String event = "fire " + System.currentTimeMillis();
        CompletionStage<String> completion = this.fireAlarm.
        fireAsync(event, NotificationOptions.ofExecutor(threadPool)); // returns immediately
        completion.thenAccept(this::eventDelivered);
    }

    void eventDelivered(String event) {
        //...receipt after delivery
    }
}
Asynchronous events are received with the @ObservesAsync annotation:


import java.time.LocalTime;
import javax.enterprise.event.ObservesAsync;

public class EventSink {

    public void onFireNotification(@ObservesAsync String event) {
       //...
    }

}

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

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