0

I am new to java's executor package. I want to delegate the responsibility of creating thread to another task(class) from spring rabbitmq(amqp). Currently I am creating inner level runnable class inside asynchronous MesageListener's onMessage() method to achieve parallelism when there are several messages on the rabbitmq topic exchange's queue. The functionality works fine for me. I want to separate the runnable code piece form onMessage(Message message) method. Following is the code.

package com.xyz.forum.event.listener;

import com.xyz.forum.constant.ClassType;
import com.xyz.forum.domain.dto.impl.AnswerDto;
import com.xyz.forum.domain.dto.impl.PollDto;
import com.xyz.forum.domain.dto.impl.QuestionDto;
import com.xyz.forum.manager.PumpManager;
import com.xyz.forum.utils.XyzForumConsumerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by bhupati on 11/3/16.
 */
public class XyzForumListener implements MessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(XyzForumListener.class);
    private static final String CLASS_HEADER = "__TypeId__";

    private Executor threadPool = Executors.newFixedThreadPool(10);

    @Autowired @Qualifier("pollPump") PumpManager pollPumpManager;
    @Autowired @Qualifier("questionPump") PumpManager questionPumpManager;
    @Autowired @Qualifier("answerPump") PumpManager answerPumpManager;

    @Override
    public void onMessage(final Message message) {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                handleMessage(message);
            }
        };
        threadPool.execute(runnable);
    }

    private void handleMessage(Message message) {
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        String classType = (String) headers.get(CLASS_HEADER);
        LOGGER.info("Got the message: " + classType);

        switch (classType) {
            case ClassType.QUESTION:
                QuestionDto questionDto = XyzForumConsumerUtils.parseDto(message.getBody(), QuestionDto.class);
                LOGGER.info("questionDto : " + questionDto.getBody());
                questionPumpManager.executePumpService(questionDto, headers);
                break;

            case ClassType.POLL:
                PollDto pollDto = XyzForumConsumerUtils.parseDto(message.getBody(), PollDto.class);
                LOGGER.info("pollDto : " + pollDto.getBody());
                pollPumpManager.executePumpService(pollDto, headers);
                break;

            case ClassType.ANSWER:
                AnswerDto answerDto = XyzForumConsumerUtils.parseDto(message.getBody(), AnswerDto.class);
                LOGGER.info("answerDto : " + answerDto.getBody());
                answerPumpManager.executePumpService(answerDto, headers);

            default: LOGGER.warn("Unknown Type");
                break;

        }

    }

}

In general is there a standard pattern for to run one listener's method concurrently using java.util.concurrent package.

Bhupati Patel
  • 1,400
  • 2
  • 13
  • 18

1 Answers1

1

Understand that, when handing off like that, the message will be immediately ack'd and will be lost on a system crash.

It's generally better to simply increase the concurrency in the listener container and let the container manage the threading.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179