LocationMessageListener.java

/*
 * Copyright 2005-2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openwms.wms.location.events;

import org.ameba.annotation.Measured;
import org.openwms.common.location.api.commands.RevokeLocationRemoveCommand;
import org.openwms.common.location.api.messages.LocationMO;
import org.openwms.core.SpringProfiles;
import org.openwms.wms.location.LocationSynchronizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Profile;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * A LocationMessageListener.
 *
 * @author Heiko Scherrer
 */
@Profile(SpringProfiles.ASYNCHRONOUS_PROFILE)
@Component
@RabbitListener(queues = "${owms.events.inventory.lg.queue-name}")
class LocationMessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(LocationMessageListener.class);
    private final LocationSynchronizer synchronizer;

    LocationMessageListener(LocationSynchronizer synchronizer) {
        this.synchronizer = synchronizer;
    }

    @Measured
    @RabbitHandler
    void handle(@Payload LocationMO mo, @Header("amqp_receivedRoutingKey") String routingKey) throws InterruptedException {
        try {
            switch (routingKey) {
                case "loc.event.created" -> {
                    LOGGER.info("Location has been created on golden source [{}]", mo);
                    synchronizer.create(mo);
                }
                case "loc.event.deleted" -> {
                    LOGGER.info("Location has been deleted on golden source [{}]", mo);
                    synchronizer.delete(mo.pKey());
                }
                case "loc.event.state-changed" -> {
                    TimeUnit.MILLISECONDS.sleep(1000);
                    LOGGER.debug("Requested to change the state of a Location with foreign PID [{}] to incoming=[{}], outgoing=[{}], plcState=[{}]",
                            mo.pKey(), mo.incomingActive(), mo.outgoingActive(), mo.plcState());
                    synchronizer.changeState(mo.pKey(), mo.incomingActive(), mo.outgoingActive(), mo.plcState());
                }
                case null, default -> LOGGER.debug("RoutingKey not considered [{}]", routingKey);
            }
        } catch (Exception e) {
            LOGGER.error("Processing command rejected [{}]", mo);
            throw new AmqpRejectAndDontRequeueException(e.getMessage(), e);
        }
    }

    @Measured
    @RabbitHandler
    void handle(@Payload RevokeLocationRemoveCommand command, @Header("amqp_receivedRoutingKey") String routingKey) {
        try {
            if ("loc.event.deletion-rollback".equals(routingKey)) {
                LOGGER.info("Rollback any previous set deletion marker on the Location [{}]", command.getpKey());
                synchronizer.rollbackDeletionMark(command.getpKey());
            } else {
                LOGGER.warn("RoutingKey not considered [{}]", routingKey);
            }
        } catch (Exception e) {
            LOGGER.error("Processing command rejected [{}]", command);
            throw new AmqpRejectAndDontRequeueException(e.getMessage(), e);
        }
    }
}