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;
}
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;
}
Aggregations