0
votes

I have a Spring Integration configuration that utilizes a priority channel. When an item is read from that channel, local resources are checked at that point in time, and if the resources are not available to process the item, I would like to requeue the message so that another machine picks it up. Originally, I wrongly threw an exception thinking that a requeue would occur, but as was answered in my other question this is not going to work since the priority channel executes in another thread than the listener container.

I thought about placing a filter right after the inbound channel adapter, and throwing an exception if resources are not available at that time, but at that instance in time an accurate assessment of resources cannot be made because resource availability at that time does match what will be available when the message is selected based upon priority.

My next thought is to place a filter after the priority channel and before the service activator and direct messages that cannot be handled by current resources to the discard-channel which is defined as an outbound channel adapter that sends the message back to the original queue. Are there pitfalls to this approach?

EDIT 20150917:

Per Gary's advice, I have moved to RabbitMQ 3.5.x in order to take of the built-in priority queues. I now have a problem tracking the number of attempts as it appears my original message is placed back on the queue, rather than my modified message. I have updated the code blocks to reflect the current setup.

EDIT 20150922:

I am updating this post to reflect the final proof of concept code base that I created. I am not a Spring-Integration expert by any means, so please keep that in mind as well as the fact that this test code is not production ready. My original intent was to have messages resubmitted and retried a certain amount of times if a particular exception was thrown. This can be accomplished using the StatefulRetryOperationsInterceptor. But to experiment further, I wanted to be able to set/increment a header on failure and then have something in my flow that could react to that value. That was accomplished by using an extension of the RepublishMessageRecoverer that overrides additionalHeaders(). This object then is used to configure the RetryOperationsInterceptor.

One other minor thing: I wanted to reduce some of the default Spring Integration logging when my signal exception was thrown, so I needed to make sure I named my error channel "errorChannel" in order to replace the Spring Integration default. I also needed to create a custom ErrorHandler which to assign to the ListenerContainer default which logs everything to ERROR level.

Here is my current setup:

Spring Integration 4.2.0.RELEASE

Spring AMQP 1.5.0.RELEASE

RabbitMQ 3.5.x

Configuration

@Autowired
public void setSpringIntegrationConfigHelper (SpringIntegrationHelper springIntegrationConfigHelper) {
    this.springIntegrationConfigHelper = springIntegrationConfigHelper;   
}

@Bean
public String priorityPOCQueueName() {
    return "poc.priority";
}

@Bean
public Queue priorityPOCQueue(RabbitAdmin rabbitAdmin) {
    boolean durable = true;
    boolean exclusive = false;
    boolean autoDelete = false;

    //Adding the x-max-priority argument is what signals RabbitMQ that this is a priority queue. Must be Rabbit 3.5.x
    Map<String,Object> arguments = new HashMap<String, Object>();
    arguments.put("x-max-priority", 5);

    Queue queue = new Queue(priorityPOCQueueName(),
            durable,
            exclusive,
            autoDelete,
            arguments);

    rabbitAdmin.declareQueue(queue);
    return queue;
}

@Bean
public Binding priorityPOCQueueBinding(RabbitAdmin rabbitAdmin) {
    Binding binding = new Binding(priorityPOCQueueName(),
                                  DestinationType.QUEUE,
                                  "amq.direct",
                                  priorityPOCQueue(rabbitAdmin).getName(),
                                  null);
    rabbitAdmin.declareBinding(binding);
    return binding;
}

@Bean
public AmqpTemplate priorityPOCMessageTemplate(ConnectionFactory amqpConnectionFactory,
                                                @Qualifier("priorityPOCQueueName") String queueName,
                                                @Qualifier("jsonMessageConverter") MessageConverter messageConverter) {
    RabbitTemplate template = new RabbitTemplate(amqpConnectionFactory);
    template.setChannelTransacted(false);
    template.setExchange("amq.direct");
    template.setQueue(queueName);
    template.setRoutingKey(queueName);
    template.setMessageConverter(messageConverter);
    return template;
}


@Autowired
@Qualifier("priorityPOCQueue")
public void setPriorityPOCQueue(Queue priorityPOCQueue) {
    this.priorityPOCQueue = priorityPOCQueue;
}

@Bean
public MessageRecoverer miTestMessageRecoverer(final AmqpTemplate priorityPOCMessageTemplate) {
    return new MessageRecoverer() {

        @Override
        public void recover(org.springframework.amqp.core.Message msg, Throwable t) {
            StringBuilder sb = new StringBuilder();
            sb.append("Firing Test Recoverer: ").append(t.getClass().getName()).append(" Message Count: ")
            .append(msg.getMessageProperties().getMessageCount())
            .append(" ID: ").append(msg.getMessageProperties().getMessageId())
            .append(" DeliveryTag: ").append(msg.getMessageProperties().getDeliveryTag())
            .append(" Redilivered: ").append(msg.getMessageProperties().isRedelivered());
            logger.debug(sb.toString());

            PriorityMessage m = new PriorityMessage(5);
            m.setId(randomGenerator.nextLong(10L, 1000000L));
            priorityPOCMessageTemplate.convertAndSend(m , new SimulateErrorHeaderPostProcessor(Boolean.FALSE, m.getPriority()));
        }

    };
}


