java - JMS 中的客户端确认

标签 java jms

谁知道如何在 JMS 中进行客户端确认。

现在我正在使用 AUTO_ACKNOWLEDGMENT 来进行确认。

我确实知道如何将 AUTO_ACKNOWLEDGMENT 更改为 CLIENT_ACKNOWLEDGMENT

这是我的代码。

   public Connection openConnection() throws JMSException {
    ConnectionFactory connectionFactory = jmsConfiguration
            .connectionFactory();
    Connection conn = connectionFactory.createConnection();
    return conn;
}

/**
 * 
 * @param queueData
 * @param queueName
 * @throws JMSException
 * @throws DataCaptureException
 */
public void sendMessage(String queueData, String queueName)
        throws JMSException, DataCaptureException {
    Connection connection = null;
    try {

        connection = openConnection();
        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(queueName);
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        Message message = session.createTextMessage(queueData);
        producer.send(message);
        session.close();
    } catch (JMSException e) {
        throw new DataCaptureException(e.getMessage(), e.getCause());
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

/**
 * 
 * @param queueName
 * @return
 * @throws JMSException
 * @throws DataCaptureException
 */
public String receiveMessage(String queueName) throws JMSException,
        DataCaptureException {
    Connection connection = null;
    String queueMessage = null;
    try {

        connection = openConnection();
        connection.start();
        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(session
                .createQueue(queueName));
        Message message = consumer.receive(1000);
        consumer.close();
        if (null != message) {
            queueMessage = ((TextMessage) message).getText();
        }
        session.close();
    } catch (JMSException e) {
        throw new DataCaptureException(e.getMessage(), e.getCause());
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    return queueMessage;
}

谁能告诉我如何将其更改为 CLIENT_ACKNOWLEDGMENT 以及如何验证它。

最佳答案

您需要将 session 的创建更改为 CLIENT_ACKNOWLEDGE:

Session session = connection.createSession(false,
            Session.CLIENT_ACKNOWLEDGE);

那么您必须自己在代码中确认这些消息:

message.acknowledge();

一旦您愿意删除该消息,您就应该确认该消息。要验证它是否有效,就是查看该消息是否仍然存在于目的地上。

关于java - JMS 中的客户端确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366563/

相关文章:

multithreading - 具有多个生产 session 的单个 JMS 连接何时开始成为瓶颈?

java - Java与Android的交互

java - 使用 Apache Camel 处理不同优先级的 JMS 消息

java - 使用 apache httpcomponents 进行 Http 身份验证

java Restful Web 应用程序和 WADL

java - Fragment 和 ListFragment 不兼容

spring - 具有多个实例的微服务事件驱动设计

Java 11 : Execution default-compile of goal org. apache.maven.plugins :maven-compiler-plugin:3. 8.0 :compile failed.:NullPointerException

java - Android 智能 watch 采样频率

java - 如何利用 Apache Activemq 的连接池?