SplitEventPropagator.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.shipping.splits;

import org.ameba.annotation.Measured;
import org.openwms.core.SpringProfiles;
import org.openwms.wms.shipping.CycleAvoidingMappingContext;
import org.openwms.wms.shipping.impl.ShippingMapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * A SplitEventPropagator exposes events about {@code ShippimgOrderPositionSplit}s to foreign parties.
 *
 * @author Heiko Scherrer
 */
@Profile((SpringProfiles.ASYNCHRONOUS_PROFILE))
@Component
class SplitEventPropagator {

    private final AmqpTemplate amqpTemplate;
    private final String exchangeName;
    private final ShippingMapper mapper;

    SplitEventPropagator(AmqpTemplate amqpTemplate, @Value("${owms.shipping.exchange-name}") String exchangeName, ShippingMapper mapper) {
        this.amqpTemplate = amqpTemplate;
        this.exchangeName = exchangeName;
        this.mapper = mapper;
    }

    @Measured
    @TransactionalEventListener(fallbackExecution = true)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void onEvent(SplitCreated event) {
        amqpTemplate.convertAndSend(exchangeName, "split.event.created", mapper.convertTo(event.getSource(), new CycleAvoidingMappingContext()));
    }
}