private Map<String, String> buildAttributes(final BasicProperties properties) { final Map<String, String> attributes = new HashMap<>(); addAttribute(attributes, ATTRIBUTES_PREFIX + "appId", properties.getAppId()); addAttribute(attributes, ATTRIBUTES_PREFIX + "contentEncoding", properties.getContentEncoding()); addAttribute(attributes, ATTRIBUTES_PREFIX + "contentType", properties.getContentType()); addAttribute(attributes, ATTRIBUTES_PREFIX + "headers", properties.getHeaders()); addAttribute(attributes, ATTRIBUTES_PREFIX + "deliveryMode", properties.getDeliveryMode()); addAttribute(attributes, ATTRIBUTES_PREFIX + "priority", properties.getPriority()); addAttribute(attributes, ATTRIBUTES_PREFIX + "correlationId", properties.getCorrelationId()); addAttribute(attributes, ATTRIBUTES_PREFIX + "replyTo", properties.getReplyTo()); addAttribute(attributes, ATTRIBUTES_PREFIX + "expiration", properties.getExpiration()); addAttribute(attributes, ATTRIBUTES_PREFIX + "messageId", properties.getMessageId()); addAttribute(attributes, ATTRIBUTES_PREFIX + "timestamp", properties.getTimestamp() == null ? null : properties.getTimestamp().getTime()); addAttribute(attributes, ATTRIBUTES_PREFIX + "type", properties.getType()); addAttribute(attributes, ATTRIBUTES_PREFIX + "userId", properties.getUserId()); addAttribute(attributes, ATTRIBUTES_PREFIX + "clusterId", properties.getClusterId()); return attributes; }
@Override public void handleDelivery(String s, com.rabbitmq.client.Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException { try { Map<String, Object> headers = basicProperties.getHeaders(); if (headers == null || !headers.containsKey(CloudBus.HEADER_MESSAGE_META_DATA)) { return; } LongString metaData = (LongString) headers.get(CloudBus.HEADER_MESSAGE_META_DATA); Map m = JSONObjectUtil.toObject(new String(metaData.getBytes()), LinkedHashMap.class); trackMessage((MessageMetaData) JSONObjectUtil.rehashObject(m, metaDataClassCache.get(m.get("className")))); } catch (Throwable t) { logger.warn("unhandled throwable", t); } }
if (basicProperties.getHeaders() != null && basicProperties.getHeaders().containsKey(AMQP_PROPERTY_HEADER__COMPRESSED)) { data = Compresser.inflate(binary); } else {
protected String extractCorrelationIdFromHeaders(AMQP.BasicProperties properties) throws UnsupportedEncodingException { String decodedCorrelationId=null; if(properties.getHeaders() != null) { try { Object rawCorrelationId = properties.getHeaders().get(CORRELATION_ID_KEY); if(rawCorrelationId==null){ log.info("no correlationId provided in headers"); return null; } byte[] correlationIdAsByteArray = ((LongString) rawCorrelationId).getBytes(); decodedCorrelationId = new String(correlationIdAsByteArray, "UTF-8"); } catch(UnsupportedEncodingException e){ log.warn("extracted correlationId, but unable to decode it",e); } } return decodedCorrelationId; }
public static Message unbox(AMQP.BasicProperties properties, Message msg) { //common properties msg.setCorrelationId(properties.getCorrelationId()); msg.setHeaders(properties.getHeaders()); msg.setTimestamp(properties.getTimestamp().getTime()); String msgIdStr = properties.getMessageId(); if (msgIdStr != null && !msgIdStr.isEmpty()) msg.setMessageId(Long.parseLong(msgIdStr)); else logger.error("[unbox] illegal message id (can not be null) "); msg.setContentEncoding(properties.getContentEncoding()); msg.setContentType(properties.getContentType()); msg.setAppId(properties.getAppId()); msg.setReplyTo(properties.getReplyTo()); return msg; }
private Object isSerializeHeaderEnabled(final AMQP.BasicProperties properties) { return properties.getHeaders().get(RabbitMQEndpoint.SERIALIZE_HEADER); }
private boolean hasHeaders(final AMQP.BasicProperties properties) { return properties != null && properties.getHeaders() != null; }
@Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { setMQHeader(topic, properties.getHeaders()); String message = new String(body, StandardCharsets.UTF_8); try { consumer.accept(message); } catch (RuntimeException e) { throw e; } finally { channel.basicAck(envelope.getDeliveryTag(), false); } } };
private void populateMessageHeadersFromRabbitMQHeaders(final Message message, final AMQP.BasicProperties properties) { Map<String, Object> headers = properties.getHeaders(); if (headers != null) { for (Map.Entry<String, Object> entry : headers.entrySet()) { // Convert LongStrings to String. if (entry.getValue() instanceof LongString) { message.setHeader(entry.getKey(), entry.getValue().toString()); } else { message.setHeader(entry.getKey(), entry.getValue()); } } } }
private static Map<String, Object> headers(final BasicProperties props, Envelope envelope) { Map<String, Object> result = props.getHeaders().entrySet().stream().collect(toMap(Entry::getKey, MessageFactory::headerValue)); result.put("DeliveryTag", envelope.getDeliveryTag()); result.put("Exchange", envelope.getExchange()); result.put("RoutingKey", envelope.getRoutingKey()); result.put("Redeliver", envelope.isRedeliver()); return result; }
@Override public void handleReturn(final int replyCode, final String replyText, final String exchange, final String routingKey, final BasicProperties properties, final byte[] body) throws IOException { LOGGER.warn( "Message {} publication in {}:{} failed ({}): {}", properties.getHeaders().get(BROKER_CONTROLLER_MESSAGE), exchange, routingKey, replyCode, replyText); }
private void verifyHeader(final BasicProperties properties) throws IOException { final Object header = properties.getHeaders().get(HttpHeaders.CONTENT_TYPE); checkNotNull(header,"No %s header defined",HttpHeaders.CONTENT_TYPE); checkArgument(Notifications.MIME.equals(header.toString()),"Unsupported %s header (%s)",HttpHeaders.CONTENT_TYPE,header); }
public static void main(String[] args) throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("order.q", true, consumer); while (true) { QueueingConsumer.Delivery msg = consumer.nextDelivery(); String orderType = msg.getProperties().getHeaders().get("type").toString(); String orderItem = new String(msg.getBody()); processor.get(orderType).processOrder(orderItem); Thread.sleep(2000); } } }
@Test public void testFromUnsupportedValue() { MessageProperties messageProperties = new MessageProperties(); messageProperties.setHeader("unsupported", new Object()); BasicProperties basicProps = messagePropertiesConverter.fromMessageProperties(messageProperties, "UTF-8"); assertTrue("Unsupported value not converted to String", basicProps.getHeaders().get("unsupported") instanceof String); }
@Test @SuppressWarnings("unchecked") public void testFromUnsupportedValueInMap() { MessageProperties messageProperties = new MessageProperties(); Map<String, Object> mapWithUnsupportedValue = new HashMap<String, Object>(); mapWithUnsupportedValue.put("unsupported", new Object()); messageProperties.setHeader("map", mapWithUnsupportedValue); BasicProperties basicProps = messagePropertiesConverter.fromMessageProperties(messageProperties, "UTF-8"); assertTrue("Unsupported value nested in Map not converted to String", ((Map<String, Object>) basicProps.getHeaders().get("map")).get("unsupported") instanceof String); }
@Test @SuppressWarnings("unchecked") public void testFromUnsupportedValueDeepInList() { MessageProperties messageProperties = new MessageProperties(); List<List<Object>> listWithUnsupportedValue = Arrays.asList(Arrays.asList(new Object())); messageProperties.setHeader("list", listWithUnsupportedValue); BasicProperties basicProps = messagePropertiesConverter.fromMessageProperties(messageProperties, "UTF-8"); assertTrue("Unsupported value deeply nested in List not converted to String", ((List<Object>) ((List<?>) basicProps.getHeaders().get("list")).get(0)).get(0) instanceof String); }
@Test public void testFromUnsupportedValueInList() { MessageProperties messageProperties = new MessageProperties(); List<Object> listWithUnsupportedValue = Arrays.asList(new Object()); messageProperties.setHeader("list", listWithUnsupportedValue); BasicProperties basicProps = messagePropertiesConverter.fromMessageProperties(messageProperties, "UTF-8"); assertTrue("Unsupported value nested in List not converted to String", ((List<?>) basicProps.getHeaders().get("list")).get(0) instanceof String); }
private void verifyHeader(final BasicProperties properties) throws IOException { final Object header = properties.getHeaders().get(HttpHeaders.CONTENT_TYPE); checkNotNull(header,"No %s header defined",HttpHeaders.CONTENT_TYPE); checkArgument(Notifications.MIME.equals(header.toString()),"Unsupported %s header (%s)",HttpHeaders.CONTENT_TYPE,header); }
@Override public final void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) { getEventQueue().add(getDeliverEvent(envelope, body, properties.getHeaders() == null ? Collections.emptyMap() : properties.getHeaders())); }
public static MessageProperties valueOf(BasicProperties bp) { MessageProperties mp = new MessageProperties(); mp.setAppId(bp.getAppId()); mp.setClusterId(bp.getClusterId()); mp.setContentEncoding(bp.getContentEncoding()); mp.setContentType(bp.getContentType()); mp.setCorrelationId(bp.getCorrelationId()); mp.setDeliveryMode(bp.getDeliveryMode()); mp.setExpiration(bp.getExpiration()); mp.setHeaders(bp.getHeaders()); mp.setMessageId(bp.getMessageId()); mp.setPriority(bp.getPriority()); mp.setReplyTo(bp.getReplyTo()); mp.setTimestamp(bp.getTimestamp()); mp.setType(bp.getType()); mp.setUserId(bp.getUserId()); return mp; }