ExternalStartListener.java

  1. /*
  2.  * Copyright 2005-2025 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.openwms.tms.routing.routes;

  17. import org.ameba.annotation.Measured;
  18. import org.openwms.core.SpringProfiles;
  19. import org.openwms.tms.api.MessageVO;
  20. import org.openwms.tms.api.TransportOrderApi;
  21. import org.openwms.tms.api.requests.state.StateChangeRequest;
  22. import org.openwms.tms.api.requests.state.StateChangeResponse;
  23. import org.openwms.tms.routing.RouteSearchAlgorithm;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.amqp.core.AmqpTemplate;
  27. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.context.annotation.Profile;
  30. import org.springframework.stereotype.Component;

  31. import static java.lang.String.format;

  32. /**
  33.  * A ExternalStartListener.
  34.  *
  35.  * @author Heiko Scherrer
  36.  */
  37. @Profile({SpringProfiles.ASYNCHRONOUS_PROFILE})
  38. @Component
  39. class ExternalStartListener {

  40.     private static final Logger LOGGER = LoggerFactory.getLogger(ExternalStartListener.class);
  41.     public static final String RESPONSE_STATE_CHANGE = "response.state.change";
  42.     private final TransportOrderApi transportOrderApi;
  43.     private final RouteSearchAlgorithm routeSearch;
  44.     private final AmqpTemplate amqpTemplate;
  45.     private final String exchangeName;

  46.     ExternalStartListener(TransportOrderApi transportOrderApi, RouteSearchAlgorithm routeSearch, AmqpTemplate amqpTemplate,
  47.             @Value("${owms.requests.routing.to.exchange-name}") String exchangeName) {
  48.         this.transportOrderApi = transportOrderApi;
  49.         this.routeSearch = routeSearch;
  50.         this.amqpTemplate = amqpTemplate;
  51.         this.exchangeName = exchangeName;
  52.     }

  53.     @Measured
  54.     @RabbitListener(queues = "${owms.requests.routing.to.queue-name}")
  55.     public void onRequest(StateChangeRequest request) {
  56.         if ("STARTED".equals(request.getRequestedState())) {
  57.             try {
  58.                 var vo = transportOrderApi.findByPKey(request.getTransportOrderPkey());
  59.                 var route = routeSearch.findBy(vo.getSourceLocation(), vo.getTargetLocation(), vo.getTargetLocationGroup());
  60.                 LOGGER.debug("TransportOrder to start has a Route [{}] ", route.getRouteId());
  61.                 amqpTemplate.convertAndSend(exchangeName, RESPONSE_STATE_CHANGE, new StateChangeResponse(request, "STARTED", null));
  62.             } catch (NoRouteException ex) {

  63.                 // not that bad we know no Route exists...
  64.                 var msg = format("A TransportOrder was requested to start that has no Route. PKey: [%s]", request.getTransportOrderPkey());
  65.                 LOGGER.warn(msg, msg);
  66.                 amqpTemplate.convertAndSend(exchangeName, RESPONSE_STATE_CHANGE, new StateChangeResponse(request, "XX",
  67.                         MessageVO.newBuilder()
  68.                                 .messageText(msg)
  69.                                 .build()
  70.                 ));
  71.             } catch (Exception ex) {
  72.                 var msg = format("Generic exception. Route for TransportOrder [%s] could not be determined", request.getTransportOrderPkey());
  73.                 LOGGER.warn(msg, msg);
  74.                 amqpTemplate.convertAndSend(exchangeName, RESPONSE_STATE_CHANGE, new StateChangeResponse(request, "XX",
  75.                         MessageVO.newBuilder()
  76.                                 .messageText(msg)
  77.                                 .build()
  78.                 ));
  79.             }
  80.         }
  81.     }
  82. }