SQS AWS - message.acknowledge - 具有确认模式的异常:UNORDERED_ACKNOWLEDGE

时间:2016-07-06 05:46:19

标签: amazon-web-services jms amazon-sqs

我正在运行SyncMessageReceiverUnorderedAcknowledge.java程序,完全如下所示:http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/jmsclient.html#jmsclient-ackmode

public class SyncMessageReceiverUnorderedAcknowledge {

// Visibility time-out for the queue. It must match to the one set for the queue for this example to work.
private static final long TIME_OUT_SECONDS = 1;

public static void main(String args[]) throws JMSException, InterruptedException {
    // Create the configuration for the example
    ExampleConfiguration config = ExampleConfiguration.parseConfig("SyncMessageReceiverUnorderedAcknowledge", args);

    // Setup logging for the example
    ExampleCommon.setupLogging();

    // Create the connection factory based on the config
    SQSConnectionFactory connectionFactory =
            SQSConnectionFactory.builder()
                    .withRegion(config.getRegion())
                    .withAWSCredentialsProvider(config.getCredentialsProvider())
                    .build();

    // Create the connection
    SQSConnection connection = connectionFactory.createConnection();

    // Create the queue if needed
    ExampleCommon.ensureQueueExists(connection, config.getQueueName());

    // Create the session  with unordered acknowledge mode
    Session session = connection.createSession(false, **SQSSession.UNORDERED_ACKNOWLEDGE**);

    // Create the producer and consume
    MessageProducer producer = session.createProducer(session.createQueue(config.getQueueName()));
    MessageConsumer consumer = session.createConsumer(session.createQueue(config.getQueueName()));

    // Open the connection
    connection.start();

    // Send two text messages
    sendMessage(producer, session, "Message 1");
    sendMessage(producer, session, "Message 2");

    // Receive a message and don't acknowledge it
    receiveMessage(consumer, false);

    // Receive another message and acknowledge it
    receiveMessage(consumer, true);

    // Wait for the visibility time out, so that unacknowledged messages reappear in the queue
    System.out.println("Waiting for visibility timeout...");
    Thread.sleep(TimeUnit.SECONDS.toMillis(TIME_OUT_SECONDS));

    // Attempt to receive another message and acknowledge it. This will result in receiving the first message since
    // we have acknowledged only the second message. In the UNORDERED_ACKNOWLEDGE mode, all the messages must
    // be explicitly acknowledged.
    receiveMessage(consumer, true);

    // Close the connection. This will close the session automatically
    connection.close();
    System.out.println("Connection closed.");
}

/**
 * Sends a message through the producer.
 *
 * @param producer Message producer
 * @param session Session
 * @param messageText Text for the message to be sent
 * @throws JMSException
 */
private static void sendMessage(MessageProducer producer, Session session, String messageText) throws JMSException {
    // Create a text message and send it
    producer.send(session.createTextMessage(messageText));
}

/**
 * Receives a message through the consumer synchronously with the default timeout (TIME_OUT_SECONDS).
 * If a message is received, the message is printed. If no message is received, "Queue is empty!" is
 * printed.
 *
 * @param consumer Message consumer
 * @param acknowledge If true and a message is received, the received message is acknowledged.
 * @throws JMSException
 */
private static void receiveMessage(MessageConsumer consumer, boolean acknowledge) throws JMSException {
    // Receive a message
    Message message = consumer.receive(TimeUnit.SECONDS.toMillis(TIME_OUT_SECONDS));

    if (message == null) {
        System.out.println("Queue is empty!");
    } else {
        // Since this queue has only text messages, cast the message object and print the text
        System.out.println("Received: " + ((TextMessage) message).getText());

        // Acknowledge the message if asked
        if (acknowledge) **message.acknowledge();**
    }
}

}

到达以下代码时:

// Create the session  with unordered acknowledge mode
Session session = connection.createSession(false,SQSSession.UNORDERED_ACKNOWLEDGE);
// Acknowledge the message if asked
if (acknowledge) message.acknowledge();

我得到以下异常:

  

线程“main”中的异常 java.lang.NoSuchMethodError:com.amazonaws.services.sqs.AmazonSQS.deleteMessage(Lcom / amazonaws / services / sqs / model / DeleteMessageRequest;)V   在com.amazon.sqs.javamessaging.AmazonSQSMessagingClientWrapper.deleteMessage(AmazonSQSMessagingClientWrapper.java:127)   在com.amazon.sqs.javamessaging.acknowledge.UnorderedAcknowledger.acknowledge(UnorderedAcknowledger.java:42)   在com.amazon.sqs.javamessaging.message.SQSMessage.acknowledge(SQSMessage.java:883)   at sample.sqs.SyncMessageReceiverUnorderedAcknowledge.receiveMessage(SyncMessageReceiverUnorderedAcknowledge.java:116)   at sample.sqs.SyncMessageReceiverUnorderedAcknowledge.main(SyncMessageReceiverUnorderedAcknowledge.java:67)

我正在运行以下gradle依赖项:

  

编译( “com.amazonaws:AWS-java的SDK-SQS:13年11月1日”)

     

编译( “com.amazonaws:亚马逊SQS-java的消息-lib的:1.0.0”)

我调试了代码,所有的aws amazon java类看起来都很完美。

此外,我创建了一个新程序,它将运行独立的deleteMessage。 使用deleteMessage(DeleteMessageRequest deleteMessageRequest) - 我得到相同的异常

但是使用DeleteMessageBatchResult deleteMessageBatch(DeleteMessageBatchRequest deleteMessageBatchRequest) - 它在同一个类中 - 它可以工作!

我清理了gradle缓存文件夹,再次下载了所有的jar,vlean,build,但我得到了相同的结果: - (

帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

https://github.com/awslabs/amazon-sqs-java-messaging-lib/issues/22

我更改了gradle依赖项: 编译( “com.amazonaws:AWS-java的SDK-SQS:1.9.6”)

现在它完美无缺。

打开amazon-sqs-java-messaging-lib项目的pom.xml,我可以看到:

<properties> <aws-java-sdk.version>1.9.6</aws-java-sdk.version> </properties>

我想知道AWS团队何时计划将其更改为最新版本(例如1.11.13)?或其中一个最新的。