private byte[] receiveInternal(long timeout) throws IOException { if (timeout <= 0) timeout = Long.MAX_VALUE; long termination = timeout <= 0 || timeout == Long.MAX_VALUE ? Long.MAX_VALUE : System.currentTimeMillis() + timeout; Throwable ee = null; while (System.currentTimeMillis() < termination) { try { GetResponse response = channel.basicGet(this.queueName, true); if (response != null) { //Envelope envelope = response.getEnvelope(); //long deliveryTag = envelope.getDeliveryTag(); //channel.basicAck(deliveryTag, false); return response.getBody(); } //Data.logger.warn("receive failed: response empty"); } catch (Throwable e) { Data.logger.warn("receive failed: " + e.getMessage(), e); ee = e; } try {Thread.sleep(1000);} catch (InterruptedException e) {return null;} } if (ee == null) return null; throw new IOException(ee.getMessage()); }
@Override public GetResponse basicGet(String queue, boolean autoAck) throws IOException { return this.delegate.basicGet(queue, autoAck); }
@Override public GetResponse basicGet(String queue, boolean autoAck) throws IOException { return this.delegate.basicGet(queue, autoAck); }
@Override public GetResponse basicGet(String queue, boolean autoAck) throws IOException { return delegate.basicGet(queue,autoAck); } }
public static GetResponse consumeSingleMessage(Channel channel, String queueName) throws IOException { return channel.basicGet(queueName, true); }
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); }
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();
private Boolean messageReceived(Channel channel, String queueName) { try { return channel.basicGet(queueName, !AUTO_ACK) != null; } catch (Exception e) { return false; } } }
Channel channel = connection.createChannel(this.transacted); try { GetResponse resp = channel.basicGet(this.queue, false); if (resp == null) { RabbitUtils.closeChannel(channel);
@Override public Optional<Message> next() { try { return Optional.ofNullable(MessageFactory.create(channel.basicGet(queue, true))); } catch (final IOException e) { throw new JocoteException(e); } }
@Override public Optional<Message> next() { try { final GetResponse basicGet = channel.basicGet(queue, false); if (basicGet != null) lastMessage = basicGet; return Optional.ofNullable(MessageFactory.create(basicGet)); } catch (final IOException e) { throw new JocoteException(e); } }
@Override public byte[] get() { try { GetResponse response = null; while (response == null) { response = channel.basicGet(queueName, true); } return response.getBody(); } catch (final IOException e) { monitor.warn("Failed to publish message", e); return new byte[0]; } } }
GetResponse getFromRabbitQueue() { String qN = rmqQueueName(); try { return getSession().getChannel().basicGet(qN, false); } catch (Exception e) { // includes unchecked exceptions, e.g. ShutdownSignalException if (!(e instanceof ShutdownSignalException) && !(e.getCause() instanceof ShutdownSignalException)) { logger.error("basicGet for queue '{}' threw unexpected exception", qN, e); } } return null; }
@Override public byte[] get() { try { GetResponse response = null; while (response == null) { response = channel.basicGet(queueName, true); } return response.getBody(); } catch (final IOException e) { monitor.warn("Failed to publish message", e); return new byte[0]; } } }
/** * Asserts that the given queue DOES contain * at least one message. * * @param queue The queue name * @throws IOException */ public void queueNotEmtpy(String queue) throws IOException { GetResponse response = channel.basicGet(queue, false); Assert.assertNotNull(response); channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true); }
/** * Asserts that the given queue DOES NOT contain * any messages. * * @param queue The queue name * @throws IOException */ public void queueEmtpy(String queue) throws IOException { GetResponse response = channel.basicGet(queue, false); if (response != null) { channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true); } Assert.assertNull(response); }
public Message receive(String queueName) throws IOException { GetResponse response; try { response = channel.basicGet(queueName, NO_AUTO_ACK); } catch (IOException e) { waitForConnection("Could not receive message from " + queueName); response = channel.basicGet(queueName, NO_AUTO_ACK); } if (response != null) { String body = new String(response.getBody(), StandardCharsets.UTF_8); Message message = deserialize(body); message.getEnvelope().setBody(body); message.getEnvelope().setDeliveryTag(response.getEnvelope().getDeliveryTag()); message.getEnvelope().setSource(queueName); return message; } return null; }
@Test void demonstrateDurability(DockerRabbitMQ rabbitMQ) throws Exception { String queueName = createQueue(channel); publishAMessage(channel); rabbitMQ.restart(); awaitAtMostOneMinute.until(() -> containerIsRestarted(rabbitMQ)); assertThat(channel.basicGet(queueName, !AUTO_ACK)).isNotNull(); }
@Override public void basicGet(String queue, boolean autoAck, Handler<AsyncResult<JsonObject>> resultHandler) { forChannel(resultHandler, (channel) -> { GetResponse response = channel.basicGet(queue, autoAck); if (response == null) { return null; } else { JsonObject json = new JsonObject(); populate(json, response.getEnvelope()); if (includeProperties) { put("properties", Utils.toJson(response.getProps()), json); } put("body", parse(response.getProps(), response.getBody()), json); Utils.put("messageCount", response.getMessageCount(), json); return json; } }); }
@Test public void testSendAndReceiveInCallback() throws Exception { template.convertAndSend(ROUTE, "message"); final MessagePropertiesConverter messagePropertiesConverter = new DefaultMessagePropertiesConverter(); String result = template.execute(channel -> { // We need noAck=false here for the message to be explicitly // acked GetResponse response = channel.basicGet(ROUTE, false); MessageProperties messageProps = messagePropertiesConverter.toMessageProperties(response.getProps(), response.getEnvelope(), "UTF-8"); // Explicit ack channel.basicAck(response.getEnvelope().getDeliveryTag(), false); return (String) new SimpleMessageConverter().fromMessage(new Message(response.getBody(), messageProps)); }); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }