簡體   English   中英

使用 azure 服務總線和 spring 集成的多個隊列

[英]Multiple queues using azure service bus and spring integration

我有這種情況。 我在 Azure ServiceBus 中有很多隊列,下面的實現有效,但是它不靈活,因為需要為每個隊列復制。 我想從動態形成中改變,也許作為方法 send() 中的一個參數,隊列名稱和@ServiceActivator 和@MessagingGateway 的 OUTPUT_CHANNEL,有可能嗎?

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);
    }
}

com.azure.spring.integration.core.handler.DefaultMessageHandler支持來自消息頭的動態目標解析:

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

    return this.destination;
}

因此,您需要的是網關的send()方法上的@Header(name = AzureHeaders.NAME) String destination參數。 OUTPUT_CHANNEL的動態性質沒有任何理由:該DefaultMessageHandler只需要一個網關和一個服務激活器就足夠了。 您使用有效負載和目標目的地作為參數調用send()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM