Example usage for com.rabbitmq.client BasicProperties getReplyTo

List of usage examples for com.rabbitmq.client BasicProperties getReplyTo

Introduction

In this page you can find the example usage for com.rabbitmq.client BasicProperties getReplyTo.

Prototype

public abstract String getReplyTo();

Source Link

Document

Retrieve the value in the replyTo field.

Usage

From source file:org.mule.transport.amqp.internal.domain.AmqpMuleMessageFactory.java

License:Open Source License

private void addBasicProperties(final BasicProperties amqpProperties, final DefaultMuleMessage muleMessage,
        final Map<String, Object> messageProperties) {
    if (amqpProperties == null)
        return;//from ww  w.  j a va  2 s .  c  om

    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_APP_ID, amqpProperties.getAppId(), messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_CONTENT_ENCODING, amqpProperties.getContentEncoding(),
            messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_CONTENT_TYPE, amqpProperties.getContentType(),
            messageProperties);

    final String correlationId = amqpProperties.getCorrelationId();
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_CORRELATION_ID, correlationId, messageProperties);
    putIfNonNull(MuleProperties.MULE_CORRELATION_ID_PROPERTY, correlationId, messageProperties);
    muleMessage.setCorrelationId(correlationId);

    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_DELIVERY_MODE, amqpProperties.getDeliveryMode(),
            messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_EXPIRATION, amqpProperties.getExpiration(), messageProperties);

    final String messageId = amqpProperties.getMessageId();
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_MESSAGE_ID, messageId, messageProperties);
    putIfNonNull(MuleProperties.MULE_MESSAGE_ID_PROPERTY, messageId, messageProperties);
    muleMessage.setUniqueId(messageId == null ? UUID.getUUID() : messageId);

    final String clusterId = amqpProperties.getClusterId();
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_CLUSTER_ID, clusterId, messageProperties);

    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_PRIORITY, amqpProperties.getPriority(), messageProperties);

    final String replyTo = amqpProperties.getReplyTo();
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_REPLY_TO, replyTo, messageProperties);
    muleMessage.setReplyTo(replyTo);

    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_TIMESTAMP, amqpProperties.getTimestamp(), messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_TYPE, amqpProperties.getType(), messageProperties);
    putIfNonNull(AmqpConnector.MESSAGE_PROPERTY_USER_ID, amqpProperties.getUserId(), messageProperties);
}

From source file:org.mule.transport.amqp.internal.domain.AmqpMuleMessageFactoryTestCase.java

License:Open Source License

