我想尽可能地缩小这段Java代码:
Consumer consumerone = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
processobjone(body);
}
};
channel.basicConsume(QUEUE_FIRST_NAME, true, consumerone);
Consumer consumersec = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
processobjsec(body);
}
};
channel.basicConsume(QUEUE_SEC_NAME, true, consumersec);
// Processing
private void processobjone(byte[] body) {
// handle obj
}
private void processobjsec(byte[] body) {
// handle obj
}
// .... and many more
我尝试了这种可能的解决方案但是我遇到了多个错误:
import java.util.function.Consumer;
Map<String, Consumer<byte[]>> queueToConsumer = new HashMap<>();
queueToConsumer.put(ElementTypeEnum.QUEUE_TRANSACTION, this::process_transaction);
queueToConsumer.put(ElementTypeEnum.QUEUE_API_ATTEMPT, this::process_api_attempt);
queueToConsumer.forEach((queueName, consumer) -> {
channel.basicConsume(queueName, true, new DefaultConsumer() {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
consumer.accept(body);
}
});
});
private void process_transaction(byte[] vv) {
}
private void process_api_attempt(byte[] vv) {
}
这些是我做出更改时的错误:
The method basicConsume(String, boolean, Consumer) in the type Channel is not applicable for the arguments (String, boolean, new DefaultConsumer(){})
您能告诉我如何解决问题吗?可能我需要更改用于重定向到正确的Java方法的模式?
答案 0 :(得分:1)
看起来没有导入DefaultConsumer,导致编译器无法识别它