Home > front end >  Multiple queues using azure service bus and spring integration
Multiple queues using azure service bus and spring integration

Time:08-12

I have this scenario. I have many queues in Azure ServiceBus, the implementation below works, however it is not flexible since would need to replicate for each queue. I would like to change from dynamically forming, maybe as a parameter in the method send() the queue name and the OUTPUT_CHANNEL for @ServiceActivator and @MessagingGateway, It is possible?

import com.azure.spring.cloud.service.servicebus.properties.ServiceBusEntityType;
import com.azure.spring.integration.core.handler.DefaultMessageHandler;
import com.azure.spring.messaging.servicebus.core.ServiceBusTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageHandler;
import org.springframework.stereotype.Component;

@Component
public class TestIntegration {

    private static final String OUTPUT_CHANNEL = "output";
    private static final String QUEUE_NAME = "myQueue";

    @Autowired
    private QueueOutboundGateway messagingGateway;

    public void send(String message) {
        this.messagingGateway.send(message);
    }

    @Bean
    @ServiceActivator(inputChannel = OUTPUT_CHANNEL)
    public MessageHandler queueMessageSender(ServiceBusTemplate serviceBusTemplate) {
        serviceBusTemplate.setDefaultEntityType(ServiceBusEntityType.QUEUE);
        return new DefaultMessageHandler(QUEUE_NAME, serviceBusTemplate);
    }

    @MessagingGateway(defaultRequestChannel = OUTPUT_CHANNEL)
    public interface QueueOutboundGateway {
        void send(String text);
    }
}

CodePudding user response:

The com.azure.spring.integration.core.handler.DefaultMessageHandler supports a dynamic destination resolution from message headers:

private String toDestination(Message<?> message) {
    if (message.getHeaders().containsKey(AzureHeaders.NAME)) {
        return message.getHeaders().get(AzureHeaders.NAME, String.class);
    }

    return this.destination;
}

So, what you need is a @Header(name = AzureHeaders.NAME) String destination argument on your gateway's send() method. There is no reason in the dynamic nature for the OUTPUT_CHANNEL: only one gateway and one service activator for that DefaultMessageHandler is enough. You call send() with payload and target destination as params.

  • Related