design patterns - Spring boot and rabbitmq integration, how to recover on component failure? -
i have spring rest app rabbit messaging middle ware. when app accepts incoming rest request, replies user meanwhile processing asynchronously, generates new message , puts on rabbit. there, consumers read message , deliver external system. now, there case rabbit server may down. handle kind of failure, have blocking queue (of course adjustable size)
@bean public taskexecutor taskexecutor() { threadpooltaskexecutor executor = new threadpooltaskexecutor(); executor.setcorepoolsize(5); executor.setmaxpoolsize(10); executor.setqueuecapacity(25); return executor; }
the queue able handle messages until rabbit comes online. if takes long , requests exceeds queue size, going fail. question is, there ways can improve , make more efficient? industry best practice? there patterns scenario?
i used spring retryable. pretty sufficient requirement. can retry, configure retry intervals, max attempts etc. implement custom recover function.
@async @retryable(value = { amqpexception.class }, maxattempts=10, backoff=@backoff(delay=100, maxdelay=300000,multiplier = 2)) public void sendemailmessagetoqueue(simplemailmessage email){ log.info("sending message queue"+thread.currentthread().getname()); try { rabbittemplate.convertandsend(queuename, email); }catch (amqpexception e){ log.log(level.warning,"could not send message queue, retry.",e); } } @recover public void connectionexception(amqpexception e) { log.log(level.severe,"could not send message queue, save message in db instead."+thread.currentthread().getname(),e); }
Comments
Post a Comment