List of usage examples for com.amazonaws.services.lambda.model InvokeResult getStatusCode
public Integer getStatusCode()
From source file:example.lambda.InvokeLambdaFunction.java
License:Apache License
public static void main(String[] args) { String function_name = "HelloFunction"; String function_input = "{\"who\":\"AWS SDK for Java\"}"; AWSLambda lambda = AWSLambdaClientBuilder.defaultClient(); InvokeRequest req = new InvokeRequest().withFunctionName(function_name) .withPayload(ByteBuffer.wrap(function_input.getBytes())); InvokeResult res = lambda.invoke(req); if (res.getStatusCode() == 200) { System.out.println("Lambda function returned:"); ByteBuffer response_payload = res.getPayload(); System.out.println(new String(response_payload.array())); } else {/*from ww w. ja v a2 s . com*/ System.out.format("Received a non-OK response from AWS: %d\n", res.getStatusCode()); } }
From source file:example.lambda.InvokeLambdaFunctionAsync.java
License:Apache License
public static void main(String[] args) { String function_name = "HelloFunction"; String function_input = "{\"who\":\"AWS SDK for Java\"}"; AWSLambdaAsync lambda = AWSLambdaAsyncClientBuilder.defaultClient(); InvokeRequest req = new InvokeRequest().withFunctionName(function_name) .withPayload(ByteBuffer.wrap(function_input.getBytes())); Future<InvokeResult> future_res = lambda.invokeAsync(req); System.out.print("Waiting for future"); while (future_res.isDone() == false) { System.out.print("."); try {// w w w . ja v a 2s .c o m Thread.sleep(1000); } catch (InterruptedException e) { System.err.println("\nThread.sleep() was interrupted!"); System.exit(1); } } try { InvokeResult res = future_res.get(); if (res.getStatusCode() == 200) { System.out.println("\nLambda function returned:"); ByteBuffer response_payload = res.getPayload(); System.out.println(new String(response_payload.array())); } else { System.out.format("Received a non-OK response from AWS: %d\n", res.getStatusCode()); } } catch (InterruptedException | ExecutionException e) { System.err.println(e.getMessage()); System.exit(1); } System.exit(0); }
From source file:org.apache.nifi.processors.aws.lambda.PutLambda.java
License:Apache License
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { FlowFile flowFile = session.get();//from w w w . j a v a2 s . c o m if (flowFile == null) { return; } final String functionName = context.getProperty(AWS_LAMBDA_FUNCTION_NAME).getValue(); final String qualifier = context.getProperty(AWS_LAMBDA_FUNCTION_QUALIFIER).getValue(); // Max size of message is 6 MB if (flowFile.getSize() > MAX_REQUEST_SIZE) { getLogger().error("Max size for request body is 6mb but was {} for flow file {} for function {}", new Object[] { flowFile.getSize(), flowFile, functionName }); session.transfer(flowFile, REL_FAILURE); return; } final AWSLambdaClient client = getClient(); try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); session.exportTo(flowFile, baos); InvokeRequest invokeRequest = new InvokeRequest().withFunctionName(functionName) .withLogType(LogType.Tail).withInvocationType(InvocationType.RequestResponse) .withPayload(ByteBuffer.wrap(baos.toByteArray())).withQualifier(qualifier); long startTime = System.nanoTime(); InvokeResult result = client.invoke(invokeRequest); flowFile = session.putAttribute(flowFile, AWS_LAMBDA_RESULT_STATUS_CODE, result.getStatusCode().toString()); if (!StringUtils.isBlank(result.getLogResult())) { flowFile = session.putAttribute(flowFile, AWS_LAMBDA_RESULT_LOG, new String(Base64.decode(result.getLogResult()), Charset.defaultCharset())); } if (result.getPayload() != null) { flowFile = session.putAttribute(flowFile, AWS_LAMBDA_RESULT_PAYLOAD, new String(result.getPayload().array(), Charset.defaultCharset())); } if (!StringUtils.isBlank(result.getFunctionError())) { flowFile = session.putAttribute(flowFile, AWS_LAMBDA_RESULT_FUNCTION_ERROR, result.getFunctionError()); session.transfer(flowFile, REL_FAILURE); } else { session.transfer(flowFile, REL_SUCCESS); final long totalTimeMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime); session.getProvenanceReporter().send(flowFile, functionName, totalTimeMillis); } } catch (final InvalidRequestContentException | InvalidParameterValueException | RequestTooLargeException | ResourceNotFoundException | UnsupportedMediaTypeException unrecoverableException) { getLogger().error("Failed to invoke lambda {} with unrecoverable exception {} for flow file {}", new Object[] { functionName, unrecoverableException, flowFile }); flowFile = populateExceptionAttributes(session, flowFile, unrecoverableException); session.transfer(flowFile, REL_FAILURE); } catch (final TooManyRequestsException retryableServiceException) { getLogger().error( "Failed to invoke lambda {} with exception {} for flow file {}, therefore penalizing flowfile", new Object[] { functionName, retryableServiceException, flowFile }); flowFile = populateExceptionAttributes(session, flowFile, retryableServiceException); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); context.yield(); } catch (final AmazonServiceException unrecoverableServiceException) { getLogger().error("Failed to invoke lambda {} with exception {} for flow file {} sending to fail", new Object[] { functionName, unrecoverableServiceException, flowFile }); flowFile = populateExceptionAttributes(session, flowFile, unrecoverableServiceException); session.transfer(flowFile, REL_FAILURE); context.yield(); } catch (final Exception exception) { getLogger().error("Failed to invoke lambda {} with exception {} for flow file {}", new Object[] { functionName, exception, flowFile }); session.transfer(flowFile, REL_FAILURE); context.yield(); } }