Versions Compared

Key

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

...

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

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

            @Override public void onComplete(String mtd, Object[] args, @Nullable Object res, ServiceContext ctx) {
                AuditProvider.get().recordFinishEvent(mtd, ctx.currentCallContext().attribute("sessionId"));
            }

            @Override public void onError(String mtd, Object[] args, Throwable err, ServiceContext ctx) {
                AuditProvider.get().recordError(mtd, ctx.currentCallContext().attribute("sessionId"), err.getMessage());
            }
        }

        // Set context parameters for the 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);
 
        // Set context parameters for the service proxy.
        ServiceCallContext callCtx = ServiceCallContext.builder().put("sessionId", sessionId).build();

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

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

...