/** * submits the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. Parsing and Fallback occur on the * {@code userExecutor} thread. */ public ListenableFuture<?> submit(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> submitting %s", commandName); return withFallback(transform(http.submit(command), transformer, userExecutor), fallback); }
@Override public Object apply(Invocation in) { if (isFuture(in.getInvokable())) { return submit(in); } Invocation async = toAsync(in); Optional<Long> timeoutNanos = config.getTimeoutNanos(async); if (timeoutNanos.isPresent()) { return invokeWithTimeout(async, timeoutNanos.get()); } return invoke(async); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
/** * looks up the corresponding {@code Invocation} that returns a * {@code Future}. Only Invokables that return {@code Futures} are annotated * in a way that can be parsed into an {@linkplain HttpRequest}. */ private Invocation toAsync(Invocation in) { Invocation async = sync2async.apply(in); checkState(isFuture(async.getInvokable()), "not a future: %s", async); return async; }
public void testMethodWithTimeoutRunsTimeLimiter() throws Exception { expect(config.getTimeoutNanos(asyncGet)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andReturn(response); replay(http, timeLimiter, fallback, config, future); invokeHttpMethod.apply(get); }
@SuppressWarnings("unchecked") @BeforeMethod void createMocks() { http = createMock(HttpCommandExecutorService.class); timeLimiter = createMock(TimeLimiter.class); fallback = createMock(org.jclouds.Fallback.class); config = createMock(InvocationConfig.class); future = createMock(ListenableFuture.class); invokeHttpMethod = new InvokeSyncToAsyncHttpMethod(sync2async, toRequest, http, transformerForRequest, timeLimiter, config, userThreads); expect(config.getCommandName(asyncGet)).andReturn("ns:get"); expect(config.getFallback(asyncGet)).andReturn(fallback); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
/** * looks up the corresponding {@code Invocation} that returns a * {@code Future}. Only Invokables that return {@code Futures} are annotated * in a way that can be parsed into an {@linkplain HttpRequest}. */ private Invocation toAsync(Invocation in) { Invocation async = sync2async.apply(in); checkState(isFuture(async.getInvokable()), "not a future: %s", async); return async; }
public void testMethodWithNoTimeoutCallGetDirectly() throws Exception { expect(config.getTimeoutNanos(asyncGet)).andReturn(Optional.<Long> absent()); expect(http.invoke(new HttpCommand(getRequest))).andReturn(response); replay(http, timeLimiter, fallback, config, future); invokeHttpMethod.apply(get); }
@Override public Object apply(Invocation in) { if (isFuture(in.getInvokable())) { return submit(in); } Invocation async = toAsync(in); Optional<Long> timeoutNanos = config.getTimeoutNanos(async); if (timeoutNanos.isPresent()) { return invokeWithTimeout(async, timeoutNanos.get()); } return invoke(async); }
/** * submits the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. Parsing and Fallback occur on the * {@code userExecutor} thread. */ public ListenableFuture<?> submit(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> submitting %s", commandName); return withFallback(transform(http.submit(command), transformer, userExecutor), fallback); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
/** * looks up the corresponding {@code Invocation} that returns a * {@code Future}. Only Invokables that return {@code Futures} are annotated * in a way that can be parsed into an {@linkplain HttpRequest}. */ private Invocation toAsync(Invocation in) { Invocation async = sync2async.apply(in); checkState(isFuture(async.getInvokable()), "not a future: %s", async); return async; }
@SuppressWarnings("unchecked") public void testSubmitRunsFallbackCreateOnGet() throws Exception { IllegalStateException exception = new IllegalStateException(); expect(http.submit(new HttpCommand(getRequest))).andReturn( Futures.<HttpResponse> immediateFailedFuture(exception)); expect(fallback.create(exception)).andReturn(Futures.<HttpResponse> immediateFuture(fallbackResponse)); // not using the field, as you can see above we are making an immediate // failed future instead. future = createMock(ListenableFuture.class); replay(http, timeLimiter, fallback, config, future); assertEquals(ListenableFuture.class.cast(invokeHttpMethod.apply(asyncGet)).get(), fallbackResponse); } }
@Override public Object apply(Invocation in) { if (isFuture(in.getInvokable())) { return submit(in); } Invocation async = toAsync(in); Optional<Long> timeoutNanos = config.getTimeoutNanos(async); if (timeoutNanos.isPresent()) { return invokeWithTimeout(async, timeoutNanos.get()); } return invoke(async); }
/** * submits the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. Parsing and Fallback occur on the * {@code userExecutor} thread. */ public ListenableFuture<?> submit(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> submitting %s", commandName); return withFallback(transform(http.submit(command), transformer, userExecutor), fallback); }
public void testDirectCallRunsFallbackCreateOrPropagate() throws Exception { IllegalStateException exception = new IllegalStateException(); expect(config.getTimeoutNanos(asyncGet)).andReturn(Optional.<Long> absent()); expect(http.invoke(new HttpCommand(getRequest))).andThrow(exception); expect(fallback.createOrPropagate(exception)).andReturn(fallbackResponse); replay(http, timeLimiter, fallback, config, future); assertEquals(invokeHttpMethod.apply(get), fallbackResponse); }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }
public void testTimeLimitedRunsFallbackCreateOrPropagate() throws Exception { IllegalStateException exception = new IllegalStateException(); expect(config.getTimeoutNanos(asyncGet)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andThrow(exception); expect(fallback.createOrPropagate(exception)).andReturn(fallbackResponse); replay(http, timeLimiter, fallback, config, future); assertEquals(invokeHttpMethod.apply(get), fallbackResponse); }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }