LambdaController

Abstract service that receives remove calls via the AWS Lambda protocol.

Implements: ILambdaController, IOpenable, IConfigurable, IReferenceable

Description

The LambdaController class allows you to create abstract services that receive remove calls via the AWS Lambda protocol.

Important points

This service is intended to work inside LambdaFunction container that exploses registered actions externally.

Configuration parameters

  • dependencies:
    • controller: override for Controller dependency

References

  • *:logger:*:*:1.0: (optional) ILogger components to pass log messages.
  • *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements.

Constructors

Creates an instance of this service.

public LambdaController(String name)

  • name: string - service name to generate action cmd.

Fields

_counters

Performance counters.

protected CompositeCounters _counters = new CompositeCounters(); CompositeCounters _counters = new CompositeCounters();

_dependencyResolver

Dependency resolver.

protected DependencyResolver _dependencyResolver = new DependencyResolver();

_logger

Dependency resolver.

protected CompositeLogger _logger = new CompositeLogger(); CompositeLogger _logger = new CompositeLogger();

_tracer

Tracer.

protected CompositeTracer _tracer = new CompositeTracer();

Instance methods

act

Calls registered action in this lambda function. The “cmd” parameter in the action parameters determines the action shall be called.

  • This method shall only be used in testing.

public Object act(Map<String, Object> params) throws ApplicationException

  • params: Object - action parameters.
  • returns: Object - results

applyInterceptors

Applies given action to the interseptors

protected Function<Map<String, Object>, ?> applyInterceptors(Function<Map<String, Object>, ?> action)

  • action: Function<Map<String, Object>, ?> - applied action.
  • returns: Function<Map<String, Object>, ?> - wrapped interceptors action.

applyValidation

Applies a validation according to a given schema.

protected Function<Map<String, Object>, ?> applyValidation(Schema schema, Function<Map<String, Object>, ?> action)

  • schema: Schema - validation schema
  • action: Function<Map<String, Object>, ?> - action
  • returns: Function<Map<String, Object>, ?> - results

close

Closes a component and frees used resources.

public void close(IContext context) throws ApplicationException

  • context: IContext - (optional) a context to trace execution through a call chain.

configure

Configures a component by passing configuration parameters.

public void configure(ConfigParams config) throws ConfigException

  • config: ConfigParams - configuration parameters to be set.

generateActionCmd

Adds .cmd to the name of the command.

protected String generateActionCmd(String name)

  • name: string - name of the command
  • returns: string - generated command

getActions

Gets all the actions supported by the service.

public List<LambdaAction[]> getActions()

instrument

Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.

protected InstrumentTiming instrument(IContext context, String name)

  • context: IContext - (optional) a context to trace execution through a call chain.
  • name: string - method name.
  • returns: InstrumentTiming - InstrumentTiming object to end the time measurement.

isOpen

Checks if the component is open.

public boolean isOpen()

  • returns: boolean - true if the component is open and false otherwise.

open

Opens the component.

public void open(IContext context) throws ApplicationException

  • context: IContext - (optional) a context to trace execution through a call chain.

registerAction

Registers an action in AWS Lambda function.

protected void registerAction(String name, Schema schema, Function<Map<String, Object>, ?> action)

  • name: string - action name
  • schema: Schema - validation schema used to validate received parameters.
  • action: Function<Map<String, Object>, ?> - action function that is called when an operation is invoked.

registerActionWithAuth

Registers an action with authorization.

protected void registerActionWithAuth(String name, Schema schema, AuthorizeFunction<Map<String, Object>, Function<Map<String, Object>, ?>, ?> authorize, Function<Map<String, Object>, ?> action)

  • name: string - action’s name
  • schema: Schema - validation schema used to validate received parameters.
  • authorize: Function<Map<String, Object>, ?>, ?> - authorization interceptor
  • action: Function<Map<String, Object>, ?> - action function that is called when an operation is invoked.

registerInterceptor

Registers a middleware for actions in AWS Lambda service.

protected void registerInterceptor(AuthorizeFunction<Map<String, Object>, Function<Map<String, Object>, ?>, ?> action)

  • action: AuthorizeFunction<Map<String, Object>, Function<Map<String, Object>, ?>, ?> - action function that is called when middleware is invoked.

setReferences

Sets references to dependent components.

public void setReferences(IReferences references) throws ReferenceException, ConfigException

  • references: IReferences - references used to locate the component dependencies.

Abstract methods

register

Registers all service routes in an HTTP endpoint.

This method is called by the service and must be overriden in child classes.

protected void register()

Examples

class MyLambdaController extends LambdaController {
   private IMyService _service;
   ...
   public MyLambdaController() {
      super('v1.myservice');
      this._dependencyResolver.put(
          "service",
          new Descriptor("mygroup","controller","*","*","1.0")
      );
   }
   public void setReferences(references: ) {
      super.setReferences(references);
      this._service = this._dependencyResolver.getRequired(IDummyService.class, "service");
   }

   private MyData getData(IContext context, String id) {
        return this._service.getMyData(context, id);
   }

   public void register() {
       this.registerAction("/get_mydata",
                  null,
                  this::getData
         );
       ...
   }
}

var controller = new MyLambdaController();
controller.configure(ConfigParams.fromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 8080
));

controller.open("123");

See also