Skip to content

Interceptors in Play!

peterszatmary edited this page Nov 27, 2018 · 3 revisions

How to create interceptor in Play

Log Performance interceptor

This interceptor for example measure performance.

package core.pl;

import play.Logger;
import play.mvc.Action;
import play.mvc.Http;
import play.libs.F;
import play.mvc.*;

public class LogPerformanceInterceptor extends Action.Simple {

    @Override
    public F.Promise<Result> call(Http.Context context) throws Throwable {
        // finding out which method in which controller is performed
        final String METHOD = context.args.get("ROUTE_CONTROLLER") + "." + context.args.get("ROUTE_ACTION_METHOD");
        Logger.info("Method " + METHOD + " started");
        long start = System.currentTimeMillis();
        F.Promise<Result> result = delegate.call(context);

        long end = System.currentTimeMillis();
        Logger.info("Method " + METHOD + " end in " + (end - start) + " milliseconds.");
        return result;
    }
}

Calling interceptors

on class level = for all controller methods on method level = for one controller method

@With anotation can contain 1 .. N interceptors.

@With(LogPerformanceInterceptor.class)

Result

interceptors in Play