0

I have added ttl (30seconds) in the queue during the auto-creation of the queue in the RabbitConfig class. I would like to extended/reduced ttl(10seconds) for certain message and reject them, when necessary, inside the @RabbitListener method (listenToMain()).

I have managed to figure out that I am able to include the ttl(10seconds) in every message, which I have done it in the MyService.call(), but when the message goes back to the main-queue, the ttl will go back to the ttl (30seconds) I have set in the queue. How can I code it in a way that, the ttl will always be indicated in the MyService.call()?

Here are my codes:

RabbitConfig.java

@Configuration
public class RabbitConfig{

    /*Setup all the RabbitTemplate etc code*/
    
    //Auto creation of queue
    String mainQueue = "main-queue";
    String subQueue = "sub-queue";
    
    String mainExchange = "main-exchange";
    String subExchange = "sub-exchange";
    
    String mainRouting = "mainroute";
    String subRouting = "subroute";
    
    @Bean
    Queue mainQueue(){
        Map<String, Object> args = new HashMap();
        args.put("x-dead-letter-exchange", subExchange);
        args.put("x-dead-letter-routing-key", subRouting);
        
        return ....to build this queue with the args values.....
    }
    
    @Bean
    Queue subQueue(){
        Map<String, Object> args = new HashMap();
        args.put("x-dead-letter-exchange", mainExchange);
        args.put("x-dead-letter-routing-key", mainRouting);
        args.put("x-message-ttl", 30000);
        
        return ....to build this queue with the args values.....
    }
    
    @Bean
    Exchange mainExchange(){
        return ......to build the main exchange......
    }
    
    @Bean
    Exchange subExchange(){
        return ......to build the sub exchange......
    }
    
    @Bean
    Binding mainBinding(){
        return ...tobind the mainExchange() and mainQueue().......
    }
    
    @Bean
    Binding subBinding(){
        return ...tobind the subExchange() and subQueue().......
    }
}

MyMessagePostProcessor.java

public class MyMessagePostProcessor implements MessagePostProcessor {

    private final Integer ttl;

    public MyMessagePostProcessor(final Integer ttl) {
        this.ttl = ttl;
    }

    @Override
    public Message postProcessMessage(final Message message) throws AmqpException {
        message.getMessageProperties().getHeaders().put("expiration", ttl.toString());
        return message;
    }
}

MyService.java

@Service
public class MyService{
    public void call(){
        final String message = "message";
        final MessagePostProcessor messagePostProcessor = new MyMessagePostProcessor(10000);
        rabbitTemplate.convertAndSend("sub-exchange", "subroute", message, messagePostProcessor);
    }
}

RabbitConsume.java

@Service
public class RabbitConsume{
    @RabbitListener(queue="main-queue")
    public void listenToMain(String msg, Channel channel,
        @Header(AmqpHeaders.DELIVERY_TAG) long tag  
    ){
        channel.basicReject(tag, false)
    }
}
xxestter
  • 441
  • 7
  • 19

0 Answers0