public static void checkInboundProperties(final AmqpMessage amqpMessage, final MuleMessage muleMessage) {
    assertEquals(amqpMessage.getConsumerTag(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CONSUMER_TAG, PropertyScope.INBOUND));

    final Envelope envelope = amqpMessage.getEnvelope();
    assertEquals(envelope.getDeliveryTag(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_DELIVERY_TAG, PropertyScope.INBOUND));
    assertEquals(envelope.isRedeliver(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_REDELIVER, PropertyScope.INBOUND));
    assertEquals(envelope.getExchange(),
            muleMessage.getProperty(AmqpConnector.EXCHANGE, PropertyScope.INBOUND));
    assertEquals(envelope.getRoutingKey(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_ROUTING_KEY, PropertyScope.INBOUND));

    final BasicProperties amqpProperties = amqpMessage.getProperties();
    assertEquals(amqpProperties.getAppId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_APP_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getContentEncoding(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CONTENT_ENCODING, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getContentType(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CONTENT_TYPE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getClusterId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CLUSTER_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getCorrelationId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_CORRELATION_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getCorrelationId(), muleMessage.getCorrelationId());
    assertEquals(amqpProperties.getDeliveryMode(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_DELIVERY_MODE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getExpiration(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_EXPIRATION, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getMessageId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_MESSAGE_ID, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getMessageId(), muleMessage.getUniqueId());
    assertEquals(amqpProperties.getPriority(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_PRIORITY, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getReplyTo(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_REPLY_TO, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getReplyTo(), muleMessage.getReplyTo());
    assertEquals(amqpProperties.getTimestamp(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_TIMESTAMP, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getType(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_TYPE, PropertyScope.INBOUND));
    assertEquals(amqpProperties.getUserId(),
            muleMessage.getProperty(AmqpConnector.MESSAGE_PROPERTY_USER_ID, PropertyScope.INBOUND));

    for (final Entry<String, Object> header : amqpProperties.getHeaders().entrySet()) {
        assertEquals(header.getValue(), muleMessage.getProperty(header.getKey(), PropertyScope.INBOUND));
    }//from ww  w  .j av a2  s .  com
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java

License:Apache License

private void testNoExistingReplyToOrCorrelationGuts(final boolean standardHeader) throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    Queue replyQueue = new Queue("new.replyTo");
    template.setReplyQueue(replyQueue);//w  w  w.  ja  v  a 2  s  .co m
    if (!standardHeader) {
        template.setCorrelationKey(CORRELATION_HEADER);
    }

    MessageProperties messageProperties = new MessageProperties();
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    final AtomicReference<String> replyTo = new AtomicReference<String>();
    final AtomicReference<String> correlationId = new AtomicReference<String>();
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3];
            replyTo.set(basicProps.getReplyTo());
            if (standardHeader) {
                correlationId.set(basicProps.getCorrelationId());
            } else {
                correlationId.set((String) basicProps.getHeaders().get(CORRELATION_HEADER));
            }
            MessageProperties springProps = new DefaultMessagePropertiesConverter()
                    .toMessageProperties(basicProps, null, "UTF-8");
            Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps);
            template.onMessage(replyMessage);
            return null;
        }
    }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
    Message reply = template.sendAndReceive(message);
    assertNotNull(reply);

    assertNotNull(replyTo.get());
    assertEquals("new.replyTo", replyTo.get());
    assertNotNull(correlationId.get());
    assertNull(reply.getMessageProperties().getReplyTo());
    if (standardHeader) {
        assertNull(reply.getMessageProperties().getCorrelationId());
    } else {
        assertNull(reply.getMessageProperties().getHeaders().get(CORRELATION_HEADER));
    }
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java

License:Apache License

@Test
public void testReplyToOneDeep() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    Queue replyQueue = new Queue("new.replyTo");
    template.setReplyQueue(replyQueue);//from ww  w .  j  a va 2 s  . c  o  m
    template.setReplyTimeout(60000);

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setReplyTo("replyTo1");
    messageProperties.setCorrelationId("saveThis".getBytes());
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    final AtomicReference<String> replyTo = new AtomicReference<String>();
    final AtomicReference<String> correlationId = new AtomicReference<String>();
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3];
            replyTo.set(basicProps.getReplyTo());
            correlationId.set(basicProps.getCorrelationId());
            MessageProperties springProps = new DefaultMessagePropertiesConverter()
                    .toMessageProperties(basicProps, null, "UTF-8");
            Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps);
            template.onMessage(replyMessage);
            return null;
        }
    }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
    Message reply = template.sendAndReceive(message);
    assertNotNull(reply);

    assertNotNull(replyTo.get());
    assertEquals("new.replyTo", replyTo.get());
    assertNotNull(correlationId.get());
    assertFalse("saveThis".equals(correlationId.get()));
    assertEquals("replyTo1", reply.getMessageProperties().getReplyTo());

}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java

License:Apache License

@Test
public void testReplyToThreeDeep() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    Queue replyQueue = new Queue("replyTo2");
    template.setReplyQueue(replyQueue);/*  ww  w.  ja  v  a2s .  c o m*/

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setReplyTo("replyTo1");
    messageProperties.setCorrelationId("a".getBytes());
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    final AtomicInteger count = new AtomicInteger();
    final List<String> nestedReplyTo = new ArrayList<String>();
    final List<String> nestedCorrelation = new ArrayList<String>();
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3];
            nestedReplyTo.add(basicProps.getReplyTo());
            nestedCorrelation.add(basicProps.getCorrelationId());
            MessageProperties springProps = new DefaultMessagePropertiesConverter()
                    .toMessageProperties(basicProps, null, "UTF-8");
            Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps);
            if (count.incrementAndGet() < 2) {
                Message anotherMessage = new Message("Second".getBytes(), springProps);
                template.setReplyQueue(new Queue("replyTo3"));
                replyMessage = template.sendAndReceive(anotherMessage);
                nestedReplyTo.add(replyMessage.getMessageProperties().getReplyTo());
                nestedCorrelation
                        .add(new String(replyMessage.getMessageProperties().getCorrelationId(), "UTF-8"));
            }
            template.onMessage(replyMessage);
            return null;
        }
    }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
    Message reply = template.sendAndReceive(message);
    assertNotNull(reply);

    assertEquals(3, nestedReplyTo.size());
    assertEquals("replyTo2", nestedReplyTo.get(0));
    assertEquals("replyTo3", nestedReplyTo.get(1));
    assertEquals("replyTo2", nestedReplyTo.get(2)); // intermediate reply

    assertEquals("replyTo1", reply.getMessageProperties().getReplyTo());
    assertEquals("a", new String(reply.getMessageProperties().getCorrelationId(), "UTF-8"));

}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java

