public boolean isConnected() { return connection != null && connection.isOpen() && channel != null && channel.isOpen(); }
public void stop() throws IOException { if (channel != null && channel.isOpen()) { try { channel.close(); } catch (TimeoutException e) { LOG.error("Timeout when closing AMQP channel", e); channel.abort(); } } if (connection != null && connection.isOpen()) { connection.close(); } }
@Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); try { totalBytesRead.addAndGet(body.length); lastSecBytesReadTmp.addAndGet(body.length); final RawMessage rawMessage = new RawMessage(body); // TODO figure out if we want to unsubscribe after a certain time, or if simply blocking is enough here if (amqpTransport.isThrottled()) { amqpTransport.blockUntilUnthrottled(); } sourceInput.processRawMessage(rawMessage); channel.basicAck(deliveryTag, false); } catch (Exception e) { LOG.error("Error while trying to process AMQP message", e); if (channel.isOpen()) { channel.basicNack(deliveryTag, false, requeueInvalid); if (LOG.isDebugEnabled()) { if (requeueInvalid) { LOG.debug("Re-queue message with delivery tag {}", deliveryTag); } else { LOG.debug("Message with delivery tag {} not re-queued", deliveryTag); } } } } } });
/** * Publishes message with provided AMQP properties (see * {@link BasicProperties}) to a pre-defined AMQP Exchange. * * @param bytes bytes representing a message. * @param properties instance of {@link BasicProperties} * @param exchange the name of AMQP exchange to which messages will be published. * If not provided 'default' exchange will be used. * @param routingKey (required) the name of the routingKey to be used by AMQP-based * system to route messages to its final destination (queue). */ void publish(byte[] bytes, BasicProperties properties, String routingKey, String exchange) { this.validateStringProperty("routingKey", routingKey); exchange = exchange == null ? "" : exchange.trim(); if (exchange.length() == 0) { processLog.info("The 'exchangeName' is not specified. Messages will be sent to default exchange"); } processLog.info("Successfully connected AMQPPublisher to " + this.connectionString + " and '" + exchange + "' exchange with '" + routingKey + "' as a routing key."); final Channel channel = getChannel(); if (channel.isOpen()) { try { channel.basicPublish(exchange, routingKey, true, properties, bytes); } catch (Exception e) { throw new IllegalStateException("Failed to publish to Exchange '" + exchange + "' with Routing Key '" + routingKey + "'.", e); } } else { throw new IllegalStateException("This instance of AMQPPublisher is invalid since its publishingChannel is closed"); } }
private void init() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setAutomaticRecoveryEnabled(true); factory.setHost(this.server); if (this.port > 0) factory.setPort(this.port); if (this.username != null && this.username.length() > 0) factory.setUsername(this.username); if (this.password != null && this.password.length() > 0) factory.setPassword(this.password); try { this.connection = factory.newConnection(); //Map<String, Object> map = this.connection.getServerProperties(); if (!this.connection.isOpen()) throw new IOException("no connection"); this.channel = connection.createChannel(); if (!this.channel.isOpen()) throw new IOException("no channel"); this.queues = new ConcurrentHashMap<>(); } catch (TimeoutException e) { throw new IOException(e.getMessage()); } }
@Bean public CachingConnectionFactory cf() throws Exception { ConnectionFactory cf = mock(ConnectionFactory.class); cf.setHost("localhost"); cf = spy(cf); willAnswer(i -> { this.connection = mock(Connection.class); willAnswer(ii -> { this.channel = mock(Channel.class); given(this.channel.isOpen()).willReturn(true); return this.channel; }).given(this.connection).createChannel(); return this.connection; }).given(cf).newConnection((ExecutorService) isNull(), anyString()); cf.setAutomaticRecoveryEnabled(false); CachingConnectionFactory ccf = new CachingConnectionFactory(cf); ccf.setSimplePublisherConfirms(true); return ccf; }
private void testNackOrRequeue(boolean requeue) throws IOException, TimeoutException { Channel channel = mock(Channel.class); willReturn(true).given(channel).isOpen(); Envelope envelope = new Envelope(123L, false, "ex", "rk"); BasicProperties props = new BasicProperties.Builder().build(); GetResponse getResponse = new GetResponse(envelope, props, "bar".getBytes(), 0); willReturn(getResponse).given(channel).basicGet("foo", false); Connection connection = mock(Connection.class); willReturn(true).given(connection).isOpen(); willReturn(channel).given(connection).createChannel(); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); willReturn(connection).given(connectionFactory).newConnection((ExecutorService) isNull(), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory); AmqpMessageSource source = new AmqpMessageSource(ccf, "foo"); Message<?> received = source.receive(); verify(connection).createChannel(); StaticMessageHeaderAccessor.getAcknowledgmentCallback(received) .acknowledge(requeue ? Status.REQUEUE : Status.REJECT); verify(channel).basicReject(123L, requeue); verify(connection).createChannel(); ccf.destroy(); verify(channel).close(); verify(connection).close(30000); }
@Test public void testAck() throws Exception { Channel channel = mock(Channel.class); willReturn(true).given(channel).isOpen(); Envelope envelope = new Envelope(123L, false, "ex", "rk"); BasicProperties props = new BasicProperties.Builder().build();
public boolean isConnected() { return connection != null && connection.isOpen() && channel != null && channel.isOpen(); }
/** Gets a recovery channel, creating one if necessary. */ @Override Channel getRecoveryChannel() throws IOException { if (recoveryChannel == null || !recoveryChannel.isOpen()) recoveryChannel = delegate.createChannel(RECOVERY_CHANNEL_NUM); return recoveryChannel; }
/** Gets a recovery channel, creating one if necessary. */ @Override Channel getRecoveryChannel() throws IOException { if (recoveryChannel == null || !recoveryChannel.isOpen()) recoveryChannel = delegate.createChannel(RECOVERY_CHANNEL_NUM); return recoveryChannel; }
public boolean isConnected() { return connection != null && connection.isOpen() && channel != null && channel.isOpen(); }
Channel channel() throws ControllerException { this.lock.lock(); try { checkState(connected(),"Not connected"); if(!this.channel.isOpen()) { createChannel(); } return this.channel; } finally { this.lock.unlock(); } }
Channel channel() throws ControllerException { this.lock.lock(); try { checkState(connected(),"Not connected"); if(!this.channel.isOpen()) { createChannel(); } return this.channel; } finally { this.lock.unlock(); } }
/** * Unbind consumer from channel */ private void stop() throws IOException, TimeoutException { if (channel.isOpen()) { if (tag != null) { channel.basicCancel(tag); } channel.close(); } } }
public void close() { try { if(channel.isOpen()) { channel.close(); } if(connection.isOpen()) { connection.close(); } } catch (IOException e) { logger.warn("Error closing channel or connection to MQ service", e); } }
public void close() { try { if(channel.isOpen()) { channel.close(); } if(connection.isOpen()) { connection.close(); } } catch (IOException | TimeoutException e) { logger.warn("Error closing channel or connection to MQ service", e); } }
@Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); given(channel.isOpen()).willReturn(true); return channel; } }).given(mockConnection).createChannel();
@Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); given(channel.isOpen()).willReturn(true); return channel; } }).given(mockConnection).createChannel();
@Bean public ConnectionFactory connectionFactory() throws IOException { ConnectionFactory factory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); willReturn(connection).given(factory).createConnection(); willReturn(channel).given(connection).createChannel(anyBoolean()); given(channel.isOpen()).willReturn(true); return factory; }