// subscribed topic
final String topic = "meta-test";
// consumer group
final String group = "meta-example";
// create consumer,ǿ�ҽ���ʹ�õ���
final MessageConsumer consumer = sessionFactory.createConsumer(new ConsumerConfig(group));
// subscribe topic
consumer.subscribe(topic, 1024 * 1024, new MessageListener() {
private int count = 0;
@Override
public void recieveMessages(final Message message) {
System.out.println("Receive message " + new String(message.getData()));
// set auto ack to false
message.getPartition().setAutoAck(false);
// ack once per two messages
if (++this.count % 2 == 0) {
message.getPartition().ack();
System.out.println("ack message");
}
}
@Override
public Executor getExecutor() {
// Thread pool to process messages,maybe null.
return null;
}
});
// complete subscribe
consumer.completeSubscribe();
}