当订阅处于接收和删除模式时,我们能否以编程方式发送确认 - JMS 1.0

问题描述 投票:0回答:1

我正在尝试使用azure服务总线中的JMS v1.0以编程方式发送确认(Java 17),Spring Boot 2.5.0的主题订阅处于接收和删除模式。 对于上述情况,我收到以下错误。

“连接失败:10 次尝试后继续重试。”

我尝试过的代码如下。

ConnectionFactory factory   = new ServiceBusJmsConnectionFactory(<CONNECTION_STRING>, new ServiceBusJmsConnectionFactory());
Connection connection       = factory.createConnection();
connection.setClientId(<CLIENT_ID>);
connection.setExceptionListener(new MyExceptionHandler());
connection.start();
Session session = connection.createSession(true, session.CLIENT_ACKNOWLEDGE);

请帮忙。

java azure azureservicebus azure-servicebus-topics
1个回答
0
投票

我尝试使用下面的 Spring Boot 应用程序向使用 JMS 在 RECEIVE_AND_DELETE 模式下配置的 Azure 服务总线主题订阅发送确认。

代码:

ServiceBusService.java:

import com.microsoft.azure.servicebus.jms.ServiceBusJmsConnectionFactory;
import com.microsoft.azure.servicebus.jms.ServiceBusJmsConnectionFactorySettings;
import javax.jms.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

@Service
public class ServiceBusService implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(ServiceBusService.class);
    @Value("${azure.servicebus.connection-string}")
    private String connectionString;
    @Value("${azure.servicebus.topic-name}")
    private String topicName;
    @Value("${azure.servicebus.subscription-name}")
    private String subscriptionName;

    private Connection connection;
    private Session session;
    private MessageConsumer consumer;

    @Override
    public void run(String... args) throws Exception {
        initializeConnection();
        sendMessage("Hello, Kamali!");
        receiveMessages();
        closeResources();
    }

    private void initializeConnection() throws JMSException {
        ServiceBusJmsConnectionFactory factory = new ServiceBusJmsConnectionFactory(connectionString, new ServiceBusJmsConnectionFactorySettings());
        connection = factory.createConnection();
        connection.setClientID("<clientID>");
        session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        connection.start();
    }

    public void sendMessage(String messageContent) {
        try {
            Destination destination = session.createTopic(topicName);
            MessageProducer producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage(messageContent);
            producer.send(message);
            logger.info("Sent a single message to the topic: {}", topicName);
            producer.close();
        } catch (JMSException e) {
            logger.error("Error sending message: {}", e.getMessage());
        }
    }

    public void receiveMessages() {
        try {
            Destination destination = session.createTopic(topicName);
            consumer = session.createDurableSubscriber((Topic) destination, subscriptionName);
            consumer.setMessageListener(message -> {
                try {
                    if (message instanceof TextMessage) {
                        String messageBody = ((TextMessage) message).getText();
                        logger.info("Received: {}", messageBody);
                        message.acknowledge();
                    }
                } catch (JMSException e) {
                    logger.error("Error processing message: {}", e.getMessage());
                }
            });
            logger.info("Waiting for messages... Press enter to stop.");
            System.in.read();
        } catch (Exception e) {
            logger.error("Error receiving messages: {}", e.getMessage());
        }
    }
    private void closeResources() {
        try {
            if (consumer != null) consumer.close();
            if (session != null) session.close();
            if (connection != null) connection.close();
        } catch (JMSException e) {
            logger.error("Error closing resources: {}", e.getMessage());
        }
    }
}

应用程序属性:

azure.servicebus.connection-string=<connecString>
azure.servicebus.topic-name=<TopicName>
azure.servicebus.subscription-name=<SubName>
logging.level.org.apache.qpid.jms=DEBUG

pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>javax.jms-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-servicebus-jms</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

输出:

以下 Spring Boot 应用程序成功运行并向 Azure 服务总线主题发送消息确认。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.