org.apache.ignite.services

## Interface ServiceCallInterceptor

All Superinterfaces:
  Serializable

All Known Implementing Classes:
  CompositeServiceCallInterceptor

    public interface ServiceCallInterceptor
    extends Serializable

Service call interceptor.

Allows the user to intercept the call to any service method except lifecycle methods (`init()`, `execute()` and `cancel()`).

A typical use of an interceptor is a middleware logic that applies to all custom methods in a service. The user can specify multiple interceptors in the `service configuration`. Each interceptor invokes the next interceptor in the chain using a delegated call, the last interceptor will call the service method.

Usage example:

     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.
             return 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(audit, security);

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

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

     // Make a service proxy with the call context to define the "sessionId" attribute.
     MyService proxy = ignite.services().serviceProxy("service", MyService.class, false, callCtx, 0);

     // Service method call will be intercepted.
     proxy.placeOrder(order1);
     proxy.placeOrder(order2);

See Also:
  `ServiceCallContext`, `ServiceContext`

### Method Summary

All Methods Instance Methods Abstract Methods
`Object` `invoke(String mtd, Object[] args, ServiceContext ctx, Callable<Object> next)` Intercepts delegated service call.

### Method Detail

#### invoke

    Object invoke(String mtd,
                  Object[] args,
                  ServiceContext ctx,
                  Callable<Object> next)
           throws Exception

Intercepts delegated service call.

Parameters:
  `mtd` - Method name.
  `args` - Method arguments.
  `ctx` - Service context.
  `next` - Delegated call to a service method and/or interceptor in the chain.
Returns:
  Service call result.
Throws:
  `Exception`

${current.year} Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.15.0
Release Date | : April 25 2023