@Override public void setUri(String uriString) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException { super.setUri(uriString); }
/** * Sets up the queue. The default implementation just declares the queue. The user may override * this method to have a custom setup for the queue (i.e. binding the queue to an exchange or * defining custom queue parameters) */ protected void setupQueue() throws IOException { if (queueName != null) { channel.queueDeclare(queueName, false, false, false, null); } }
public void connect() throws IOException { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); factory.setPort(port); factory.setVirtualHost(virtualHost); factory.setRequestedHeartbeat(heartbeatTimeout); factory.setAutomaticRecoveryEnabled(true); factory.useSslProtocol(); } catch (NoSuchAlgorithmException | KeyManagementException e) { throw new IOException("Couldn't enable TLS for AMQP input.", e); factory.setUsername(username); factory.setPassword(password); connection = factory.newConnection(); } catch (TimeoutException e) { throw new IOException("Timeout while opening new AMQP connection", e); channel = connection.createChannel(); channel.basicQos(prefetchCount); connection.addShutdownListener(cause -> { if (cause.isInitiatedByApplication()) { LOG.info("Shutting down AMPQ consumer."); return;
public RabbitmqProducer(MaxwellContext context) { super(context); exchangeName = context.getConfig().rabbitmqExchange; props = context.getConfig().rabbitmqMessagePersistent ? MessageProperties.MINIMAL_PERSISTENT_BASIC : null; ConnectionFactory factory = new ConnectionFactory(); factory.setHost(context.getConfig().rabbitmqHost); factory.setPort(context.getConfig().rabbitmqPort); factory.setUsername(context.getConfig().rabbitmqUser); factory.setPassword(context.getConfig().rabbitmqPass); factory.setVirtualHost(context.getConfig().rabbitmqVirtualHost); try { this.channel = factory.newConnection().createChannel(); if(context.getConfig().rabbitmqDeclareExchange) { this.channel.exchangeDeclare(exchangeName, context.getConfig().rabbitmqExchangeType, context.getConfig().rabbitMqExchangeDurable, context.getConfig().rabbitMqExchangeAutoDelete, null); } } catch (IOException | TimeoutException e) { throw new RuntimeException(e); } }
@Override public void open(Configuration config) throws Exception { super.open(config); ConnectionFactory factory = setupConnectionFactory(); try { connection = factory.newConnection(); channel = connection.createChannel(); if (channel == null) { throw new RuntimeException("None of RabbitMQ channels are available"); } setupQueue(); consumer = new QueueingConsumer(channel); RuntimeContext runtimeContext = getRuntimeContext(); if (runtimeContext instanceof StreamingRuntimeContext && ((StreamingRuntimeContext) runtimeContext).isCheckpointingEnabled()) { autoAck = false; // enables transaction mode channel.txSelect(); } else { autoAck = true; } LOG.debug("Starting RabbitMQ source with autoAck status: " + autoAck); channel.basicConsume(queueName, autoAck, consumer); } catch (IOException e) { throw new RuntimeException("Cannot create RMQ connection with " + queueName + " at " + rmqConnectionConfig.getHost(), e); } running = true; }
@Override public void open(Configuration config) throws Exception { ConnectionFactory factory = rmqConnectionConfig.getConnectionFactory(); try { connection = factory.newConnection(); channel = connection.createChannel(); if (channel == null) { throw new RuntimeException("None of RabbitMQ channels are available"); } setupQueue(); if (returnListener != null) { channel.addReturnListener(returnListener); } } catch (IOException e) { throw new RuntimeException("Error while creating the channel", e); } }
public void acknowledge(final GetResponse response) throws IOException { if (autoAcknowledge) { return; } getChannel().basicAck(response.getEnvelope().getDeliveryTag(), true); }
@Override public void run(SourceContext<OUT> ctx) throws Exception { while (running) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); synchronized (ctx.getCheckpointLock()) { OUT result = schema.deserialize(delivery.getBody()); if (schema.isEndOfStream(result)) { break; } if (!autoAck) { final long deliveryTag = delivery.getEnvelope().getDeliveryTag(); if (usesCorrelationId) { final String correlationId = delivery.getProperties().getCorrelationId(); Preconditions.checkNotNull(correlationId, "RabbitMQ source was instantiated " + "with usesCorrelationId set to true but a message was received with " + "correlation id set to null!"); if (!addId(correlationId)) { // we have already processed this message continue; } } sessionIds.add(deliveryTag); } ctx.collect(result); } } }
@Override public void close() throws TimeoutException, IOException { if (closed) { return; } if (logger.isDebugEnabled()) { logger.debug("Closing AMQP channel for " + this.channel.getConnection().toString()); } this.channel.close(); closed = true; }
@Override public void close() throws TimeoutException, IOException { closed = true; GetResponse lastMessage = null; GetResponse response; while ((response = responseQueue.poll()) != null) { lastMessage = response; } if (lastMessage != null) { getChannel().basicNack(lastMessage.getEnvelope().getDeliveryTag(), true, true); } }
@Override public void close() throws Exception { super.close(); try { if (connection != null) { connection.close(); } } catch (IOException e) { throw new RuntimeException("Error while closing RMQ connection with " + queueName + " at " + rmqConnectionConfig.getHost(), e); } }
@Override public void handleDelivery(final String consumerTag, final Envelope envelope, final BasicProperties properties, final byte[] body) throws IOException { if (!autoAcknowledge && closed) { channel.basicReject(envelope.getDeliveryTag(), true); return; } try { responseQueue.put(new GetResponse(envelope, properties, body, Integer.MAX_VALUE)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } };
@Override public void handleDelivery( String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body ) { this._queue.add(new Delivery(envelope, properties, body)); }
@Override public void close() { Exception t = null; try { if (channel != null) { channel.close(); } } catch (IOException | TimeoutException e) { t = e; } try { if (connection != null) { connection.close(); } } catch (IOException e) { if (t != null) { LOG.warn("Both channel and connection closing failed. Logging channel exception and failing with connection exception", t); } t = e; } if (t != null) { throw new RuntimeException("Error while closing RMQ connection with " + queueName + " at " + rmqConnectionConfig.getHost(), t); } }
/** * Sets up the queue. The default implementation just declares the queue. The user may override * this method to have a custom setup for the queue (i.e. binding the queue to an exchange or * defining custom queue parameters) */ protected void setupQueue() throws IOException { channel.queueDeclare(queueName, true, false, false, null); }
@JsonIgnore @Override public void setUri(URI uri) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException { super.setUri(uri); }