Search in sources :

Example 1 with ServerlessException

use of org.springframework.cloud.serverless.ServerlessException in project spring-teaching-demos by kennyk65.

the class ApplicationLambdaEntry method handleRequest.

public Object handleRequest(Object o, Context context) {
    LambdaLogger logger = context.getLogger();
    logger.log("Start of Lambda Function");
    // Get a reference to the Spring ApplicationMainEntry Context:
    ApplicationContext spring = SpringLauncherSingleton.getInstance();
    // Get the bean / entry point to your application:
    Map<String, Object> map = spring.getBeansWithAnnotation(Handler.class);
    if (map == null || map.size() < 1) {
        throw new ServerlessException("No @Handler bean was found in the application context.");
    } else if (map.size() > 1) {
        throw new ServerlessException("Only one @Handler bean is permitted in the application context, found " + map.size() + ".");
    }
    // Get the one and only one @Handler bean:
    Object handlerBean = map.values().iterator().next();
    // Get the AWS Lambda Adapter:
    LambdaAdapter adapter = spring.getBean(LambdaAdapter.class);
    // Call the @CloudFunction on the @Handler:
    Object result = adapter.handle(handlerBean, o, context);
    logger.log("End of Lambda Function, " + context.getRemainingTimeInMillis() + " millisecond of time remaining.\n" + Runtime.getRuntime().freeMemory() + " memory free\n");
    return result;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ServerlessException(org.springframework.cloud.serverless.ServerlessException) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Example 2 with ServerlessException

use of org.springframework.cloud.serverless.ServerlessException in project spring-teaching-demos by kennyk65.

the class LambdaAdapter method handle.

/**
 * Invoke the @CloudFunction method on the @Handler bean,
 * resolving method parameters as needed.
 */
public Object handle(Object handlerBean, Object o, Context context) {
    // TODO - CACHE THIS FOR PERFORMANCE.
    Method cloudFunction = findAnnotatedMethod(handlerBean);
    Parameter[] parameters = cloudFunction.getParameters();
    Object[] args = null;
    if (parameters != null || parameters.length > 0) {
        args = new Object[parameters.length];
        int i = 0;
        for (Parameter p : parameters) {
            // UNMARSHALLING, LOGGERS, ETC.
            if (context.getClass().isAssignableFrom(p.getType())) {
                args[i] = context;
            } else if (context.getClass().isAssignableFrom(Object.class)) {
                args[i] = o;
            }
            i++;
        }
    }
    Object returnVal = null;
    try {
        returnVal = cloudFunction.invoke(handlerBean, args);
    } catch (Exception e) {
        throw new ServerlessException("Error invoking @CloudFunction method", e);
    }
    return returnVal;
}
Also used : Parameter(java.lang.reflect.Parameter) ServerlessException(org.springframework.cloud.serverless.ServerlessException) Method(java.lang.reflect.Method) ServerlessException(org.springframework.cloud.serverless.ServerlessException)

Aggregations

ServerlessException (org.springframework.cloud.serverless.ServerlessException)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ApplicationContext (org.springframework.context.ApplicationContext)1