@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); final BasicProperties props = new BasicProperties().builder() .contentType("application/ld+json").contentEncoding("UTF-8").build(); service.serialize(event).ifPresent(message -> { try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, props, message.getBytes()); } catch (final IOException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); final BasicProperties props = new BasicProperties().builder() .contentType("application/ld+json").contentEncoding("UTF-8").build(); service.serialize(event).ifPresent(message -> { try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, props, message.getBytes()); } catch (final IOException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
public static AMQP.BasicProperties fromJson( JsonObject json ) { if ( json == null ) { return new AMQP.BasicProperties(); } return new AMQP.BasicProperties.Builder() .contentType(json.getString("contentType")) .contentEncoding(json.getString("contentEncoding")) .headers(asMap(json.getJsonObject("headers"))) .deliveryMode(json.getInteger("deliveryMode")) .priority(json.getInteger("priority")) .correlationId(json.getString("correlationId")) .replyTo(json.getString("replyTo")) .expiration(json.getString("expiration")) .messageId(json.getString("messageId")) .timestamp(parseDate(json.getString("timestamp"))) .type(json.getString("type")) .userId(json.getString("userId")) .appId(json.getString("appId")) .clusterId(json.getString("clusterId")).build(); }
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().contentType("text/plain").contentEncoding("UTF-8").build();
builder.contentEncoding(toStr(message.properties().get(ContentEncoding.name())));
@Test public void testMessageOrderingWhenConsuming(TestContext ctx) throws IOException { String queueName = "message_ordering_test"; String address = queueName + ".address"; int count = 1000; List<String> sendingOrder = IntStream.range(1, count).boxed().map(Object::toString).collect(Collectors.toList()); // set up queue AMQP.Queue.DeclareOk ok = channel.queueDeclare(queueName, false, false, true, null); ctx.assertNotNull(ok.getQueue()); AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().contentType("text/plain").contentEncoding("UTF-8").build(); // send messages for (String msg : sendingOrder) channel.basicPublish("", queueName, properties, msg.getBytes("UTF-8")); List<String> receivedOrder = Collections.synchronizedList(new ArrayList<>()); Async async = ctx.async(sendingOrder.size()); vertx.eventBus().consumer(address, msg -> { ctx.assertNotNull(msg); JsonObject json = (JsonObject) msg.body(); ctx.assertNotNull(json); String body = json.getString("body"); ctx.assertNotNull(body); receivedOrder.add(body); async.countDown(); }); client.basicConsume(queueName, address, ctx.asyncAssertSuccess()); async.awaitSuccess(15000); for (int i = 0; i < sendingOrder.size(); i++) { ctx.assertTrue(sendingOrder.get(i).equals(receivedOrder.get(i))); } }
propertiesBuilder.contentEncoding("gzip"); propertiesBuilder.messageId(messageid); propertiesBuilder.timestamp(new Date(message.getJavaTimestamp()));
propertiesBuilder.contentEncoding("gzip"); propertiesBuilder.messageId(messageid); propertiesBuilder.timestamp(new Date(message.getJavaTimestamp()));
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().contentType("text/plain").contentEncoding("UTF-8").build();
@Test public void testMessageOrderingWhenConsuming(TestContext ctx) throws IOException { String queueName = "message_ordering_test"; String address = queueName + ".address"; int count = 1000; List<String> sendingOrder = IntStream.range(1, count).boxed().map(Object::toString).collect(Collectors.toList()); // set up queue AMQP.Queue.DeclareOk ok = channel.queueDeclare(queueName, false, false, true, null); ctx.assertNotNull(ok.getQueue()); AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().contentType("text/plain").contentEncoding("UTF-8").build(); // send messages for (String msg : sendingOrder) channel.basicPublish("", queueName, properties, msg.getBytes("UTF-8")); List<String> receivedOrder = Collections.synchronizedList(new ArrayList<>()); Async async = ctx.async(sendingOrder.size()); vertx.eventBus().consumer(address, msg -> { ctx.assertNotNull(msg); JsonObject json = (JsonObject) msg.body(); ctx.assertNotNull(json); String body = json.getString("body"); ctx.assertNotNull(body); receivedOrder.add(body); async.countDown(); }); client.basicConsume(queueName, address, ctx.asyncAssertSuccess()); async.awaitSuccess(15000); for (int i = 0; i < sendingOrder.size(); i++) { ctx.assertTrue(sendingOrder.get(i).equals(receivedOrder.get(i))); } }
@Override public BasicProperties fromMessageProperties(final MessageProperties source, final String charset) { BasicProperties.Builder target = new BasicProperties.Builder(); target.headers(this.convertHeadersIfNecessary(source.getHeaders())) .timestamp(source.getTimestamp()) .messageId(source.getMessageId()) .userId(source.getUserId()) .appId(source.getAppId()) .clusterId(source.getClusterId()) .type(source.getType()); MessageDeliveryMode deliveryMode = source.getDeliveryMode(); if (deliveryMode != null) { target.deliveryMode(MessageDeliveryMode.toInt(deliveryMode)); } target.expiration(source.getExpiration()) .priority(source.getPriority()) .contentType(source.getContentType()) .contentEncoding(source.getContentEncoding()); String correlationId = source.getCorrelationId(); if (StringUtils.hasText(correlationId)) { target.correlationId(correlationId); } String replyTo = source.getReplyTo(); if (replyTo != null) { target.replyTo(replyTo); } return target.build(); }
@Override public BasicProperties fromMessageProperties(final MessageProperties source, final String charset) { BasicProperties.Builder target = new BasicProperties.Builder(); target.headers(this.convertHeadersIfNecessary(source.getHeaders())) .timestamp(source.getTimestamp()) .messageId(source.getMessageId()) .userId(source.getUserId()) .appId(source.getAppId()) .clusterId(source.getClusterId()) .type(source.getType()); MessageDeliveryMode deliveryMode = source.getDeliveryMode(); if (deliveryMode != null) { target.deliveryMode(MessageDeliveryMode.toInt(deliveryMode)); } target.expiration(source.getExpiration()) .priority(source.getPriority()) .contentType(source.getContentType()) .contentEncoding(source.getContentEncoding()); String correlationId = source.getCorrelationId(); if (StringUtils.hasText(correlationId)) { target.correlationId(correlationId); } String replyTo = source.getReplyTo(); if (replyTo != null) { target.replyTo(replyTo); } return target.build(); }
private void sendMessageActual(Message.MessageForSending message) { reinitIfNecessary(); if (channel == null) throw new ReportedFailedException("No connection to RabbitMQ"); try { AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .contentType(message.getContentType()) .contentEncoding(message.getContentEncoding()) .deliveryMode((message.isPersistent()) ? 2 : 1) .headers(message.getHeaders()) .build(); channel.basicPublish(message.getExchangeName(), message.getRoutingKey(), properties, message.getBody()); } catch (AlreadyClosedException ace) { logger.error("already closed exception while attempting to send message", ace); reset(); throw new ReportedFailedException(ace); } catch (IOException ioe) { logger.error("io exception while attempting to send message", ioe); reset(); throw new ReportedFailedException(ioe); } catch (Exception e) { logger.warn("Unexpected error while sending message. Backing off for a bit before trying again (to allow time for recovery)", e); try { Thread.sleep(1000); } catch (InterruptedException ie) { } } }
/** * Build and populate the AMQP.BasicProperties using the RabbitMQMessage * * @param message the RabbitMQMessage to be used to get the properties * @return AMQP.BasicProperties object */ private AMQP.BasicProperties.Builder buildBasicProperties(RabbitMQMessage message) { AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder(); builder.messageId(message.getMessageId()); builder.contentType(message.getContentType()); builder.replyTo(message.getReplyTo()); builder.correlationId(message.getCorrelationId()); builder.contentEncoding(message.getContentEncoding()); Map<String, Object> headers = message.getHeaders(); if (message.getSoapAction() != null) { headers.put(RabbitMQConstants.SOAP_ACTION, message.getSoapAction()); } builder.headers(headers); return builder; }
public static AMQP.BasicProperties box(Message msg) { AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder(); return builder.messageId(String.valueOf(msg.getMessageId())) .appId(msg.getAppId()) .clusterId(msg.getClusterId()) .contentEncoding(msg.getContentEncoding()) .contentType(msg.getContentType()) .correlationId(msg.getCorrelationId()) .deliveryMode((int) msg.getDeliveryMode()) .expiration(msg.getExpiration()) .headers(msg.getHeaders()) .priority((int) msg.getPriority()) .replyTo(msg.getReplyTo()) .timestamp(new Date(msg.getTimestamp())) .type(msg.getType()) .userId(msg.getUserId()) .build(); }
AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder(); propsBuilder.type(typeName); if (compress) { propsBuilder.contentEncoding("zip"); } BasicProperties props = propsBuilder.build(); channel.basicPublish(targetExchange, "", true, props, data); // and receiving works like this: Delivery delivery = consumer.nextDelivery(); byte[] data = delivery.getBody(); BasicProperties props = delivery.getProperties(); String typeName = props.getType(); String replyToServerId = props.getReplyTo(); String contentEncoding = props.getContentEncoding();
String setupQueue(TestContext ctx, Set<String> messages, String contentType) throws Exception { String queue = randomAlphaString(10); AMQP.Queue.DeclareOk ok = channel.queueDeclare(queue, false, false, true, null); ctx.assertNotNull(ok.getQueue()); AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .contentType(contentType).contentEncoding("UTF-8").build(); if (messages != null) { for (String msg : messages) { channel.basicPublish("", queue, properties, msg.getBytes("UTF-8")); } } return queue; }
String setupQueue(TestContext ctx, Set<String> messages, String contentType) throws Exception { String queue = randomAlphaString(10); AMQP.Queue.DeclareOk ok = channel.queueDeclare(queue, false, false, true, null); ctx.assertNotNull(ok.getQueue()); AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .contentType(contentType).contentEncoding("UTF-8").build(); if (messages != null) { for (String msg : messages) { channel.basicPublish("", queue, properties, msg.getBytes("UTF-8")); } } return queue; }
/** * Sets the content charset encoding of this message. * * @param charset The charset encoding * @return The modified message */ public Message contentEncoding(String charset) { basicProperties = basicProperties.builder() .contentEncoding(charset) .build(); return this; }
public BasicProperties toBasicProperties() { BasicProperties.Builder builder = new BasicProperties.Builder(); return builder.appId(appId).clusterId(clusterId).contentEncoding(contentEncoding).contentType(contentType).correlationId(correlationId) .deliveryMode(deliveryMode).expiration(expiration).headers(headers).messageId(messageId).priority(priority).replyTo(replyTo) .timestamp(timestamp).type(type).userId(userId).build(); }