首页 > 解决方案 > Undefined method type

问题描述

I want to shrink this Java code as much as possible:

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

I tried this possible solution but I get multiple errors:

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) {       
}

These are the errors when I made the changes:

The method basicConsume(String, boolean, Consumer) in the type Channel is not applicable for the arguments (String, boolean, new DefaultConsumer(){})

Can you advice how I can solve the issues? Probably I need to change the pattern which is used to redirect to proper Java methods?

标签: javarabbitmq

解决方案


看起来 DefaultConsumer 没有被导入,导致编译器无法识别它


推荐阅读