License:Apache License

@Test
public void testReplyToOneDeepCustomCorrelationKey() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    template.setCorrelationKey(CORRELATION_HEADER);
    Queue replyQueue = new Queue("new.replyTo");
    template.setReplyQueue(replyQueue);/*from   ww  w  . j ava 2  s.c  o m*/

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setReplyTo("replyTo1");
    messageProperties.setCorrelationId("saveThis".getBytes());
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    final AtomicReference<String> replyTo = new AtomicReference<String>();
    final AtomicReference<String> correlationId = new AtomicReference<String>();
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3];
            replyTo.set(basicProps.getReplyTo());
            correlationId.set((String) basicProps.getHeaders().get(CORRELATION_HEADER));

            MessageProperties springProps = new DefaultMessagePropertiesConverter()
                    .toMessageProperties(basicProps, null, "UTF-8");
            Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps);
            template.onMessage(replyMessage);
            return null;
        }
    }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
    Message reply = template.sendAndReceive(message);
    assertNotNull(reply);

    assertNotNull(replyTo.get());
    assertEquals("new.replyTo", replyTo.get());
    assertNotNull(correlationId.get());
    assertEquals("replyTo1", reply.getMessageProperties().getReplyTo());
    assertTrue(!"saveThis".equals(correlationId.get()));
    assertEquals("replyTo1", reply.getMessageProperties().getReplyTo());

}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java

License:Apache License

@Test
public void testReplyToThreeDeepCustomCorrelationKey() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory));
    template.setCorrelationKey(CORRELATION_HEADER);
    Queue replyQueue = new Queue("replyTo2");
    template.setReplyQueue(replyQueue);//from   w ww  .  j ava  2 s .c  om

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setReplyTo("replyTo1");
    messageProperties.setCorrelationId("a".getBytes());
    Message message = new Message("Hello, world!".getBytes(), messageProperties);
    final AtomicInteger count = new AtomicInteger();
    final List<String> nestedReplyTo = new ArrayList<String>();
    final List<String> nestedCorrelation = new ArrayList<String>();
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3];
            nestedReplyTo.add(basicProps.getReplyTo());
            nestedCorrelation.add(basicProps.getCorrelationId());
            MessageProperties springProps = new DefaultMessagePropertiesConverter()
                    .toMessageProperties(basicProps, null, "UTF-8");
            Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps);
            if (count.incrementAndGet() < 2) {
                Message anotherMessage = new Message("Second".getBytes(), springProps);
                template.setReplyQueue(new Queue("replyTo3"));
                replyMessage = template.sendAndReceive(anotherMessage);
                nestedReplyTo.add(replyMessage.getMessageProperties().getReplyTo());
                nestedCorrelation
                        .add((String) replyMessage.getMessageProperties().getHeaders().get(CORRELATION_HEADER));
            }
            template.onMessage(replyMessage);
            return null;
        }
    }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
    Message reply = template.sendAndReceive(message);
    assertNotNull(reply);

    assertEquals(3, nestedReplyTo.size());
    assertEquals("replyTo2", nestedReplyTo.get(0));
    assertEquals("replyTo3", nestedReplyTo.get(1));
    assertEquals("replyTo2", nestedReplyTo.get(2)); //intermediate reply

    assertEquals("replyTo1", reply.getMessageProperties().getReplyTo());
    assertEquals("a", new String(reply.getMessageProperties().getCorrelationId(), "UTF-8"));

}

From source file:org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter.java

License:Apache License

public MessageProperties toMessageProperties(final BasicProperties source, final Envelope envelope,
        final String charset) {
    MessageProperties target = new MessageProperties();
    Map<String, Object> headers = source.getHeaders();
    if (!CollectionUtils.isEmpty(headers)) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof LongString) {
                value = this.convertLongString((LongString) value, charset);
            }//from w ww  . ja  va 2s .c om
            target.setHeader(entry.getKey(), value);
        }
    }
    target.setTimestamp(source.getTimestamp());
    target.setMessageId(source.getMessageId());
    target.setUserId(source.getUserId());
    target.setAppId(source.getAppId());
    target.setClusterId(source.getClusterId());
    target.setType(source.getType());
    Integer deliverMode = source.getDeliveryMode();
    if (deliverMode != null) {
        target.setDeliveryMode(MessageDeliveryMode.fromInt(deliverMode));
    }
    target.setExpiration(source.getExpiration());
    target.setPriority(source.getPriority());
    target.setContentType(source.getContentType());
    target.setContentEncoding(source.getContentEncoding());
    String correlationId = source.getCorrelationId();
    if (correlationId != null) {
        try {
            target.setCorrelationId(source.getCorrelationId().getBytes(charset));
        } catch (UnsupportedEncodingException ex) {
            throw new AmqpUnsupportedEncodingException(ex);
        }
    }
    String replyTo = source.getReplyTo();
    if (replyTo != null) {
        target.setReplyTo(replyTo);
    }
    if (envelope != null) {
        target.setReceivedExchange(envelope.getExchange());
        target.setReceivedRoutingKey(envelope.getRoutingKey());
        target.setRedelivered(envelope.isRedeliver());
        target.setDeliveryTag(envelope.getDeliveryTag());
    }
    return target;
}

