簡體   English   中英

如何避免 Spring Cloud Dataflow Source 應用程序不斷返回相同的消息

[英]How to avoid Spring Cloud Dataflow Source application to keep returning same message

我正在訂閱 rabbitMQ 服務器上的隊列並使用其上的消息來檢索正文和一個特定的 header。這是在 Spring 雲數據流源應用程序上完成的,問題是當它啟動時它會繼續執行並返回最后一條消息。 我需要添加什么才能使應用程序僅在新消息到達正在偵聽的隊列時執行?

這是我的供應商代碼:

@Log4j2
@EnableConfigurationProperties({ RabbitMQProperties.class })
@Configuration
public class ReceiveMessageConfiguration {
    private final static String QUEUE_NAME = "UniversalId";
    String payload;
    Connection connection;
    Channel channel;


    @Bean
    public Supplier<String> receiverMessage(RabbitMQProperties rabbitMQProperties) throws Exception {
        return () -> {
            try {
                ConnectionFactory factory = new ConnectionFactory();
                factory.setHost("localhost");
                factory.setRequestedHeartbeat(0);

                connection = factory.newConnection();
                channel = connection.createChannel();

                channel.queueDeclare(QUEUE_NAME, true, false, false, null);

                DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                    String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
//              Get headers from properties
                    AMQP.BasicProperties properties = delivery.getProperties();
                    Map<String, Object> headers = properties.getHeaders();

//              Extract and print payload and header
                    for (Map.Entry<String, Object> header : headers.entrySet()) {
                        if (header.getKey().toString().equals("UniversalId")) {
//                      log.info("ID nedeed: " + header.getValue());
                        }
                    }
                    payload = message;
                };
                channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
                });
                log.info(payload);
                if (payload != null) {
                    return payload;
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                try {
                    channel.close();
                    connection.close();
                } catch (IOException | TimeoutException e) {
                    e.printStackTrace();
                }
            }
            return new String("No message");

        };
    }
}

我的屬性文件是這樣的:

spring.cloud.stream.function.bindings.receiverMessage-in-0=input
spring.cloud.stream.function.bindings.receiverMessage-out-0=output
spring.cloud.stream.bindings.input.destination=receiverMessage-input
spring.cloud.stream.bindings.output.destination=receiverMessage-output

更新:這是控制台消息的示例:

2022-12-21T16:25:19.725-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[           main][0;39m [36mm.c.n.r.RabbitMqCustomSourceApplication [0;39m [2m:[0;39m Started RabbitMqCustomSourceApplication in 7.888 seconds (process running for 9.482)
[2m2022-12-21T16:25:20.432-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:21.478-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:22.523-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:23.568-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:24.604-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:25.913-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:27.056-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:28.124-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m null
[2m2022-12-21T16:25:29.167-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m [
    {
        "MailRequest": {
            "action": "fourth",
            "mails": []
        }
    }
]
[2m2022-12-21T16:25:30.233-06:00[0;39m [32m INFO[0;39m [35m21600[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mm.c.n.r.s.ReceiveMessageConfiguration   [0;39m [2m:[0;39m [
    {
        "MailRequest": {
            "action": "fourth",
            "mails": []
        }
    }
]

我已經嘗試將 RabbitMQ API 更改為 AMQP API,但行為是相同的。

乳暈,

因為這是直接使用 Rabbit/AMQP 的自定義函數,所以 SCDF 沒有影響。 因此,這更像是一個 Rabbit/AMQP 問題。

此外,您可能想看看RabbitSource提供的應用程序,它可以作為替代品。

首先,我要感謝@onobc 的指導。 為了結束這個問題,我使用帶有有效負載的轉換處理器默認應用程序和 SPeL 表達式來提取特定標頭,如下所示: - -expression="new String(headers.get('LoggerID') + payload) 。其中 LoggerID 是您要提取的 header 的名稱。

暫無
暫無

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

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