|
1 | 1 | package com.microsoft.migration.assets.config; |
2 | 2 |
|
3 | | -import org.springframework.amqp.core.AcknowledgeMode; |
4 | | -import org.springframework.amqp.core.Binding; |
5 | | -import org.springframework.amqp.core.BindingBuilder; |
6 | | -import org.springframework.amqp.core.DirectExchange; |
7 | | -import org.springframework.amqp.core.Queue; |
8 | | -import org.springframework.amqp.core.QueueBuilder; |
9 | | -import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; |
10 | | -import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
11 | | -import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; |
12 | | -import org.springframework.amqp.support.converter.MessageConverter; |
13 | | -import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer; |
| 3 | +import com.azure.core.credential.TokenCredential; |
| 4 | +import com.azure.core.exception.ResourceExistsException; |
| 5 | +import com.azure.core.exception.ResourceNotFoundException; |
| 6 | +import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient; |
| 7 | +import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClientBuilder; |
| 8 | +import com.azure.messaging.servicebus.administration.models.CreateQueueOptions; |
| 9 | +import com.azure.messaging.servicebus.administration.models.QueueProperties; |
| 10 | +import com.azure.spring.cloud.autoconfigure.implementation.servicebus.properties.AzureServiceBusProperties; |
| 11 | +import com.azure.spring.messaging.ConsumerIdentifier; |
| 12 | +import com.azure.spring.messaging.PropertiesSupplier; |
| 13 | +import com.azure.spring.messaging.servicebus.core.properties.ProcessorProperties; |
14 | 14 | import org.springframework.context.annotation.Bean; |
15 | 15 | import org.springframework.context.annotation.Configuration; |
16 | 16 |
|
| 17 | +import java.time.Duration; |
| 18 | + |
17 | 19 | @Configuration |
18 | 20 | public class RabbitConfig { |
19 | 21 | public static final String IMAGE_PROCESSING_QUEUE = "image-processing"; |
20 | | - |
21 | | - // Dead letter queue configuration for the retry mechanism |
22 | | - public static final String RETRY_EXCHANGE = "image-processing.retry"; |
23 | | - public static final String RETRY_QUEUE = "image-processing.retry"; |
24 | | - public static final String RETRY_ROUTING_KEY = "retry"; |
25 | | - public static final int RETRY_DELAY_MS = 60000; // 1 minute delay |
26 | | - |
27 | | - @Bean |
28 | | - public Queue imageProcessingQueue() { |
29 | | - return QueueBuilder.durable(IMAGE_PROCESSING_QUEUE |
30 | | -) |
31 | | - .withArgument("x-dead-letter-exchange", RETRY_EXCHANGE) |
32 | | - .withArgument("x-dead-letter-routing-key", RETRY_ROUTING_KEY) |
33 | | - .build(); |
34 | | - } |
35 | | - |
36 | | - @Bean |
37 | | - public Queue retryQueue() { |
38 | | - return QueueBuilder.durable(RETRY_QUEUE) |
39 | | - .withArgument("x-dead-letter-exchange", "") |
40 | | - .withArgument("x-dead-letter-routing-key", IMAGE_PROCESSING_QUEUE |
41 | | - ) |
42 | | - .withArgument("x-message-ttl", RETRY_DELAY_MS) |
43 | | - .build(); |
44 | | - } |
| 22 | + public static final String RETRY_QUEUE = "retry-queue"; |
| 23 | + public static final Duration RETRY_QUEUE_TTL = Duration.ofMinutes(1); |
45 | 24 |
|
46 | 25 | @Bean |
47 | | - public DirectExchange retryExchange() { |
48 | | - return new DirectExchange(RETRY_EXCHANGE); |
| 26 | + public ServiceBusAdministrationClient adminClient(AzureServiceBusProperties properties, TokenCredential credential) { |
| 27 | + return new ServiceBusAdministrationClientBuilder() |
| 28 | + .credential(properties.getFullyQualifiedNamespace(), credential) |
| 29 | + .buildClient(); |
49 | 30 | } |
50 | 31 |
|
51 | 32 | @Bean |
52 | | - public Binding retryBinding() { |
53 | | - return BindingBuilder |
54 | | - .bind(retryQueue()) |
55 | | - .to(retryExchange()) |
56 | | - .with(RETRY_ROUTING_KEY); |
| 33 | + public QueueProperties retryQueue(ServiceBusAdministrationClient adminClient) { |
| 34 | + try { |
| 35 | + return adminClient.getQueue(RETRY_QUEUE); |
| 36 | + } catch (ResourceNotFoundException e) { |
| 37 | + try { |
| 38 | + CreateQueueOptions options = new CreateQueueOptions() |
| 39 | + .setDefaultMessageTimeToLive(RETRY_QUEUE_TTL) |
| 40 | + .setDeadLetteringOnMessageExpiration(true); |
| 41 | + return adminClient.createQueue(RETRY_QUEUE, options); |
| 42 | + } catch (ResourceExistsException ex) { |
| 43 | + // Queue was created by another instance in the meantime |
| 44 | + return adminClient.getQueue(RETRY_QUEUE); |
| 45 | + } |
| 46 | + } |
57 | 47 | } |
58 | 48 |
|
59 | 49 | @Bean |
60 | | - public MessageConverter jsonMessageConverter() { |
61 | | - return new Jackson2JsonMessageConverter(); |
| 50 | + public QueueProperties imageProcessingQueue(ServiceBusAdministrationClient adminClient, QueueProperties retryQueue) { |
| 51 | + QueueProperties queue; |
| 52 | + try { |
| 53 | + queue = adminClient.getQueue(IMAGE_PROCESSING_QUEUE); |
| 54 | + } catch (ResourceNotFoundException e) { |
| 55 | + try { |
| 56 | + CreateQueueOptions options = new CreateQueueOptions() |
| 57 | + .setForwardDeadLetteredMessagesTo(RETRY_QUEUE); |
| 58 | + queue = adminClient.createQueue(IMAGE_PROCESSING_QUEUE, options); |
| 59 | + } catch (ResourceExistsException ex) { |
| 60 | + // Queue was created by another instance in the meantime |
| 61 | + queue = adminClient.getQueue(IMAGE_PROCESSING_QUEUE); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Configure retry queue's DLQ forwarding now that image processing queue exists |
| 66 | + try { |
| 67 | + retryQueue.setForwardDeadLetteredMessagesTo(IMAGE_PROCESSING_QUEUE); |
| 68 | + adminClient.updateQueue(retryQueue); |
| 69 | + } catch (Exception ex) { |
| 70 | + // Ignore update errors since basic functionality will still work |
| 71 | + } |
| 72 | + |
| 73 | + return queue; |
62 | 74 | } |
63 | 75 |
|
64 | 76 | @Bean |
65 | | - public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory( |
66 | | - ConnectionFactory connectionFactory, |
67 | | - SimpleRabbitListenerContainerFactoryConfigurer configurer) { |
68 | | - SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); |
69 | | - configurer.configure(factory, connectionFactory); |
70 | | - factory.setMessageConverter(jsonMessageConverter()); |
71 | | - factory.setAcknowledgeMode(AcknowledgeMode.MANUAL); |
72 | | - factory.setDefaultRequeueRejected(false); |
73 | | - return factory; |
| 77 | + public PropertiesSupplier<ConsumerIdentifier, ProcessorProperties> propertiesSupplier() { |
| 78 | + return identifier -> { |
| 79 | + ProcessorProperties processorProperties = new ProcessorProperties(); |
| 80 | + processorProperties.setAutoComplete(false); |
| 81 | + return processorProperties; |
| 82 | + }; |
74 | 83 | } |
75 | 84 | } |
0 commit comments