@Bean
public RepublishMessageRecoverer miRepublishRecoverer(final AmqpTemplate priorityPOCMessageTemplate) {
    class MiRecoverer extends RepublishMessageRecoverer {

        public MiRecoverer(AmqpTemplate errorTemplate) {
            super(errorTemplate);
            this.setErrorRoutingKeyPrefix("");
        }

        @Override
        protected Map<? extends String, ? extends Object> additionalHeaders(
                org.springframework.amqp.core.Message message, Throwable cause) {
            Map<String, Object> map = new HashMap<>();
           if (message.getMessageProperties().getHeaders().containsKey("jmattempts") == false) {
                 map.put("jmattempts", 0);
            } else {
                Integer count = Integer.valueOf(message.getMessageProperties().getHeaders().get("jmattempts").toString());
                map.put("jmattempts", ++count);
            }
           return map;
        }
    } ;
    return new MiRecoverer(priorityPOCMessageTemplate);
}

@Bean
public StatefulRetryOperationsInterceptor inadequateResourceInterceptor(@Qualifier("priorityPOCMessageTemplate") AmqpTemplate priorityPOCMessageTemplate
        , @Qualifier("priorityMessageKeyGenerator") PriorityMessageKeyGenerator priorityMessageKeyGenerator
        ,  @Qualifier("miTestMessageRecoverer") MessageRecoverer messageRecoverer
        , @Qualifier("miRepublishRecoverer") RepublishMessageRecoverer miRepublishRecoverer) {
    StatefulRetryInterceptorBuilder b = RetryInterceptorBuilder.stateful();
    return b.maxAttempts(2)
            .backOffOptions(2000L, 1.0D, 4000L)
            .messageKeyGenerator(priorityMessageKeyGenerator)
            .recoverer(miRepublishRecoverer)
           .build();
 }


@Bean(name="exec.priorityPOC")
TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor e = new ThreadPoolTaskExecutor();
    e.setCorePoolSize(1);
    e.setQueueCapacity(1);
    return e;
}

/*    @Bean(name="poc.priorityChannel")
public MessageChannel pocPriorityChannel() {
    PriorityChannel c = new PriorityChannel(new PriorityComparator());
    c.setComponentName("poc.priorityChannel");
    c.setBeanName("poc.priorityChannel");
    return c;
}
*/
@Bean(name="poc.inputChannel")
public MessageChannel pocPriorityChannel() {
    DirectChannel c = new DirectChannel();
    c.setComponentName("poc.inputChannel");
    c.setBeanName("poc.inputChannel");
    return c;
}

@Bean(name="poc.inboundChannelAdapter") //make this a unique name
public AmqpInboundChannelAdapter amqpInboundChannelAdapter(@Qualifier("exec.priorityPOC") TaskExecutor taskExecutor
        , @Qualifier("errorChannel") MessageChannel pocErrorChannel
        , @Qualifier("inadequateResourceInterceptor") StatefulRetryOperationsInterceptor inadequateResourceInterceptor) {

    org.aopalliance.aop.Advice[] adviceChain = new org.aopalliance.aop.Advice[]{inadequateResourceInterceptor};
    int concurrentConsumers = 1;
    AmqpInboundChannelAdapter a =  springIntegrationConfigHelper.createInboundChannelAdapter(taskExecutor
            , pocPriorityChannel(), new Queue[]{priorityPOCQueue},  concurrentConsumers, adviceChain
            , new PocErrorHandler());
    a.setErrorChannel(pocErrorChannel);
    return a;

}

@Transformer(inputChannel = "poc.inputChannel", outputChannel = "poc.procesPoc")
public Message<PriorityMessage> incrementAttempts(Message<PriorityMessage> msg) {
    //I stopped using this in the POC.
    return msg;
}

@ServiceActivator(inputChannel="poc.procesPoc")
public void procesPoc(@Header(SimulateErrorHeaderPostProcessor.ERROR_SIMULATE_HEADER_KEY) Boolean simulateError
        ,  @Headers Map<String, Object> headerMap
        , PriorityMessage priorityMessage) throws InterruptedException {
    if (isFirstMessageReceived == false) {
        //Thread.sleep(15000); //Cause a bit of a backup so we can see prioritizing in action.
        isFirstMessageReceived = true;
    }
    Integer retryAttempts = 0;
    if (headerMap.containsKey("jmattempts")) {
        retryAttempts = Integer.valueOf(headerMap.get("jmattempts").toString());
    }
    logger.debug("Received message with priority: " + priorityMessage.getPriority() + ", simulateError: " + simulateError +  ", Current attempts count is "
        + retryAttempts);
    if (simulateError && retryAttempts < PriorityMessage.MAX_MESSAGE_RETRY_COUNT) {
        logger.debug(" Simulating an error and re-queue'ng. Current attempt count is " + retryAttempts);
        throw new AnalyzerNonAdequateResourceException();
    } else if (simulateError && retryAttempts > PriorityMessage.MAX_MESSAGE_RETRY_COUNT) {
        logger.debug(" Max attempt count exceeded");
    }
}

