Versions Compared

Key

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

...

  • ServiceCallContext -  immutable user parameter map that will be implicitly passed to the service (and interceptor) on every method call.

    Code Block
    languagejava
    themeRDark
    titleServiceCallContext.java
    linenumberstrue
    collapsetrue
    public interface ServiceCallContext {
        public String attribute(String name);
    
        public byte[] binaryAttribute(String name);
    }


  • ServiceCallInterceptor  - intercepts service method calls.

    Code Block
    languagejava
    themeRDark
    titleServiceCallInterceptor.java
    linenumberstrue
    collapsetrue
    public interface ServiceCallInterceptor extends Serializable {
        // Called BEFORE the service method is executed.
        public default void onInvoke(ServiceInterceptorContext ctxString mtd, Object[] args, @Nullable ServiceCallContext callCtx) throws ServiceInterceptException {
            // No-op.
        }
    
          // Called AFTER the service method is executed.
        public default void onComplete(String mtd, @Nullable Object res, ServiceInterceptorContext@Nullable ServiceCallContext ctxcallCtx) throws ServiceInterceptException {
            // No-op.
        }
    
          // Called when onInvoke, onComplete or service method throws an exception.
        public default void onError(String mtd, Throwable err, @Nullable ServiceInterceptorContextServiceCallContext ctxcallCtx) {
            // No-op.
        }
    }

    ServiceInterceptorContext -  extended mutable version of caller context (interceptor obtains method call parameters from it and can use it to update the caller context).

    Code Block
    languagejava
    themeRDark
    titleServiceInterceptorContext.java
    linenumberstrue
    collapsetrue
    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);
    }


  • ServiceInterceptException - unchecked exception that is used to highlight the exception that occurred during method interception (not execution).

...

Code Block
languagejava
themeRDark
titleExample.java
linenumberstrue
collapsetrue
        ServiceCallInterceptor security = new ServiceCallInterceptor() {
            @Override public void onInvoke(ServiceInterceptorContextString mtd, Object[] args, ServiceCallContext ctx) throws ServiceInterceptException {
                // Check permission before execution of the method.
                if (!CustomSecurityProvider.get().access(ctx.method()mtd, ctx.attribute("sessionId")))
                    throw new SecurityException("Method invocation is not permitted");
            }
        }

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

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

            @Override public void onError(String mtd, Throwable err, ServiceInterceptorContextServiceCallContext ctx) {
                AuditProvider.get().recordError(ctx.method()mtd, 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(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);  

...

Service method can read current context parameters using ServiceContext#currentCallContext method. It is only accessible from the current thread during the execution of a service method.Interceptor can read and update context parameters using ServiceInterceptorContext.

If an interceptor has been specified, but the user has not passed the caller context through the proxy, then for each call to the service method, an empty context will be dynamically created.

...