protected void publish(RoutingKey key, BasicProperties props, byte[] event) { if (this.closed) throw new QueueException("This producer is closed, cannot publish"); Timer.Context ctx = this.publishTimer.time(); try { try { this.channel.basicPublish(this.exchange, key == null ? null : key.toString(), props, event); } catch (IOException e) { throw new QueueException("Cannot publish event", e); } } finally { ctx.stop(); } }
@Override public synchronized final void removeBinding(K binding) { if (binding != null) { this.bindings.remove(binding); try { this.removeQueueBinding(this.channel, binding.toString()); } catch (Exception e) { throw new QueueException("Failed to remove binding", e); } } }
@Override public synchronized final void addBinding(K binding) { if (binding != null) { this.bindings.add(binding); try { this.addQueueBinding(this.channel, binding.toString()); } catch (Exception e) { throw new QueueException("Failed to add binding", e); } } }
throw new QueueException("Cannot connect to queue broker", e); throw new QueueException("Failed to open channel", e);
/** * Invoke this during the constructor! */ protected void init() { if (this.closed) return; try { // initialise the connection and channel this.channel = broker.connect(); // log recovery events ((Recoverable) this.channel).addRecoveryListener((r) -> { logger.warn("Lost connection to RabbitMQ lost, auto-recovery complete"); }); // setup this thing this.setup(); } catch (IOException e) { logger.warn("Failed to connect to RabbitMQ", e); throw new QueueException("Failed to connect to RabbitMQ, is it up?", e); } }
throw new QueueException("Cannot init connection factory", e); throw new QueueException("Cannot init connection factory", e); throw new QueueException("Cannot init connection factory", e);
@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); } }
throw new QueueException("Cannot declare consumer", e);