List of usage examples for com.rabbitmq.client GetResponse getEnvelope
public Envelope getEnvelope()
From source file:org.mule.transport.rmq.RmqMessageRequester.java
License:Open Source License
protected MuleMessage doRequest(long timeout) throws Exception { GetResponse response = channel.basicGet(queue, noAck); QueueingConsumer.Delivery delivery = new Delivery(response.getEnvelope(), response.getProps(), response.getBody());// w w w . j av a2s . co m return new DefaultMuleMessage(connector.getMessageAdapter(delivery)); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplate.java
License:Apache License
@Override public Message receive(final String queueName) { return execute(new ChannelCallback<Message>() { @Override/*from w ww .j a va 2 s . c om*/ public Message doInRabbit(Channel channel) throws IOException { GetResponse response = channel.basicGet(queueName, !isChannelTransacted()); // Response can be null is the case that there is no message on the queue. if (response != null) { long deliveryTag = response.getEnvelope().getDeliveryTag(); if (isChannelLocallyTransacted(channel)) { channel.basicAck(deliveryTag, false); channel.txCommit(); } else if (isChannelTransacted()) { // Not locally transacted but it is transacted so it // could be synchronized with an external transaction ConnectionFactoryUtils.registerDeliveryTag(getConnectionFactory(), channel, deliveryTag); } return RabbitTemplate.this.buildMessageFromResponse(response); } return null; } }); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplate.java
License:Apache License
@SuppressWarnings("unchecked") private <R, S> boolean doReceiveAndReply(final String queueName, final ReceiveAndReplyCallback<R, S> callback, final ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException { return this.execute(new ChannelCallback<Boolean>() { @Override//w ww .j a va2 s . c o m public Boolean doInRabbit(Channel channel) throws Exception { boolean channelTransacted = RabbitTemplate.this.isChannelTransacted(); GetResponse response = channel.basicGet(queueName, !channelTransacted); // Response can be null in the case that there is no message on the queue. if (response != null) { long deliveryTag = response.getEnvelope().getDeliveryTag(); boolean channelLocallyTransacted = RabbitTemplate.this.isChannelLocallyTransacted(channel); if (channelLocallyTransacted) { channel.basicAck(deliveryTag, false); } else if (channelTransacted) { // Not locally transacted but it is transacted so it could be synchronized with an external transaction ConnectionFactoryUtils.registerDeliveryTag(RabbitTemplate.this.getConnectionFactory(), channel, deliveryTag); } Message receiveMessage = RabbitTemplate.this.buildMessageFromResponse(response); Object receive = receiveMessage; if (!(ReceiveAndReplyMessageCallback.class.isAssignableFrom(callback.getClass()))) { receive = RabbitTemplate.this.getRequiredMessageConverter().fromMessage(receiveMessage); } S reply; try { reply = callback.handle((R) receive); } catch (ClassCastException e) { StackTraceElement[] trace = e.getStackTrace(); if (trace[0].getMethodName().equals("handle") && trace[1].getFileName().equals("RabbitTemplate.java")) { throw new IllegalArgumentException("ReceiveAndReplyCallback '" + callback + "' can't handle received object '" + receive + "'", e); } else { throw e; } } if (reply != null) { Address replyTo = replyToAddressCallback.getReplyToAddress(receiveMessage, reply); Message replyMessage = RabbitTemplate.this.convertMessageIfNecessary(reply); MessageProperties receiveMessageProperties = receiveMessage.getMessageProperties(); MessageProperties replyMessageProperties = replyMessage.getMessageProperties(); Object correlation = RabbitTemplate.this.correlationKey == null ? receiveMessageProperties.getCorrelationId() : receiveMessageProperties.getHeaders().get(RabbitTemplate.this.correlationKey); if (RabbitTemplate.this.correlationKey == null || correlation == null) { // using standard correlationId property if (correlation == null) { String messageId = receiveMessageProperties.getMessageId(); if (messageId != null) { correlation = messageId.getBytes(RabbitTemplate.this.encoding); } } replyMessageProperties.setCorrelationId((byte[]) correlation); } else { replyMessageProperties.setHeader(RabbitTemplate.this.correlationKey, correlation); } // 'doSend()' takes care about 'channel.txCommit()'. RabbitTemplate.this.doSend(channel, replyTo.getExchangeName(), replyTo.getRoutingKey(), replyMessage, null); } else if (channelLocallyTransacted) { channel.txCommit(); } return true; } return false; } }); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
License:Apache License
@Test public void testSendAndReceiveInCallback() throws Exception { template.convertAndSend(ROUTE, "message"); final MessagePropertiesConverter messagePropertiesConverter = new DefaultMessagePropertiesConverter(); String result = template.execute(new ChannelCallback<String>() { @Override/*w w w . j a va2 s.c o m*/ public String doInRabbit(Channel channel) throws Exception { // We need noAck=false here for the message to be expicitly // 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); }
From source file:org.springframework.integration.amqp.inbound.AmqpMessageSource.java
License:Apache License
@Override protected AbstractIntegrationMessageBuilder<Object> doReceive() { Connection connection = this.connectionFactory.createConnection(); // NOSONAR - RabbitUtils Channel channel = connection.createChannel(this.transacted); try {/*from w ww .j a v a2 s . c o m*/ GetResponse resp = channel.basicGet(this.queue, false); if (resp == null) { RabbitUtils.closeChannel(channel); RabbitUtils.closeConnection(connection); return null; } AcknowledgmentCallback callback = this.ackCallbackFactory .createCallback(new AmqpAckInfo(connection, channel, this.transacted, resp)); MessageProperties messageProperties = this.propertiesConverter.toMessageProperties(resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name()); messageProperties.setConsumerQueue(this.queue); Map<String, Object> headers = this.headerMapper.toHeadersFromRequest(messageProperties); org.springframework.amqp.core.Message amqpMessage = new org.springframework.amqp.core.Message( resp.getBody(), messageProperties); Object payload; if (this.batchingStrategy.canDebatch(messageProperties)) { List<Object> payloads = new ArrayList<>(); this.batchingStrategy.deBatch(amqpMessage, fragment -> payloads.add(this.messageConverter.fromMessage(fragment))); payload = payloads; } else { payload = this.messageConverter.fromMessage(amqpMessage); } AbstractIntegrationMessageBuilder<Object> builder = getMessageBuilderFactory().withPayload(payload) .copyHeaders(headers) .setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback); if (this.rawMessageHeader) { builder.setHeader(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, amqpMessage); builder.setHeader(IntegrationMessageHeaderAccessor.SOURCE_DATA, amqpMessage); } return builder; } catch (IOException e) { RabbitUtils.closeChannel(channel); RabbitUtils.closeConnection(connection); throw RabbitExceptionTranslator.convertRabbitAccessException(e); } }
From source file:taucalcworker.TaucalcWorker.java
/** * @param args the command line arguments * @throws java.io.IOException// w w w . j a va2 s. c o m * @throws java.util.concurrent.TimeoutException */ public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(WORKQUEUENAME, false, false, false, null); channel.queueDeclare(RESULTQUEUENAME, false, false, false, null); channel.basicQos(1); byte[] inputbyte = { 0, 0, 0, 0 }; String input = ""; do { if (System.in.available() > 0) { System.in.read(inputbyte); input = new String(inputbyte); } GetResponse response = channel.basicGet(WORKQUEUENAME, false); if (response != null) { long deliverytag = response.getEnvelope().getDeliveryTag(); byte[] body = response.getBody(); int tries = ByteBuffer.wrap(body).getInt(); System.out.println("Task received: " + tries); int success = 0; for (int i = 0; i < tries; i++) { double x = Math.random(); double y = Math.random(); if (x * x + y * y <= 1) { success++; } } System.out.println("success: " + success + " out of " + tries); double tau = ((double) success / tries) * 8; System.out.println("Tau = " + tau); byte[] resultbytes = new byte[8]; ByteBuffer.wrap(resultbytes).putDouble(tau); channel.basicPublish("", RESULTQUEUENAME, null, resultbytes); channel.basicAck(deliverytag, false); } } while (!input.equals("stop")); channel.close(); connection.close(); System.out.println("You stopped the program."); }