Versions Compared

Key

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

...

Code Block
languagejava
themeConfluence
titleServiceInterceptorContext
linenumberstrue
public interface ServiceInterceptorContext extends ServiceCallContext {
    public String method();

    public @Nullable Object[] arguments();

    public void attribute(String name, String val);

    public void binaryAttribute(String name, byte[] val);
}

Requirements/limitations

  • The service interceptor binds to the service itself. It is executed where the service is implemented (for Java service - on Java side, for .NET-service on .NET side without any additional serialization).
  • Any interceptor can change the ServiceCallContext.
  • If an interceptor has been specified, but the user has not passed the caller context through the proxy, it will be created dynamically.
  • ServiceCallContext must be accessible inside the service.
  • Interceptor must support ignite instance resource injection.
  • (question)Interceptor should be LifeCycleAware?
  • (question) If an interceptor throws an exception, then processing is aborted, but the exception is passed to all listed interceptors (onError)?

Usage example

Diagram

draw.io Diagram
bordertrue
diagramNamemiddleware
simpleViewerfalse
width
linksauto
tbstyletop
lboxtrue
diagramWidth1001
revision13

...

Code Block
languagejava
themeConfluence
linenumberstrue
        ServiceCallInterceptor security = new ServiceCallInterceptor() {
            @Override public void onInvoke(ServiceInterceptorContext ctx) throws ServiceInterceptException {
                // Check permission before execution of the method.
                if (!CustomSecurityProvider.get().access(ctx.method(), ctx.attribute("sessionId")))
                    throw new SecurityException("Method invocation is not permitted");
            }
        }

        ServiceCallInterceptor audit = new ServiceCallInterceptor() {
            @Override public void onInvoke(ServiceInterceptorContext ctx) {
                // Record an event before execution of the method.
                AuditProvider.get().recordStartEvent(ctx.method(), ctx.attribute("sessionId"));
            }

            @Override public void onComplete(@Nullable Object res, ServiceInterceptorContext ctx) {
                AuditProvider.get().recordFinishEvent(ctx.method(), ctx.attribute("sessionId"));
            }

            @Override public void onError(Throwable err, ServiceInterceptorContext ctx) {
                AuditProvider.get().recordError(ctx.method(), ctx.attribute("sessionId"), err.getMessage());
            }
        }

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

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

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

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

        // A business method call to be intercepted.
        proxy.placeOrder(order1);
        proxy.placeOrder(order2);  

Implementation notes

The service interceptor is bound to the service during deployment.

One service can have several interceptors.

Interceptor is executed where the service is implemented (for Java service - on Java side, for .NET-service on .NET side without any additional serialization).

Interceptor only applies to user-defined business methods - it does not apply to initialize/execute/cancel methods,

Interceptor can modify ServiceCallContext, but Service methods can only read it.

If an interceptor has been specified, but the user has not passed the caller context through the proxy, it will be created dynamically.

Resource injection and lifecycle

Interceptor must support ignite instance resource injection.

(question) Interceptor should be LifeCycleAware

Exception handling

// todo  ServiceInterceptException

(question) If an interceptor throws an exception, then processing is aborted, but the exception is passed to all listed interceptors (onError)

Passing Context in the Service Chain

// todo explain forwardCallerContext


Limitations


Risks and Assumptions


Discussion Links

...