From source file:org.springframework.amqp.rabbit.support.RabbitUtils.java

License:Apache License

public static MessageProperties createMessageProperties(final BasicProperties source, final Envelope envelope,
        final String charset) {
    MessageProperties target = new MessageProperties();
    Map<String, Object> headers = source.getHeaders();
    if (!CollectionUtils.isEmpty(headers)) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            target.setHeader(entry.getKey(), entry.getValue());
        }/*ww  w.  ja  va2 s .  com*/
    }
    target.setTimestamp(source.getTimestamp());
    target.setMessageId(source.getMessageId());
    target.setUserId(source.getUserId());
    target.setAppId(source.getAppId());
    target.setClusterId(source.getClusterId());
    target.setType(source.getType());
    Integer deliverMode = source.getDeliveryMode();
    if (deliverMode != null) {
        target.setDeliveryMode(MessageDeliveryMode.fromInt(deliverMode));
    }
    target.setExpiration(source.getExpiration());
    target.setPriority(source.getPriority());
    target.setContentType(source.getContentType());
    target.setContentEncoding(source.getContentEncoding());
    String correlationId = source.getCorrelationId();
    if (correlationId != null) {
        try {
            target.setCorrelationId(source.getCorrelationId().getBytes(charset));
        } catch (UnsupportedEncodingException ex) {
            throw new AmqpUnsupportedEncodingException(ex);
        }
    }
    String replyTo = source.getReplyTo();
    if (replyTo != null) {
        target.setReplyTo(new Address(replyTo));
    }
    if (envelope != null) {
        target.setReceivedExchange(envelope.getExchange());
        target.setReceivedRoutingKey(envelope.getRoutingKey());
        target.setRedelivered(envelope.isRedeliver());
        target.setDeliveryTag(envelope.getDeliveryTag());
    }
    // TODO: what about messageCount?
    return target;
}

From source file:org.trianacode.TrianaCloud.Broker.RPCServer.java

License:Open Source License

public void run() {
    while (keepRunning) {
        try {/*from w w  w.  j  av  a  2s. c om*/
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();

            BasicProperties props = delivery.getProperties();
            BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                    .build();

            byte[] del = delivery.getBody();

            byte[] method = Arrays.copyOfRange(del, 0, 7);
            byte[] message = Arrays.copyOfRange(del, 7, del.length);

            if (Arrays.equals(method, RPCClient.GET_TASK.getBytes())) {
                String[] plugins = (new String(message)).split("\n");

                ///TODO: grab tasks by plugin

                logger.info(" [.] Task Requested");
                Task t = td.getNextPending();
                if (t == null) {
                    t = new Task();
                    t.setNOTASK(true);
                    t.setTimeToWait(10);
                }
                channel.basicPublish("", props.getReplyTo(), replyProps, TaskOps.encodeTask(t));
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            } else if (Arrays.equals(method, RPCClient.RETURN_TASK.getBytes())) {
                Task rettask = TaskOps.decodeTask(message);

                if (rettask.getNOTASK()) {
                    byte[] ret = new byte[0];
                    channel.basicPublish("", props.getReplyTo(), replyProps, ret);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                } else {
                    Task task = td.getByUUID(rettask.getUUID());

                    task.setReturnCode(rettask.getReturnCode());
                    task.setReturnData(rettask.getReturnData());
                    task.setReturnDataMD5(rettask.getReturnDataMD5());
                    task.setReturnDataType(rettask.getReturnDataType());
                    task.setState(Task.COMPLETE);

                    td.save(task);

                    logger.info(" [x] Task " + task.getUUID() + " Complete");

                    byte[] ret = new byte[1];
                    ret[0] = 0;
                    channel.basicPublish("", props.getReplyTo(), replyProps, ret);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                }

            } //else if some other rpc call/method/thing
        } catch (ShutdownSignalException e) {
            System.out.println("Shutting down the RPC Server");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error", e);
        }
    }

}