我正在尝试使用多个使用者来消耗来自Rabbit mq的消息。
在我的基本用例中:我们的队列中有5万条消息,每个使用者从队列中读取一条消息,并用50个DB查询执行算法,然后将ack发送给Rabbit mq。
我的问题是5个消费者,10个消费者,20个消费者的ack / sec保持不变,即11 Ack / sec。
我尝试在算法中评论我的插入查询。因此,我认为代码中没有锁定问题。
public void init() {
try {
factory.setUri(System.getenv("CLOUDAMQP_URL"));
connection = factory.newConnection();
} catch (IOException | TimeoutException | KeyManagementException | NoSuchAlgorithmException
| URISyntaxException e) {
log.error(e.getMessage(), e);
}
}
public void processJobInstance() {
for (int i = 0; i < batchJobInstanceList.size(); i++) {
for (int c = 0; c < NUMBER_OF_CONSUMERS_PER_JOB; c++) {
try {
/*
* Do not close channel here as doWork is a future task. We will close channel in handle
* delivery.
*/
Channel channel = connection.createChannel();
final Consumer consumer = createConsumerBean(channel, batchJobInstance.getId());
boolean autoAck = false;
channel.basicQos(1);
channel.basicConsume(batchJobInstance.getId(), autoAck, consumer);
} catch (Exception e) {
log.error("Channel failed by Exception.", e);
}
}
}
}
@Bean
@Scope("prototype")
public Consumer createConsumerBean(Channel channel, final String id) {
return new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
try {
// Algo with 50 select calls and 5-10 insert
} catch (Exception ex) {
log.error("HandleDelivery failed by Exception.", ex);
} finally {
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
@Override
public void handleCancel(String consumerTag) throws IOException {
log.info("Cancelled: " + consumerTag);
}
};
}
我的代码有什么问题,那就是越来越多的使用者无法改善性能?