TransportUnitEventPropagator.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.common.transport.events;

  17. import org.ameba.annotation.Measured;
  18. import org.openwms.common.transport.TransportUnit;
  19. import org.openwms.common.transport.TransportUnitMapper;
  20. import org.openwms.core.SpringProfiles;
  21. import org.springframework.amqp.core.AmqpTemplate;
  22. import org.springframework.beans.factory.annotation.Value;
  23. import org.springframework.cloud.context.config.annotation.RefreshScope;
  24. import org.springframework.context.annotation.Profile;
  25. import org.springframework.stereotype.Component;
  26. import org.springframework.transaction.annotation.Propagation;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import org.springframework.transaction.event.TransactionalEventListener;

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

  30. /**
  31.  * A TransportUnitEventPropagator.
  32.  *
  33.  * @author Heiko Scherrer
  34.  */
  35. @Profile(SpringProfiles.ASYNCHRONOUS_PROFILE)
  36. @RefreshScope
  37. @Component
  38. class TransportUnitEventPropagator {

  39.     private final AmqpTemplate amqpTemplate;
  40.     private final String exchangeName;
  41.     private final TransportUnitMapper mapper;

  42.     TransportUnitEventPropagator(
  43.             AmqpTemplate amqpTemplate,
  44.             @Value("${owms.events.common.tu.exchange-name}") String exchangeName,
  45.             TransportUnitMapper mapper) {
  46.         this.amqpTemplate = amqpTemplate;
  47.         this.exchangeName = exchangeName;
  48.         this.mapper = mapper;
  49.     }

  50.     @Measured
  51.     @TransactionalEventListener(fallbackExecution = true)
  52.     @Transactional(propagation = Propagation.REQUIRES_NEW)
  53.     public void onEvent(TransportUnitEvent event) {
  54.         switch (event.getType()) {
  55.             case CREATED -> amqpTemplate.convertAndSend(exchangeName, "tu.event.created", mapper.convertToMO((TransportUnit) event.getSource()));
  56.             case CHANGED -> amqpTemplate.convertAndSend(exchangeName, "tu.event.changed", mapper.convertToMO((TransportUnit) event.getSource()));
  57.             case DELETED -> amqpTemplate.convertAndSend(exchangeName, "tu.event.deleted", mapper.convertToMO((TransportUnit) event.getSource()));
  58.             case STATE_CHANGE -> amqpTemplate.convertAndSend(exchangeName, "tu.event.state-changed", mapper.convertToMO((TransportUnit) event.getSource()));
  59.             case MOVED -> amqpTemplate.convertAndSend(exchangeName, "tu.event.moved." + event.getActualLocation().getLocationId(), mapper.convertToMO((TransportUnit) event.getSource()));
  60.             default -> throw new UnsupportedOperationException(format("TransportUnitEvent [%s] not supported", event.getType()));
  61.         }
  62.     }
  63. }