InventoryAsyncConfiguration.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.app;
- import org.ameba.amqp.RabbitTemplateConfigurable;
- import org.openwms.core.SpringProfiles;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.amqp.core.Binding;
- import org.springframework.amqp.core.BindingBuilder;
- import org.springframework.amqp.core.DirectExchange;
- import org.springframework.amqp.core.Queue;
- import org.springframework.amqp.core.QueueBuilder;
- import org.springframework.amqp.core.TopicExchange;
- import org.springframework.amqp.rabbit.annotation.EnableRabbit;
- import org.springframework.amqp.rabbit.connection.ConnectionFactory;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
- import org.springframework.amqp.support.converter.MessageConverter;
- import org.springframework.amqp.support.converter.SerializerMessageConverter;
- import org.springframework.beans.factory.ObjectProvider;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import org.springframework.context.annotation.Profile;
- import org.springframework.retry.backoff.ExponentialBackOffPolicy;
- import org.springframework.retry.support.RetryTemplate;
- import java.util.Objects;
- import static org.ameba.LoggingCategories.BOOT;
- /**
- * A InventoryAsyncConfiguration is activated when the service uses asynchronous communication to access other services.
- *
- * @author Heiko Scherrer
- */
- @Profile(SpringProfiles.ASYNCHRONOUS_PROFILE)
- @Configuration
- @RefreshScope
- @EnableRabbit
- class InventoryAsyncConfiguration {
- private static final Logger BOOT_LOGGER = LoggerFactory.getLogger(BOOT);
- private static final String X_DEAD_LETTER_EXCHANGE = "x-dead-letter-exchange";
- private static final String X_DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";
- private static final String POISON_MESSAGE = "poison-message";
- @ConditionalOnExpression("'${owms.inventory.serialization}'=='json'")
- @Bean
- MessageConverter messageConverter() {
- var messageConverter = new Jackson2JsonMessageConverter();
- BOOT_LOGGER.info("Using JSON serialization over AMQP");
- return messageConverter;
- }
- @ConditionalOnExpression("'${owms.inventory.serialization}'=='barray'")
- @Bean
- MessageConverter serializerMessageConverter() {
- var messageConverter = new SerializerMessageConverter();
- BOOT_LOGGER.info("Using byte array serialization over AMQP");
- return messageConverter;
- }
- @Primary
- @Bean(name = "amqpTemplate")
- public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,
- ObjectProvider<MessageConverter> messageConverter,
- @Autowired(required = false) RabbitTemplateConfigurable rabbitTemplateConfigurable) {
- var rabbitTemplate = new RabbitTemplate(connectionFactory);
- rabbitTemplate.setObservationEnabled(true);
- var backOffPolicy = new ExponentialBackOffPolicy();
- backOffPolicy.setMultiplier(2);
- backOffPolicy.setMaxInterval(15000);
- backOffPolicy.setInitialInterval(500);
- var retryTemplate = new RetryTemplate();
- retryTemplate.setBackOffPolicy(backOffPolicy);
- rabbitTemplate.setRetryTemplate(retryTemplate);
- rabbitTemplate.setMessageConverter(Objects.requireNonNull(messageConverter.getIfUnique()));
- if (rabbitTemplateConfigurable != null) {
- rabbitTemplateConfigurable.configure(rabbitTemplate);
- }
- return rabbitTemplate;
- }
- /*~ --------------- Exchanges --------------- */
- @RefreshScope
- @Bean
- TopicExchange inventoryExchange(@Value("${owms.events.inventory.exchange-name}") String exchangeName) {
- return new TopicExchange(exchangeName, true, false);
- }
- @RefreshScope
- @Bean
- TopicExchange tuExchange(@Value("${owms.events.common.tu.exchange-name}") String exchangeName) {
- return new TopicExchange(exchangeName, true, false);
- }
- @RefreshScope
- @Bean
- TopicExchange commonLgExchange(@Value("${owms.events.common.lg.exchange-name}") String exchangeName) {
- return new TopicExchange(exchangeName, true, false);
- }
- @RefreshScope
- @Bean
- TopicExchange commonTuCommandsExchange(@Value("${owms.commands.common.tu.exchange-name}") String exchangeName) {
- return new TopicExchange(exchangeName, true, false);
- }
- @RefreshScope
- @Bean
- DirectExchange inventoryCommandsExchange(@Value("${owms.commands.inventory.exchange-name}") String exchangeName) {
- return new DirectExchange(exchangeName, true, false);
- }
- @RefreshScope
- @Bean
- TopicExchange shippingExchange(@Value("${owms.events.shipping.exchange-name}") String exchangeName) {
- return new TopicExchange(exchangeName, true, false);
- }
- @RefreshScope
- @Bean
- TopicExchange movementExchange(@Value("${owms.events.movement.exchange-name}") String exchangeName) {
- return new TopicExchange(exchangeName, true, false);
- }
- /*~ ---------------- Queues ----------------- */
- @Bean
- Queue tuQueue(
- @Value("${owms.events.inventory.tu.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName
- ) {
- return QueueBuilder.durable(queueName)
- .withArgument(X_DEAD_LETTER_EXCHANGE, exchangeName)
- .withArgument(X_DEAD_LETTER_ROUTING_KEY, POISON_MESSAGE)
- .build();
- }
- @Bean
- Queue eventsLgQueue(
- @Value("${owms.events.inventory.lg.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName
- ) {
- return QueueBuilder.durable(queueName)
- .withArgument(X_DEAD_LETTER_EXCHANGE, exchangeName)
- .withArgument(X_DEAD_LETTER_ROUTING_KEY, POISON_MESSAGE)
- .build();
- }
- @Bean
- Queue tuCommandsQueue(
- @Value("${owms.commands.inventory.tu.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName
- ) {
- return QueueBuilder.durable(queueName)
- .withArgument(X_DEAD_LETTER_EXCHANGE, exchangeName)
- .withArgument(X_DEAD_LETTER_ROUTING_KEY, POISON_MESSAGE)
- .build();
- }
- @Bean
- Queue puCommandsQueue(
- @Value("${owms.commands.inventory.pu.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName
- ) {
- return QueueBuilder.durable(queueName)
- .withArgument(X_DEAD_LETTER_EXCHANGE, exchangeName)
- .withArgument(X_DEAD_LETTER_ROUTING_KEY, POISON_MESSAGE)
- .build();
- }
- @Bean
- Queue shippingSplitQueue(
- @Value("${owms.events.shipping.split.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName
- ) {
- return QueueBuilder.durable(queueName)
- .withArgument(X_DEAD_LETTER_EXCHANGE, exchangeName)
- .withArgument(X_DEAD_LETTER_ROUTING_KEY, POISON_MESSAGE)
- .build();
- }
- @Bean
- Queue movementQueue(
- @Value("${owms.events.movement.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName
- ) {
- return QueueBuilder.durable(queueName)
- .withArgument(X_DEAD_LETTER_EXCHANGE, exchangeName)
- .withArgument(X_DEAD_LETTER_ROUTING_KEY, POISON_MESSAGE)
- .build();
- }
- /*~ --------------- Bindings ---------------- */
- @RefreshScope
- @Bean
- Binding tuBinding(
- @Qualifier("tuExchange") TopicExchange tuExchange,
- @Qualifier("tuQueue") Queue tuQueue,
- @Value("${owms.events.inventory.tu.routing-key}") String routingKey
- ) {
- return BindingBuilder
- .bind(tuQueue)
- .to(tuExchange)
- .with(routingKey);
- }
- @RefreshScope
- @Bean
- Binding lgBinding(
- @Qualifier("commonLgExchange") TopicExchange commonLgExchange,
- @Qualifier("eventsLgQueue") Queue eventsLgQueue,
- @Value("${owms.events.inventory.lg.routing-key}") String routingKey
- ) {
- return BindingBuilder
- .bind(eventsLgQueue)
- .to(commonLgExchange)
- .with(routingKey);
- }
- @RefreshScope
- @Bean
- Binding tuCommandsBinding(
- @Qualifier("commonTuCommandsExchange") TopicExchange commonTuCommandsExchange,
- @Qualifier("tuCommandsQueue") Queue tuCommandsQueue,
- @Value("${owms.commands.inventory.tu.routing-key}") String routingKey
- ) {
- return BindingBuilder
- .bind(tuCommandsQueue)
- .to(commonTuCommandsExchange)
- .with(routingKey);
- }
- @RefreshScope
- @Bean
- Binding puCommandsBinding(
- @Qualifier("inventoryCommandsExchange") DirectExchange inventoryCommandsExchange,
- @Qualifier("puCommandsQueue") Queue puCommandsQueue,
- @Value("${owms.commands.inventory.pu.routing-key}") String routingKey
- ) {
- return BindingBuilder
- .bind(puCommandsQueue)
- .to(inventoryCommandsExchange)
- .with(routingKey);
- }
- @RefreshScope
- @Bean
- Binding splitBinding(
- @Qualifier("shippingExchange") TopicExchange shippingExchange,
- @Qualifier("shippingSplitQueue") Queue shippingSplitQueue,
- @Value("${owms.events.shipping.split.routing-key}") String routingKey
- ) {
- return BindingBuilder
- .bind(shippingSplitQueue)
- .to(shippingExchange)
- .with(routingKey);
- }
- @RefreshScope
- @Bean
- Binding movementBinding(
- @Qualifier("movementExchange") TopicExchange movementExchange,
- @Qualifier("movementQueue") Queue movementQueue,
- @Value("${owms.events.movement.routing-key}") String routingKey
- ) {
- return BindingBuilder
- .bind(movementQueue)
- .to(movementExchange)
- .with(routingKey);
- }
- /* Dead Letter */
- @RefreshScope
- @Bean
- DirectExchange dlExchange(@Value("${owms.dead-letter.exchange-name}") String exchangeName) {
- return new DirectExchange(exchangeName);
- }
- @RefreshScope
- @Bean
- Queue dlQueue(@Value("${owms.dead-letter.queue-name}") String queueName) {
- return QueueBuilder.durable(queueName).build();
- }
- @RefreshScope
- @Bean
- Binding dlBinding(
- @Value("${owms.dead-letter.queue-name}") String queueName,
- @Value("${owms.dead-letter.exchange-name}") String exchangeName) {
- return BindingBuilder
- .bind(dlQueue(queueName))
- .to(dlExchange(exchangeName))
- .with(POISON_MESSAGE);
- }
- }