在呼叫者级别捕获远程服务中的用户异常

时间:2016-11-11 09:39:01

标签: java service ignite

我在一个相互依赖的Ignite集群中运行多个服务。

当我调用远程服务函数时,我想在调用者级别捕获(用户定义的)异常。请参阅基于Service example in the docs for 1.7的示例。

MyUserException.java

package com.example.testing;

public class MyUserException extends Throwable {}

MyCounterService.java

package com.example.testing;

public interface MyCounterService {
    int increment() throws MyUserException;
}

MyCounterServiceImpl.java (错误条件为ignite.cluster().forYoungest()

package com.example.testing;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteServices;
import org.apache.ignite.Ignition;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.apache.ignite.services.Service;
import org.apache.ignite.services.ServiceContext;

public class MyCounterServiceImpl implements MyCounterService, Service {

    @IgniteInstanceResource
    private Ignite ignite;

    private int value = 0;

    public int increment() throws MyUserException {
        if ((value % 2) == 0) {
            throw new MyUserException();
        } else {
            value++;
        }
        return value;
    }

    public static void main(String [] args) {
        Ignite ignite = Ignition.start();
        IgniteServices svcs = ignite.services(ignite.cluster().forYoungest());
        svcs.deployNodeSingleton("MyCounterService", new MyCounterServiceImpl());
    }

    @Override
    public void cancel(ServiceContext ctx) {
        System.out.println("Service cancelled");
    }

    @Override
    public void init(ServiceContext ctx) throws Exception {
        System.out.println("Service initialized");
    }

    @Override
    public void execute(ServiceContext ctx) throws Exception {
        System.out.println("Service running");
    }
}

MyCallerService.java

package com.example.testing;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.apache.ignite.services.Service;
import org.apache.ignite.services.ServiceContext;

public class MyCallerService implements Service {

    @IgniteInstanceResource
    private Ignite ignite;

    private Boolean stopped;

    public void run() {
        stopped = false;
        MyCounterService service = ignite.services().serviceProxy("MyCounterService", MyCounterService.class, false);
        while (!stopped)
        {
            try {
                Thread.sleep(500);
                service.increment();
            } catch (MyUserException e) {
                System.out.println("Got exception");
                //e.printStackTrace();
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
            catch (IgniteException e) {
                System.out.println("Got critial exception");
                // would print the actual user exception
                //e.getCause().getCause().getCause().printStackTrace();
                break;
            }
        }
    }

    public static void main(String [] args) {
        Ignite ignite = Ignition.start();
        ignite.services(ignite.cluster().forYoungest()).deployNodeSingleton("MyCallerService", new MyCallerService());
    }

    @Override
    public void cancel(ServiceContext ctx) {
        stopped = true;
    }

    @Override
    public void init(ServiceContext ctx) throws Exception {

    }

    @Override
    public void execute(ServiceContext ctx) throws Exception {
        run();
    }
}

在调用者级别没有捕获异常。相反,这些例外显示在控制台中。在调用服务函数时,如何正确捕获和处理异常?

MyCounterServiceImpl的输出

[18:23:23] Ignite node started OK (id=c82df19c)
[18:23:23] Topology snapshot [ver=1, servers=1, clients=0, CPUs=4, heap=3.5GB]
Service initialized
Service running
[18:23:27] Topology snapshot [ver=2, servers=2, clients=0, CPUs=4, heap=7.0GB]
Nov 17, 2016 6:23:28 PM org.apache.ignite.logger.java.JavaLogger error
SCHWERWIEGEND: Failed to execute job [jobId=82580537851-3c0a354f-69b5-496c-af10-ee789a5387c3, ses=GridJobSessionImpl [ses=GridTaskSessionImpl [taskName=o.a.i.i.processors.service.GridServiceProxy$ServiceProxyCallable, dep=LocalDeployment [super=GridDeployment [ts=1479403401422, depMode=SHARED, clsLdr=sun.misc.Launcher$AppClassLoader@1d44bcfa, clsLdrId=4fe60537851-c82df19c-cdff-43ef-b7b6-e8485231629a, userVer=0, loc=true, sampleClsName=java.lang.String, pendingUndeploy=false, undeployed=false, usage=0]], taskClsName=o.a.i.i.processors.service.GridServiceProxy$ServiceProxyCallable, sesId=72580537851-3c0a354f-69b5-496c-af10-ee789a5387c3, startTime=1479403408961, endTime=9223372036854775807, taskNodeId=3c0a354f-69b5-496c-af10-ee789a5387c3, clsLdr=sun.misc.Launcher$AppClassLoader@1d44bcfa, closed=false, cpSpi=null, failSpi=null, loadSpi=null, usage=1, fullSup=false, subjId=3c0a354f-69b5-496c-af10-ee789a5387c3, mapFut=IgniteFuture [orig=GridFutureAdapter [resFlag=0, res=null, startTime=1479403408960, endTime=0, ignoreInterrupts=false, state=INIT]]], jobId=82580537851-3c0a354f-69b5-496c-af10-ee789a5387c3]]
class org.apache.ignite.IgniteException: null
    at org.apache.ignite.internal.processors.closure.GridClosureProcessor$C2V2.execute(GridClosureProcessor.java:2009)
    at org.apache.ignite.internal.processors.job.GridJobWorker$2.call(GridJobWorker.java:509)
    at org.apache.ignite.internal.util.IgniteUtils.wrapThreadLoader(IgniteUtils.java:6521)
    at org.apache.ignite.internal.processors.job.GridJobWorker.execute0(GridJobWorker.java:503)
    at org.apache.ignite.internal.processors.job.GridJobWorker.body(GridJobWorker.java:456)
    at org.apache.ignite.internal.util.worker.GridWorker.run(GridWorker.java:110)
    at org.apache.ignite.internal.processors.job.GridJobProcessor.processJobExecuteRequest(GridJobProcessor.java:1161)
    at org.apache.ignite.internal.processors.job.GridJobProcessor$JobExecutionListener.onMessage(GridJobProcessor.java:1766)
    at org.apache.ignite.internal.managers.communication.GridIoManager.invokeListener(GridIoManager.java:1238)
    at org.apache.ignite.internal.managers.communication.GridIoManager.processRegularMessage0(GridIoManager.java:866)
    at org.apache.ignite.internal.managers.communication.GridIoManager.access$1700(GridIoManager.java:106)
    at org.apache.ignite.internal.managers.communication.GridIoManager$5.run(GridIoManager.java:829)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.ignite.internal.processors.service.GridServiceProxy$ServiceProxyCallable.call(GridServiceProxy.java:392)
    at org.apache.ignite.internal.processors.closure.GridClosureProcessor$C2V2.execute(GridClosureProcessor.java:2006)
    ... 14 more
Caused by: com.example.testing.MyUserException
    at com.example.testing.MyCounterServiceImpl.increment(MyCounterServiceImpl.java:19)
    ... 20 more

MyCallerService的输出

[18:23:28] Ignite node started OK (id=3c0a354f)
[18:23:28] Topology snapshot [ver=2, servers=2, clients=0, CPUs=4, heap=7.0GB]
Nov 17, 2016 6:23:28 PM org.apache.ignite.logger.java.JavaLogger error
SCHWERWIEGEND: Failed to obtain remote job result policy for result from ComputeTask.result(..) method (will fail the whole task): GridJobResultImpl [job=C2V2 [c=ServiceProxyCallable [mtdName=increment, svcName=MyCounterService, ignite=null]], sib=GridJobSiblingImpl [sesId=72580537851-3c0a354f-69b5-496c-af10-ee789a5387c3, jobId=82580537851-3c0a354f-69b5-496c-af10-ee789a5387c3, nodeId=c82df19c-cdff-43ef-b7b6-e8485231629a, isJobDone=false], jobCtx=GridJobContextImpl [jobId=82580537851-3c0a354f-69b5-496c-af10-ee789a5387c3, timeoutObj=null, attrs={}], node=TcpDiscoveryNode [id=c82df19c-cdff-43ef-b7b6-e8485231629a, addrs=[0:0:0:0:0:0:0:1%lo, 127.0.0.1, 172.18.22.52], sockAddrs=[/0:0:0:0:0:0:0:1%lo:47500, /127.0.0.1:47500, /172.18.22.52:47500], discPort=47500, order=1, intOrder=1, lastExchangeTime=1479403407847, loc=false, ver=1.7.0#20160801-sha1:383273e3, isClient=false], ex=class o.a.i.IgniteException: null, hasRes=true, isCancelled=false, isOccupied=true]
class org.apache.ignite.IgniteException: Remote job threw user exception (override or implement ComputeTask.result(..) method if you would like to have automatic failover for this exception).
    at org.apache.ignite.compute.ComputeTaskAdapter.result(ComputeTaskAdapter.java:101)
    at org.apache.ignite.internal.processors.task.GridTaskWorker$4.apply(GridTaskWorker.java:946)
    at org.apache.ignite.internal.processors.task.GridTaskWorker$4.apply(GridTaskWorker.java:939)
    at org.apache.ignite.internal.util.IgniteUtils.wrapThreadLoader(IgniteUtils.java:6553)
    at org.apache.ignite.internal.processors.task.GridTaskWorker.result(GridTaskWorker.java:939)
    at org.apache.ignite.internal.processors.task.GridTaskWorker.onResponse(GridTaskWorker.java:810)
    at org.apache.ignite.internal.processors.task.GridTaskProcessor.processJobExecuteResponse(GridTaskProcessor.java:995)
    at org.apache.ignite.internal.processors.task.GridTaskProcessor$JobMessageListener.onMessage(GridTaskProcessor.java:1220)
    at org.apache.ignite.internal.managers.communication.GridIoManager.invokeListener(GridIoManager.java:1238)
    at org.apache.ignite.internal.managers.communication.GridIoManager.processRegularMessage0(GridIoManager.java:866)
    at org.apache.ignite.internal.managers.communication.GridIoManager.access$1700(GridIoManager.java:106)
    at org.apache.ignite.internal.managers.communication.GridIoManager$5.run(GridIoManager.java:829)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: class org.apache.ignite.IgniteException: null
    at org.apache.ignite.internal.processors.closure.GridClosureProcessor$C2V2.execute(GridClosureProcessor.java:2009)
    at org.apache.ignite.internal.processors.job.GridJobWorker$2.call(GridJobWorker.java:509)
    at org.apache.ignite.internal.util.IgniteUtils.wrapThreadLoader(IgniteUtils.java:6521)
    at org.apache.ignite.internal.processors.job.GridJobWorker.execute0(GridJobWorker.java:503)
    at org.apache.ignite.internal.processors.job.GridJobWorker.body(GridJobWorker.java:456)
    at org.apache.ignite.internal.util.worker.GridWorker.run(GridWorker.java:110)
    at org.apache.ignite.internal.processors.job.GridJobProcessor.processJobExecuteRequest(GridJobProcessor.java:1161)
    at org.apache.ignite.internal.processors.job.GridJobProcessor$JobExecutionListener.onMessage(GridJobProcessor.java:1766)
    ... 7 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.ignite.internal.processors.service.GridServiceProxy$ServiceProxyCallable.call(GridServiceProxy.java:392)
    at org.apache.ignite.internal.processors.closure.GridClosureProcessor$C2V2.execute(GridClosureProcessor.java:2006)
    ... 14 more
Caused by: com.example.testing.MyUserException
    at com.example.testing.MyCounterServiceImpl.increment(MyCounterServiceImpl.java:19)
    ... 20 more

Got critial exception

以下这是一个需要解决的错误: https://issues.apache.org/jira/browse/IGNITE-4298

1 个答案:

答案 0 :(得分:0)

我认为异常必须抛到调用者节点。你能提供完整的代码示例吗?另外,奇怪的是,在有服务的节点上,是空值的异常。

UPD。

请问你还可以添加日志,因为对我而言,所有工作都按预期进行。我捕获了MyUserException,并在日志消息“Got exception”。