JMS中的客户端确认

时间:2014-08-18 15:11:01

标签: java jms

任何人都可以知道在JMS中进行客户端确认。

现在我正在使用AUTO_ACKNOWLEDGEMENT作为确认目的。

我知道如何将AUTO_ACKNOWLEDGEMENT更改为CLIENT_ACKNOWLEDGEMENT

这是我的代码。

   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_ACKNOWLEDGEMENT以及如何验证它。

1 个答案:

答案 0 :(得分:1)

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

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

然后你必须在代码中自己确认这些消息:

message.acknowledge();

如果您对删除该邮件感到满意,则应该确认该邮件。验证它的工作原理是查看目标上是否仍然存在该消息。