-1

There are 100 message in IBM queue (insert_queue). I want to consume 10 and store in a object and process 10 consumed message and wait for some time, then acknowledge the 10 consumed messages.

My sample code:

public void consume(String queueName, int maxloadcount) throws Exception {

  Source<Message, KillSwitch> jmsSource =
            JmsConsumer.create(
                    JmsConsumerSettings.create(IBMQueueConnectionFactory.getMQQueueConnectionFactory())
                            .withQueue(queueName)
                            .withSessionCount(1)
                            .withBufferSize(10)
                            .withAcknowledgeMode(AcknowledgeMode.ClientAcknowledge()));

    CompletionStage<List<Message>> result =
            jmsSource
                    .take(maxloadcount)
                    .map(message -> {
                        return message;
                    })
                    .runWith(Sink.seq(), materializer);

    final List<Message> outMessages = result.toCompletableFuture().get(3, TimeUnit.SECONDS);
    for (Message outMsg : outMessages) {
        BytesMessage msg = (BytesMessage) outMsg;
        byte[] messageBody = msg.getBody(byte[].class);
        System.out.println(new String(messageBody));
    }
    TimeUnit.SECONDS.sleep(5);
    outMessages.stream().forEach(message -> {
        try {
           message.acknowledge();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    });

}

which is not working

1 Answers1

0

Source.take completes the stream once the given number of elements is consumed. What you probably want is Source.throttle. The following example sends downstream, at most, 10 messages per 5 seconds:

CompletionStage<List<Message>> result =
  jmsSource
    .throttle(10, java.time.Duration.ofSeconds(5L))
    .map(message -> {
      message.acknowledge();
      return message;
    })
    .runWith(Sink.seq(), materializer);

Alternatively, take a look at Source.groupedWithin, which chunks a stream "into groups of elements received within a time window, or limited by the given number of elements, whatever happens first."

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
  • throttle which keeps consuming .but i was excepting consume 10 messages (store in list) and wait for 5 minutes then Acknowledge for each message.can you please guide on above sample – Sukanta Nahak Aug 13 '18 at 10:01
  • Thanks Jeffrey Chung can you please tell how can i store 10 messages in the list before Acknowledge to the queue. my criteria is from 100 consume 10 by 10 and store 10 messages in a list and process the 10 messsages and finally ackwnowledge 10 messages. – Sukanta Nahak Aug 13 '18 at 18:23