Versions Compared

Key

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

...

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

    Code Block
    languagejava
    themeRDark
    titleServiceCallContext.java
    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
    collapsetrue
    public interface ServiceCallInterceptor extends Serializable {
        //**
     Called BEFORE the service method* isIntercepts executed.
    a call to a public default void onInvoke(String mtd, Object[] args, ServiceContext ctx) throws ServiceInterceptException {service method.
         *
         * @param mtd // No-opMethod name.
        }
    
      *  //@param Calledargs AFTER the service method is executedMethod arguments.
        public default* void onComplete(String mtd, Object[] args, @Nullable Object res, ServiceContext ctx) throws ServiceInterceptException {@param ctx Service context.
         * @param call // No-opDelegated call.
        }
    
      *  //@return CalledService whenmethod onInvoke, onComplete or service method throws an exception.call result.
         */
        public defaultObject void onErrorinvoke(String mtd, Object[] args, ThrowableServiceContext errctx, ServiceContextCallable<Object> ctxcall) {
            // No-op.
        }
    }
    ServiceInterceptException - unchecked exception that is used to highlight the exception that occurred during method interception (not execution).
    throws Exception;
    }


Usage example

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

Code Block
languagejava
themeRDark
titleExample.java
collapsetrue
        ServiceCallInterceptor security = new ServiceCallInterceptor() {
            @Override public void onInvoke(String mtd, Object[] args, ServiceContextctx, ctxsvcCall) {
                // Check permission before execution of the method.
     -> {
           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 = new ServiceCallInterceptor() {
    mtd, args, ctx, svcCall) -> {
        @OverrideString publicsessionId void onInvoke(String mtd, Object[] args, ServiceContext ctx) {
     = ctx.currentCallContext().attribute("sessionId");
        AuditProvider prov = AuditProvider.get();

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

        AuditProvider.get().recordStartEvent(mtd, ctx.currentCallContext().attribute("sessionId"));
try {
             }

// Execute service method.
            svcCall.call();
       @Override public}
 void onComplete(String mtd, Object[] args, @Nullable Object res,catch ServiceContext(Exception ctxe) {
            // Record error.
            AuditProviderprov.getrecordError().recordFinishEvent(mtd, ctx.currentCallContext().attribute("sessionId"sessionId), e.getMessage());

            // Re-throw exception to }

initiator.
            throw e;
      @Override public void}
 onError(String mtd, Object[] args, Throwable err, ServiceContext ctx)finally {
            // Record finish event after execution  AuditProvider.get().recordError(mtd, ctx.currentCallContext().attribute("sessionId"), err.getMessage());of the service method.
            } prov.recordFinishEvent(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.
        MyServiceMyService proxy = ignite.services().serviceProxy("service", MyService.class, false, callCtx, 0);

        // AMake businessservice method call to be intercepted.
        proxy.placeOrder(order1);
        proxy.placeOrder(order2);  

Implementation details

Deployment

...