Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
themeRDark
titleExample.java
collapsetrue
    ServiceCallInterceptor security = (mtd, args, ctx, svcCall) -> {
        if (!CustomSecurityProvider.get().access(mtd, ctx.currentCallContext().attribute("sessionId")))
            throw new SecurityException("Method invocation is not permitted");

        // Execute remaining interceptors and service method.
        return svcCall.call();
    };

    ServiceCallInterceptor audit = (mtd, args, ctx, svcCall) -> {
        String sessionId = ctx.currentCallContext().attribute("sessionId");
        AuditProvider prov = AuditProvider.get();

        // Record an event before execution of the method.
        prov.recordStartEvent(ctx.name(), mtd, sessionId);

        try {
            // Execute service method.
            svcCall.call();
        }
        catch (Exception e) {
            // Record error.
            prov.recordError(ctx.name(), mtd, sessionId), e.getMessage());

            // Re-throw exception to initiator.
            throw e;
        }
        finally {
            // Record finish event after execution of the service method.
            prov.recordFinishEvent(ctx.name(), mtd, sessionId);
        }
    }

    ServiceConfiguration svcCfg = new ServiceConfiguration()
        .setName("service")
        .setService(new MyServiceImpl())
        .setMaxPerNodeCount(1)
        .setInterceptors(security, audit);

    // Deploy service.
    ignite.services().deploy(svcCfg);

    // Set context parameters for the service proxy.
    ServiceCallContext callCtx = ServiceCallContext.builder().put("sessionId", sessionId).build();

    // Make service proxy.
    MyService proxy = ignite.services().serviceProxy("service", MyService.class, false, callCtx, 0);

    // A Makebusiness servicemethod call to be intercepted.
    proxy.placeOrder(order1);
    proxy.placeOrder(order2);

...

Interceptor is located and executed where the service is implemented (for Java service - on Java side, for .NET-service on .NET side). Its execution should not cause additional serialization).

Invocation order

...

The user can specify multiple interceptors.Each interceptor invokes the next interceptor in the chain using a delegated call, the last interceptor will call the service method.

So the interceptor specified first in the configuration will process the result of the service method execution last.

draw.io Diagram
bordertrue
diagramNameinvocation
simpleViewerfalse
width
linksauto
tbstyletop
lboxtrue
diagramWidth471
revision3

Resource injection

Interceptor must support ignite instance resource injection.(question) Interceptor should support LifeCycleAwarethe injection of generic resources.

Interception scope

Interceptor only applies to user-defined service methods and does not apply to service lifecycle methods - init, execute andcancel,

...

If one service calls another, then by default the current call context will not be bound to the created proxy - the user must explicitly bind it. But Java service has a special ServiceResource annotation to inject another service proxy into the current service. If the user wants to redirect the current call context to this (injected) proxy, he can set the forwardCallerContext option of this annotation.

Exception handling

Interceptor can only throw unchecked exceptions.

Any runtime exception Exception thrown by the onInvoke/onComplete methods interceptor will be wrapped in a ServiceInterceptException.This exception will be into unchecked IgniteException and passed to the initiator (user) and to onError method of the interceptor.

If onInvoke throws an exception, then the service method will not be called.

The exception thrown by the onError method will be added to the main exception as suppressed.

...

.

Risks and Assumptions

The interceptor gives the user full control over the invocation of the service methods, so in case of implementation errors, the user may get unexpected behavior of the service.

Discussion Links

https://lists.apache.org/thread.html/r4236c1f23e524dc969bc55057467a2bbe7f9a59a6db7c7fcdc1b7d37%40%3Cdev.ignite.apache.org%3E

...