public interface ServiceCallInterceptor extends Serializable
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);
ServiceCallContext,
ServiceContext| Modifier and Type | Method and Description |
|---|---|
Object |
invoke(String mtd,
Object[] args,
ServiceContext ctx,
Callable<Object> next)
Intercepts delegated service call.
|
Object invoke(String mtd, Object[] args, ServiceContext ctx, Callable<Object> next) throws Exception
mtd - Method name.args - Method arguments.ctx - Service context.next - Delegated call to a service method and/or interceptor in the chain.Exception
Follow @ApacheIgnite
Ignite Database and Caching Platform : ver. 2.16.0 Release Date : December 15 2023