Esempio n. 1
0
    def post(self, request: Request) -> None:
        """
        Dispatches a request over middleware. Returns when message put onto outgoing channel by producer,
        does not wait for response from a consuming application i.e. is fire-and-forget
        :param request: The request to dispatch
        :return: None
        """

        if self._producer is None:
            raise ConfigurationException("Command Processor requires a Producer to post to a Broker")
        if self._message_mapper_registry is None:
            raise ConfigurationException("Command Processor requires a Message Mapper Registry to post to a Broker")

        message_mapper= self._message_mapper_registry.lookup(request)
        message = message_mapper(request)
        self._message_store.add(message)
        self._producer.send(message)
Esempio n. 2
0
    def send(self, request):
        """
        Dispatches a request. Expects one and one only target handler
        :param request: The request to dispatch
        :return: None, will throw a ConfigurationException if more than one handler factor is registered for the command
        """

        handler_factories = self._registry.lookup(request)
        if len(handler_factories) != 1:
            raise ConfigurationException("There is no handler registered for this request")
        handler = handler_factories[0]()
        handler.handle(request)
Esempio n. 3
0
    def register(self, request_class: Request,
                 mapper_func: Callable[[Request], BrightsideMessage]) -> None:
        """Adds a message mapper to a factory, using the requests key
        :param mapper_func: A callback that creates a BrightsideMessage from a Request
        :param request_class: A request type
        """

        key = request_class.__name__
        if key not in self._registry:
            self._registry[key] = mapper_func
        else:
            raise ConfigurationException(
                "There is already a message mapper defined for this key; there can be only one"
            )
Esempio n. 4
0
 def lookup(self, request_class: Request) -> Callable[[Request], Message]:
     """
     Looks up the message mapper function associated with this class. Function should take in a Request derived class
      and return a Message derived class, for sending on the wire
     :param request_class:
     :return:
     """
     key = request_class.__class__.__name__
     if key not in self._registry:
         raise ConfigurationException(
             "There is no message mapper associated with this key; we require a mapper"
         )
     else:
         return self._registry[key]
Esempio n. 5
0
    def lookup(self, request: Request) -> List[Callable[[], Handler]]:
        """
        Looks up the handler associated with a request - matches the key on the request to a registered handler
        :param request: The request we want to find a handler for
        :return:
        """
        key = request.__class__.__name__
        if key not in self._registry:
            if request.is_command():
                raise ConfigurationException(
                    "There is no handler registered for this request")
            elif request.is_event():
                return []  # type: Callable[[] Handler]

        return self._registry[key]
Esempio n. 6
0
    def lookup(self, request):
        """
        Looks up the handler associated with a request - matches the key on the request to a registered handler
        :param request: The request we want to find a handler for
        :return:
        """
        key = request.key
        if key not in self._registry:
            if request.is_command():
                raise ConfigurationException(
                    "There is no handler registered for this request")
            elif request.is_event():
                return []

        return self._registry[key]
Esempio n. 7
0
 def register(self, request, handler_factory):
     """
     Register the handler for the command
     :param request: The command or event to dispatch. It must implement getKey()
     :param handler_factory: A factory method to create the handler to dispatch to
     :return:
     """
     key = request.key
     is_command = request.is_command()
     is_event = request.is_event()
     is_present = key in self._registry
     if is_command and is_present:
         raise ConfigurationException(
             "A handler for this request has already been registered")
     elif is_event and is_present:
         self._registry[key].append(handler_factory)
     elif is_command or is_event:
         self._registry[key] = [handler_factory]