/**************************************************************************************************
 * 
 *                        Error Channel
 *
 **************************************************************************************************/
//Note that we want to override default Spring error channel, so the name of the bean must be errorChannel
@Bean(name="errorChannel")
public MessageChannel pocErrorChannel() {
    DirectChannel c = new DirectChannel();
    c.setComponentName("errorChannel");
    c.setBeanName("errorChannel");
    return c;
}

@ServiceActivator(inputChannel="errorChannel")
public void pocHandleError(Message<MessagingException> message) throws Throwable {
     MessagingException me = message.getPayload();
    logger.error("pocHandleError: error encountered: " +  me.getCause().getClass().getName());
    SortedMap<String, Object> sorted= new TreeMap<>();
    sorted.putAll(me.getFailedMessage().getHeaders());
    if (me.getCause() instanceof AnalyzerNonAdequateResourceException) {
        logger.debug("Headers: " + sorted.toString());
        //Let this message get requeued
        throw me.getCause();

    }


    Message<?> failedMsg = me.getFailedMessage();
    Object o = failedMsg.getPayload();
    StringBuilder sb = new StringBuilder();
    if (o != null) {
        sb.append("AnalyzerErrorHandler: Failed Message Type: ")
           .append(o.getClass().getCanonicalName()).append(". toString: ").append(o.toString());
        logger.error(sb.toString());
    }

    //The first level sometimes brings back either MessagingHandlingException or 
    //MessagingTransformationException which may contain a subcause 
    Exception e = (Exception)me.getCause();
    int i = 0;
    sb.delete(0, sb.length());
    sb.append("AnalyzerErrorHandler nested messages: ");
    while (e != null && i++ < 10) {
        sb.append(System.lineSeparator()).append("  ")
        .append(e.getClass().getCanonicalName()).append(": ")
        .append(e.getMessage());
    }
    if (i > 0) {
        logger.error(sb.toString());
    }

    //Don't want a message to recycle
    throw new AmqpRejectAndDontRequeueException(e);
}



/**
 * This gets set on the ListenerContainer.  The default handler on the listener
 * container logs everything with full stack trace.  We don't want to do that
 * for our known resource exception
 */
public static class PocErrorHandler implements ErrorHandler {

    @Override
    public void handleError(Throwable t) {
        Throwable cause = t.getCause();
        if (cause != null) {
            while (cause.getCause() != null) {
                cause = cause.getCause();
            }
        } else {
            cause = t;
        }
        if (cause instanceof AnalyzerNonAdequateResourceException) {
            logger.info(AnalyzerNonAdequateResourceException.class.getName() + ": not enough resources to process the item.");
            return;
        }
        else {
            logger.error("POC Listener Exception",  t);
        }
    }
}

SpringIntegrationHelper

protected ConnectionFactory connectionFactory;
protected MessageConverter messageConverter;

@Autowired
public void setConnectionFactory (ConnectionFactory connectionFactory) {
    this.connectionFactory = connectionFactory;
}

@Autowired
public void setMessageConverter(@Qualifier("jsonMessageConverter") MessageConverter messageConverter) {
    this.messageConverter = messageConverter;
}

public AmqpInboundChannelAdapter createInboundChannelAdapter(TaskExecutor taskExecutor
        , MessageChannel outputChannel, Queue[] queues, int concurrentConsumers
        , org.aopalliance.aop.Advice[] adviceChain,
        ErrorHandler errorHandler) {
    SimpleMessageListenerContainer listenerContainer =
            new SimpleMessageListenerContainer(connectionFactory);
    //AUTO is default, but setting it anyhow.
    listenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
    listenerContainer.setAutoStartup(true);
    listenerContainer.setConcurrentConsumers(concurrentConsumers);
    listenerContainer.setMessageConverter(messageConverter);
    listenerContainer.setQueues(queues);
    //listenerContainer.setChannelTransacted(false);
    listenerContainer.setErrorHandler(errorHandler);
    listenerContainer.setPrefetchCount(1);
    listenerContainer.setTaskExecutor(taskExecutor);
    listenerContainer.setDefaultRequeueRejected(true);
    if (adviceChain != null && adviceChain.length > 0) {
        listenerContainer.setAdviceChain(adviceChain);
    }



    AmqpInboundChannelAdapter a = new AmqpInboundChannelAdapter(listenerContainer);
    a.setMessageConverter(messageConverter);
    a.setAutoStartup(true);
    a.setHeaderMapper(MyAmqpHeaderMapper.createPassAllHeaders());
    a.setOutputChannel(outputChannel);
    return a;
}
1

1 Answers

1
votes

It's not clear why you want to use a PriorityChannel in this context; why not use a priority queue in RabbitMQ? That way, you can run your flow on the container thread.

Sending the queue to the back of the queue yourself would work, but there is a risk of message loss.