I'm consuming some messages from a rabbit queue. Within the consumer/processor I need to invalidate a varnish cache and requeue the message if the purge failed. I'm using a local purge proxy. The consumer is a Dropwizard microservice but I'm using a BasicHttpClient because JerseyClient doesn't allow me (AFAIK) to set PURGE as http verb.
I'm trying to improve my error handling.. Is throwing an IllegalStateException fine or should I improve it? Thanks
public void invalidateVarnishCache( String level, String idUrl ) throws IOException {
try{
String xPurgeRegex = level+"/"+idUrl+"$";
Header header = new BasicHeader( "X-Purge-Regex", xPurgeRegex );
BasicHttpRequest purgeRequest = new BasicHttpRequest("PURGE", "/" );
purgeRequest.setHeader(header);
LOGGER.log( LogEntries.VarnPurge, level, idUrl, host, xPurgeRegex );
response = httpClient.execute(host, purgeRequest);
entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if( !is2XXResponse( statusCode ) ){
int respLength = entity.getContent().available();
byte[] errorResp = new byte[ respLength ];
entity.getContent().read(errorResp, 0, respLength);
LOGGER.log( LogEntries.VarnPurgeErrResp, statusCode, new String(errorResp) );
throw new IllegalStateException("Varnish local proxy returned response with status code "+statusCode+" from Varnish backend. Consumed message will be requeued.");
/////////////////////
}else{
LOGGER.log( LogEntries.VarnPurgeSuccessResp, statusCode );
}
EntityUtils.consume(entity);
}catch( IOException ex ){
LOGGER.log( LogEntries.VarnPurgeException, ex );
throw ex;
}
}
This is the consumer..
public MessageResult apply(UpdateMessage t) {
if( !agentValidator.validMessage( t ) ){
return MessageResult.ACK; // discard message
}
String urlRequest = t.getLink();
String[] splitted = urlRequest.split( "/", -1 );
String level = splitted[2];
String idUrl = splitted[3];
try{
// Varnish cache purge. It will requeue if purge fails for any reason.
try{
pcsConnector.purgeVarnishBeforePcsRequest( level, idUrl );
}catch(Exception e){
LOGGER.log( LogEntries.PurgeError, host, urlRequest, e );
return MessageResult.NACK_REQUEUE;
}
// retrieve resource from ohter microservice
try{
response = connector.getLatestStateOfResource( urlRequest );
}catch(Exception e){
LOGGER.log( LogEntries.DBServiceError, pcsHost, urlRequest, e );
return MessageResult.NACK_REQUEUE;
}
// .....
// more logic
// ....
}catch(Exception e){
LOGGER.log( LogEntries.MsgProcessingError, t.toString(), e );
return MessageResult.NACK_REQUEUE;
}
LOGGER.log( LogEntries.MsgConsumedSuccess, t.toString() );
return MessageResult.ACK; // SUCCESS. Remove message from queue.
}