3

I have created the following FutureTask method to run a method asynchronously.

public FutureTask<Object> SendAggregateEventAsync(final
        com.Company.Product.SDK.Device.AggregateEvent.ClassObject 
        request)
{
    FutureTask<Object> futureTask;

    futureTask = new FutureTask<Object>(new Runnable() {
        public void run()
        {
            try {
                SendAggregateEvent(request);
            } catch (ResponseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }, null);

    return futureTask;
}

public void SendAggregateEvent(
        com.Company.Product.SDK.Device.AggregateEvent.ClassObject 
        request) throws ResponseException
{
    try 
    {
        if(request == null) throw new IllegalArgumentException("request");

        String[] s_array = new String[0];
        s_array[0] = "EventTime";

        String namespace = "http://Product.Company.com/" +
                "v1.0/Device/AggregateEvent";

        IBindingFactory factory;

        factory = BindingDirectory.getFactory(
                com.Compant.Product.SDK.Device.AggregateEvent.
                ClassObject.class);

        String message = ChangeDatesToUTC(MessageHelper.
                SerializeObject(factory, request), s_array, namespace);

        SendMessage(message);

    } catch (JiBXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

In order to compile I must catch the ResponseException in the FutureTask, but it is vital that this ResponseException be thrown to the implementing application and not caught by FutureTask. Is there a way around this where I can throw this exception out of FutureTask?

JME
  • 2,293
  • 9
  • 36
  • 56
  • This question is a duplicate. [Here is the original](http://stackoverflow.com/questions/3555302/how-to-catch-exceptions-in-futuretask). – Emily Mabrey Jun 03 '13 at 20:38
  • 1
    Java naming conventions: method names should be `camelCase`. The first letter in the method name should not be uppercase. While .NET languages do that, Java frowns upon that. – fge Jun 03 '13 at 21:15

1 Answers1

5

Pass a Callable to the constructor instead of passing a Runnable, and you won't have to catch the exception anymore:

futureTask = new FutureTask<Object>(new Callable() {
    public Object call() throws ResponseException {
        SendAggregateEvent(request);
        return null;
    }
};

(but the generic type of the FutureTask should be Void rather than Object).

If a ResponseException is thrown by call(), the get() method of the FutureTask will throw an ExecutionException, and the cause of this exception will be the ResponseException.

That said, shouldn't you simply submit the Callable to an ExecutorService, and let it create a Future for you? Also try to respect the Java naming conventions, and remove the throws clause from your method, since it doesn't throw any exception. The naming is also bad: your method doesn't send anything. It only creates a FutureTask which, when executed, will send an event.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    Unfortunately the naming conventions for these methods were not chose by me, and I must follow instructions given. I have removed the throws clause, but I am having trouble converting from a Runnable to a Callable. I would like to avoid creating another class for the method call that I am trying to do asynchronously, is there a way to do this. – JME Jun 03 '13 at 20:49
  • See my edited answer for how to create the Callable. I don't understand what you mean by the last part of the comment. You'll have to execute that task somewhere, otherwise, creating it doesn't serve any purpose. – JB Nizet Jun 03 '13 at 21:12
  • 1
    @JBNizet that is the answer, maybe it should be mentioned for further clarification that checked exceptions thrown by a `FutureTask` get wrapped into the declared `ExecutionException` (and the original exception is available via the latter exception's `.getCause()`). – fge Jun 03 '13 at 21:21