@Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String correlationId = properties.getCorrelationId(); if (correlationId != null) { PendingRequest request = pendingRequests.remove(correlationId); if (request != null) { try { request.complete(responseTranscoder.decodeFromBytes(body)); } catch (Exception e) { logger.error("Unhandled error completing RPC request", e); } } else { logger.warn("Cannot correlate request with id: " + correlationId + ", it is possible the request timedout."); } } else { logger.warn("Ignoring message with no correlation id"); } } });
@Override public void publish(K key, T event, long ttl) { this.publish( key, new BasicProperties.Builder().contentType("").deliveryMode(2).expiration(Long.toString(ttl)).build(), this.transcoder.encodeAsBytes(event) ); }
protected void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String correlationId = properties.getCorrelationId(); String replyTo = properties.getReplyTo(); if (correlationId != null && replyTo != null) { // decode the event T event = this.transcoder.decodeFromBytes(body); // handle the event R response = this.handler.handleDevliery(event); // ack the event this.channel.basicAck(envelope.getDeliveryTag(), false); // send the response if (response != null) { this.channel.basicPublish( "", replyTo, new BasicProperties("application/json", null, null, 1, null, correlationId, null, null, null, null, null, null, null, null), this.responseTranscoder.encodeAsBytes(response) ); } } else { logger.warn("Ignoring request without correlationId or replyTo"); } }
@Override public void publish(K key, T event) { this.publish( key, new BasicProperties.Builder().contentType("").deliveryMode(2).build(), this.transcoder.encodeAsBytes(event) ); }
protected void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { Timer.Context ctx = this.consumeTimer.time(); try { try { // decode the event T event = this.transcoder.decodeFromBytes(body); // handle the event this.handler.handleDevliery(properties.getHeaders(), event); // ack the event this.channel.basicAck(envelope.getDeliveryTag(), false); } catch (Exception e) { // send a nack for this delivery logger.error("Error handling message delivery, rejecting" + (this.requeueOnError ? " for requeue" : ""), e); this.channel.basicNack(envelope.getDeliveryTag(), false, this.requeueOnError); } } finally { ctx.stop(); } }
@Override public void publish(T event, long ttl) { if (this.defaultKey == null) throw new QueueException("No default key is given, cannot publish, did you mean to use: publish(K key, T event)?"); this.publish( this.defaultKey, new BasicProperties.Builder().contentType("").deliveryMode(2).expiration(Long.toString(ttl)).build(), this.transcoder.encodeAsBytes(event) ); } }
@Override public void publish(T event) { if (this.defaultKey == null) throw new QueueException("No default key is given, cannot publish, did you mean to use: publish(K key, T event)?"); this.publish( this.defaultKey, new BasicProperties.Builder().contentType("").deliveryMode(2).build(), this.transcoder.encodeAsBytes(event) ); }
@Override public Future<R> publish(K key, long timeout, T event, Consumer<R> onSuccess, Consumer<Exception> onError) { try { PendingRequest future = new PendingRequest(event, onSuccess, onError); // store the pending request this.pendingRequests.put(future.getId(), future); // schedule the timeout this.timer.schedule(future, timeout); // publish the request this.channel.basicPublish( this.exchange.getName(), key == null ? "" : key.toString(), new BasicProperties("application/json", null, null, 1, null, future.getId(), this.replyQueue.getName(), null, null, null, null, null, null, null), this.transcoder.encodeAsBytes(event) ); return future; } catch (IOException e) { throw new QueueException("Failed to publish request", e); } }