RequestType
- The expected request object. This is the model class that the event JSON is de-serialized toResponseType
- The expected Lambda function response object. Responses will be written to this model objectHandlerRequestType
- The request type for RequestHandler
.HandlerResponseType
- The response type for the RequestHandler
.public abstract class AbstractMicronautLambdaRuntime<RequestType,ResponseType,HandlerRequestType,HandlerResponseType> extends java.lang.Object implements io.micronaut.context.ApplicationContextProvider, AwsLambdaRuntimeApi
Modifier and Type | Field and Description |
---|---|
protected java.lang.Object |
handler |
protected java.lang.Class<HandlerRequestType> |
handlerRequestType |
protected java.lang.Class<HandlerResponseType> |
handlerResponseType |
protected java.lang.Class<RequestType> |
requestType |
protected java.lang.Class<ResponseType> |
responseType |
ERROR_TEMPLATE, INIT_ERROR_URI, INVOCATION_TEMPLATE, LAMBDA_RUNTIME_FUNCTION_ERROR_TYPE, NEXT_INVOCATION_URI, PATH_REQUEST_ID
Constructor and Description |
---|
AbstractMicronautLambdaRuntime()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
io.micronaut.context.ApplicationContextBuilder |
createApplicationContextBuilderWithArgs(java.lang.String... args) |
protected java.lang.Object |
createEnvironmentHandler() |
protected java.lang.Object |
createHandler(java.lang.String... args) |
protected HandlerRequestType |
createHandlerRequest(RequestType request) |
protected com.amazonaws.services.lambda.runtime.RequestHandler<HandlerRequestType,HandlerResponseType> |
createRequestHandler(java.lang.String... args) |
protected com.amazonaws.services.lambda.runtime.RequestStreamHandler |
createRequestStreamHandler(java.lang.String... args) |
protected ResponseType |
createResponse(HandlerResponseType handlerResponse) |
io.micronaut.context.ApplicationContext |
getApplicationContext() |
protected java.lang.String |
getEnv(java.lang.String name) |
protected io.micronaut.logging.LogLevel |
getLogLevel() |
protected void |
log(io.micronaut.logging.LogLevel level,
java.lang.String msg) |
protected void |
logn(io.micronaut.logging.LogLevel level,
java.lang.Object... messageParts) |
protected void |
logn(io.micronaut.logging.LogLevel logLevel,
java.lang.String msg)
Log with a line break.
|
protected java.util.Optional<java.lang.Class> |
parseHandlerClass(java.lang.String handler) |
protected void |
propagateTraceId(io.micronaut.http.HttpHeaders headers)
Get the X-Ray tracing header from the Lambda-Runtime-Trace-Id header in the API response.
|
protected void |
reportInitializationError(java.net.URL runtimeApiURL,
java.lang.Throwable e)
Reports Initialization error to the Runtime API.
|
protected com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent |
respond(io.micronaut.http.HttpStatus status,
java.lang.String body,
java.lang.String contentType) |
void |
run(java.lang.String... args)
Starts the runtime API event loop.
|
protected java.lang.String |
serializeAsJsonString(java.lang.Object value) |
protected boolean |
shouldLog(io.micronaut.logging.LogLevel level) |
protected void |
startRuntimeApiEventLoop(java.net.URL runtimeApiURL,
java.util.function.Predicate<java.net.URL> loopUntil,
java.lang.String... args)
Starts the runtime API event loop.
|
protected void |
validateHandler() |
protected <T> T |
valueFromContent(java.lang.String content,
java.lang.Class<T> valueType) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
errorUri, initializationErrorRequest, invocationErrorRequest, invocationResponseRequest, responseUri
protected java.lang.Object handler
protected final java.lang.Class<RequestType> requestType
protected final java.lang.Class<ResponseType> responseType
protected final java.lang.Class<HandlerRequestType> handlerRequestType
protected final java.lang.Class<HandlerResponseType> handlerResponseType
public AbstractMicronautLambdaRuntime()
public void run(java.lang.String... args) throws java.net.MalformedURLException
args
- Command line argumentsjava.net.MalformedURLException
- if the lambda endpoint URL is malformedprotected void validateHandler() throws io.micronaut.context.exceptions.ConfigurationException
io.micronaut.context.exceptions.ConfigurationException
- if the handler is not of type RequestHandler or RequestStreamHandlerpublic io.micronaut.context.ApplicationContext getApplicationContext()
getApplicationContext
in interface io.micronaut.context.ApplicationContextProvider
public io.micronaut.context.ApplicationContextBuilder createApplicationContextBuilderWithArgs(java.lang.String... args)
args
- command line argumentsApplicationContextBuilder
with the command line arguments as a property source and the environment set to lambda@Nullable protected com.amazonaws.services.lambda.runtime.RequestHandler<HandlerRequestType,HandlerResponseType> createRequestHandler(java.lang.String... args)
args
- Command Line ArgsRequestHandler
or null
.@Nullable protected com.amazonaws.services.lambda.runtime.RequestStreamHandler createRequestStreamHandler(java.lang.String... args)
args
- Command Line ArgsRequestStreamHandler
or null
.@Nullable protected java.lang.Object createHandler(java.lang.String... args)
args
- Command Line ArgscreateHandler(String...)
or createRequestStreamHandler(String...)
are implemented, it returns the handler returned by those methods. If they are not, it attempts to instantiate the class
referenced by the environment variable ReservedRuntimeEnvironmentVariables.HANDLER
via Introspection. Otherwise, it returns null
.@Nullable protected java.lang.Object createEnvironmentHandler()
ReservedRuntimeEnvironmentVariables.HANDLER
via Introspection. Otherwise, it returns null
.protected java.util.Optional<java.lang.Class> parseHandlerClass(@NonNull java.lang.String handler)
handler
- handler in format file.method, where file is the name of the file without an extension, and method is the name of a method or function that's defined in the file.@Nullable protected ResponseType createResponse(HandlerResponseType handlerResponse)
handlerResponse
- Handler response objectAPIGatewayProxyResponseEvent
it attempts to serialized the handler response
as a JSON String and set it in the response body. If the object cannot be serialized, a 400 response is returnedprotected com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent respond(io.micronaut.http.HttpStatus status, java.lang.String body, java.lang.String contentType)
status
- HTTP Status of the responsebody
- Body of the responsecontentType
- HTTP Header Content-Type valueAPIGatewayProxyResponseEvent
populated with the supplied status, body and content type@Nullable protected HandlerRequestType createHandlerRequest(RequestType request) throws com.fasterxml.jackson.core.JsonProcessingException, com.fasterxml.jackson.databind.JsonMappingException
request
- Request obtained from the Runtime APIAPIGatewayProxyRequestEvent
it attempts to build an object of type HandlerRequestType with the body of the request, else returns null
com.fasterxml.jackson.core.JsonProcessingException
- if underlying request body contains invalid contentcom.fasterxml.jackson.databind.JsonMappingException
- if the request body JSON structure does not match structure
expected for result type (or has other mismatch issues)protected void startRuntimeApiEventLoop(@NonNull java.net.URL runtimeApiURL, @NonNull java.util.function.Predicate<java.net.URL> loopUntil, java.lang.String... args)
runtimeApiURL
- The runtime API URL.loopUntil
- A predicate that allows controlling when the event loop should exitargs
- Command Line argumentsprotected void propagateTraceId(io.micronaut.http.HttpHeaders headers)
headers
- next API Response HTTP Headersprotected void reportInitializationError(java.net.URL runtimeApiURL, java.lang.Throwable e)
runtimeApiURL
- Runtime API URLe
- Exception thrown@Nullable protected java.lang.String serializeAsJsonString(java.lang.Object value)
value
- Object to be serialized@Nullable protected <T> T valueFromContent(java.lang.String content, java.lang.Class<T> valueType) throws com.fasterxml.jackson.core.JsonProcessingException, com.fasterxml.jackson.databind.JsonMappingException
T
- Type to be read intocontent
- JSON StringvalueType
- Class Type to be read intocom.fasterxml.jackson.core.JsonProcessingException
- if underlying input contains invalid contentcom.fasterxml.jackson.databind.JsonMappingException
- if the input JSON structure does not match structure
expected for result type (or has other mismatch issues)protected java.lang.String getEnv(java.lang.String name)
name
- the name of the environment variablenull
if the variable is not definedprotected void log(io.micronaut.logging.LogLevel level, java.lang.String msg)
level
- Log Levelmsg
- Message to logprotected io.micronaut.logging.LogLevel getLogLevel()
LogLevel
for the custom runtime.protected void logn(io.micronaut.logging.LogLevel logLevel, java.lang.String msg)
logLevel
- Log levelmsg
- Message to logprotected void logn(io.micronaut.logging.LogLevel level, java.lang.Object... messageParts)
level
- Log LevelmessageParts
- One or more message parts to concatenate and log if enabledprotected boolean shouldLog(io.micronaut.logging.LogLevel level)
level
- Log level