D
D
drunkstudent2015-06-20 14:42:45
RabbitMQ
drunkstudent, 2015-06-20 14:42:45

Synchronization of methods or do everything in one method (multithreaded programming)?

I'm using Spring AMQP for messaging and queuing image and video processing.

@Transactional
    @RabbitListener(queues = "test")
    public void processImage(Message message, @Header("image_id") long imageId,
                             @Header("account_id") long accountId) throws Exception {
        Image image = imageService.getImage(imageId);
        byte[] renderedImageBuffer = null;
        String format = ImageUtils.getImageFormat(message.getBody());
        switch (format.toLowerCase()) {
            case "jpeg":
                renderedImageBuffer = message.getBody();
                break;
            case "png":
                renderedImageBuffer = ImageUtils.convertPNGtoJPEG(message.getBody());
                break;
        }
        image.setWidth(image.getBufferedImage().getWidth());
        image.setHeight(image.getBufferedImage().getHeight());
        image.setData(renderedImageBuffer);
        imageService.updateImage(image);
        Image avatar = ImageUtils.scaleImage(image, 35, 35);
        avatar.setFileName("fss_avatar" + accountId);
        imageService.saveImage(avatar);
        Account account = accountService.getAccount(accountId);
        account.setAvatar(avatar);
        accountService.updateAccount(account);
    }

This listener uses 20-30 parallel threads to process data.
@Bean
    public SimpleRabbitListenerContainerFactory container() {
        SimpleRabbitListenerContainerFactory container = new SimpleRabbitListenerContainerFactory();
        container.setConnectionFactory(connectionFactory);
        container.setConcurrentConsumers(20);
        container.setMaxConcurrentConsumers(30);
        container.setChannelTransacted(true);
        container.setTransactionManager(transactionManager());
        return container;
    }

And everything seems to be fine, but then the question arose about thread safety in the utility class (ImageUtils). Should I shove all the methods into the message handler or make the methods of the utility class synchronized? On the one hand, with the first method there is less hassle, but on the other hand, the code will become unreadable. In which case will the performance be greater?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ixSci, 2015-06-20
@ixSci

Performance can only be measured, it cannot be predicted. At least in cases with multithreading, any predictions, as a rule, result in fortune-telling on coffee grounds.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question