org.springframework.amqp.core.MessageBuilder Java Examples

The following examples show how to use org.springframework.amqp.core.MessageBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Issue178ListenerConfiguration.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
@Bean
MessageListener exampleListener(final RabbitTemplate rabbitTemplate) {
	return new MessageListener() {
		public void onMessage(Message message) {
			System.out.println("received: " + message);
			try {
				String payload = new ObjectMapper().writeValueAsString(new MyPojo(
						"992e46d8-ab05-4a26-a740-6ef7b0daeab3", "CREATED"));
				Message outputMessage = MessageBuilder.withBody(payload.getBytes())
						.build();
				rabbitTemplate.send(issue178OutputExchange().getName(), "routingkey",
						outputMessage);
			}
			catch (JsonProcessingException e) {
				throw new RuntimeException(e);
			}
		}
	};
}
 
Example #2
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void continues_parent_trace() throws Throwable {
  MessageProperties props = new MessageProperties();
  props.setHeader("X-B3-TraceId", TRACE_ID);
  props.setHeader("X-B3-SpanId", SPAN_ID);
  props.setHeader("X-B3-ParentSpanId", PARENT_ID);
  props.setHeader("X-B3-Sampled", SAMPLED);

  Message message = MessageBuilder.withBody(new byte[0]).andProperties(props).build();
  onMessageConsumed(message);

  // cleared the headers to later work doesn't try to use the old parent
  assertThat(message.getMessageProperties().getHeaders()).isEmpty();

  assertThat(spans)
    .filteredOn(span -> span.kind() == CONSUMER)
    .extracting(MutableSpan::parentId)
    .contains(SPAN_ID);
}
 
Example #3
Source File: RabbitTemplateMessageQueue.java    From elasticactors with Apache License 2.0 6 votes vote down vote up
/**
 * As RabbitTemplate is asynchronous, it's recommended to set the channel as transacted or use
 * publisher confirmations in order to make sure that the message was indeed published. Using
 * transacted channel will make this method block until the broker has confirmed the publishing.
 * Publisher confirmations won't block, but you have to do the checks yourself.
 */
@Override
public boolean offer(InternalMessage message) {
    try {
        rabbitTemplate.send(
                exchange,
                routingKey,
                MessageBuilder.withBody(message.toByteArray())
                        .andProperties(createProps(message))
                        .build());
    } catch (Exception e) {
        throw new MessageDeliveryException(
                "Unexpected exception while sending message using RabbitTemplate",
                e,
                false);
    }
    return true;
}
 
Example #4
Source File: TracingMessagePostProcessorTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void should_add_b3_single_header_to_message() {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  Message postProcessMessage = tracingMessagePostProcessor.postProcessMessage(message);

  assertThat(postProcessMessage.getMessageProperties().getHeaders())
    .containsOnlyKeys("b3");
  assertThat(postProcessMessage.getMessageProperties().getHeaders().get("b3").toString())
    .matches("^[0-9a-f]{16}-[0-9a-f]{16}-1$");
}
 
Example #5
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void reports_span_if_consume_fails_with_no_message() throws Throwable {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  RuntimeException error = new RuntimeException("Test exception");
  onMessageConsumeFailed(message, error);

  assertThat(spans)
    .extracting(MutableSpan::kind)
    .containsExactly(CONSUMER, null);

  assertThat(spans)
    .filteredOn(span -> span.kind() == null)
    .extracting(MutableSpan::error)
    .containsExactly(error);
}
 
Example #6
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void reports_span_if_consume_fails() throws Throwable {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  RuntimeException error = new RuntimeException("Test exception");
  onMessageConsumeFailed(message, error);

  assertThat(spans)
    .extracting(MutableSpan::kind)
    .containsExactly(CONSUMER, null);

  assertThat(spans)
    .filteredOn(span -> span.kind() == null)
    .extracting(MutableSpan::error)
    .containsExactly(error);
}
 
Example #7
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void retains_baggage_headers() throws Throwable {
  MessageProperties props = new MessageProperties();
  props.setHeader("b3", TRACE_ID + "-" + SPAN_ID + "-" + SAMPLED);
  props.setHeader(BAGGAGE_FIELD_KEY, "");

  Message message = MessageBuilder.withBody(new byte[0]).andProperties(props).build();
  onMessageConsumed(message);

  assertThat(message.getMessageProperties().getHeaders())
    .hasSize(1) // clears b3
    .containsEntry(BAGGAGE_FIELD_KEY, "");
}
 
Example #8
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void continues_parent_trace_single_header() throws Throwable {
  MessageProperties props = new MessageProperties();
  props.setHeader("b3", TRACE_ID + "-" + SPAN_ID + "-" + SAMPLED);

  Message message = MessageBuilder.withBody(new byte[0]).andProperties(props).build();
  onMessageConsumed(message);

  // cleared the headers to later work doesn't try to use the old parent
  assertThat(message.getMessageProperties().getHeaders()).isEmpty();

  assertThat(spans)
    .filteredOn(span -> span.kind() == CONSUMER)
    .extracting(MutableSpan::parentId)
    .contains(SPAN_ID);
}
 
Example #9
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void consumer_span_starts_before_listener() throws Throwable {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  onMessageConsumed(message);

  // make sure one before the other
  assertThat(spans.get(0).startTimestamp())
    .isLessThan(spans.get(1).startTimestamp());

  // make sure they finished
  assertThat(spans.get(0).finishTimestamp())
    .isPositive();
  assertThat(spans.get(1).finishTimestamp())
    .isPositive();
}
 
Example #10
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void tags_consumer_span_but_not_listener() throws Throwable {
  MessageProperties properties = new MessageProperties();
  properties.setConsumerQueue("foo");

  Message message = MessageBuilder.withBody(new byte[0]).andProperties(properties).build();
  onMessageConsumed(message);

  assertThat(spans.get(0).tags())
    .containsExactly(entry("rabbit.queue", "foo"));
  assertThat(spans.get(1).tags())
    .isEmpty();
}
 
Example #11
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void consumer_has_remote_service_name() throws Throwable {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  onMessageConsumed(message);

  assertThat(spans)
    .extracting(MutableSpan::remoteServiceName)
    .containsExactly("my-exchange", null);
}
 
Example #12
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void consumer_and_listener_have_names() throws Throwable {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  onMessageConsumed(message);

  assertThat(spans)
    .extracting(MutableSpan::name)
    .containsExactly("next-message", "on-message");
}
 
Example #13
Source File: TracingRabbitListenerAdviceTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void starts_new_trace_if_none_exists() throws Throwable {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  onMessageConsumed(message);

  assertThat(spans)
    .extracting(MutableSpan::kind)
    .containsExactly(CONSUMER, null);
}
 
Example #14
Source File: TracingMessagePostProcessorTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void should_set_remote_service() {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  tracingMessagePostProcessor.postProcessMessage(message);

  assertThat(spans.get(0).remoteServiceName())
    .isEqualTo("my-exchange");
}
 
Example #15
Source File: MQTest.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
@Test
public void send() {
    Message message = MessageBuilder.withBody("body content".getBytes())
            .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
            .setMessageId("1")
            .setHeader("header", "header")
            .build();
    amqpTemplate.send("Queue1", message);
    amqpTemplate.convertAndSend("Queue1", "body content");

}
 
Example #16
Source File: TracingMessagePostProcessorTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void should_prefer_current_span() {
  // Will be either a bug, or a missing processor stage which can result in an old span in headers
  Message message = MessageBuilder.withBody(new byte[0]).build();
  message.getMessageProperties().setHeader("b3", B3SingleFormat.writeB3SingleFormat(grandparent));

  Message postProcessMessage;
  try (Scope scope = tracing.currentTraceContext().newScope(parent)) {
    postProcessMessage = tracingMessagePostProcessor.postProcessMessage(message);
  }

  assertThat(spans.get(0).parentId()).isEqualTo(parent.spanIdString());
  Map<String, Object> headers = postProcessMessage.getMessageProperties().getHeaders();
  assertThat(headers.get("b3").toString()).endsWith("-" + spans.get(0).id() + "-1");
}
 
Example #17
Source File: TracingMessagePostProcessorTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void should_retain_baggage() {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  message.getMessageProperties().setHeader("b3", B3SingleFormat.writeB3SingleFormat(parent));
  message.getMessageProperties().setHeader(BAGGAGE_FIELD_KEY, "");

  Message postProcessMessage = tracingMessagePostProcessor.postProcessMessage(message);

  assertThat(spans.get(0).parentId()).isEqualTo(parent.spanIdString());
  Map<String, Object> headers = postProcessMessage.getMessageProperties().getHeaders();
  assertThat(headers.get("b3").toString()).endsWith("-" + spans.get(0).id() + "-1");
  assertThat(headers.get(BAGGAGE_FIELD_KEY).toString()).isEmpty();
}
 
Example #18
Source File: TracingMessagePostProcessorTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void should_resume_headers() {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  message.getMessageProperties().setHeader("b3", B3SingleFormat.writeB3SingleFormat(parent));

  Message postProcessMessage = tracingMessagePostProcessor.postProcessMessage(message);

  assertThat(spans.get(0).parentId()).isEqualTo(parent.spanIdString());
  Map<String, Object> headers = postProcessMessage.getMessageProperties().getHeaders();
  assertThat(headers.get("b3").toString()).endsWith("-" + spans.get(0).id() + "-1");
}
 
Example #19
Source File: ITSpringRabbit.java    From brave with Apache License 2.0 5 votes vote down vote up
void send() {
  byte[] messageBody = "hello world".getBytes();
  MessageProperties properties = new MessageProperties();
  properties.setHeader("not-zipkin-header", "fakeValue");
  Message message = MessageBuilder.withBody(messageBody).andProperties(properties).build();
  rabbitTemplate.send(binding.getRoutingKey(), message);
}
 
Example #20
Source File: AmqpMessageDispatcherService.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
protected void sendPingReponseToDmfReceiver(final Message ping, final String tenant, final String virtualHost) {
    final Message message = MessageBuilder.withBody(String.valueOf(System.currentTimeMillis()).getBytes())
            .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
            .setCorrelationId(ping.getMessageProperties().getCorrelationId())
            .setHeader(MessageHeaderKey.TYPE, MessageType.PING_RESPONSE).setHeader(MessageHeaderKey.TENANT, tenant)
            .build();

    amqpSenderService.sendMessage(message,
            IpUtil.createAmqpUri(virtualHost, ping.getMessageProperties().getReplyTo()));
}
 
Example #21
Source File: MQSender.java    From iot-dc with Apache License 2.0 5 votes vote down vote up
public void send(EMqExchange[] mqExchange, String content) {
    Message message = MessageBuilder.withBody(content.getBytes())
            .setContentType(MessageProperties.CONTENT_TYPE_JSON)
            .setContentEncoding("utf-8")
            .build();
    if (mqExchange == null || mqExchange.length == 0) {
        return;
    }
    Arrays.asList(mqExchange).stream().forEach(eMqExchange -> {
        LOGGER.info("send -> {} -> {}", eMqExchange.name(), message);
        this.rabbitTemplate.convertAndSend(eMqExchange.getMqFanoutExchange(), "", message);
    });
}
 
Example #22
Source File: MQSender.java    From iot-dc with Apache License 2.0 5 votes vote down vote up
public void send(EMqExchange[] mqExchange, String content) {
    Message message = MessageBuilder.withBody(content.getBytes())
            .setContentType(MessageProperties.CONTENT_TYPE_JSON)
            .setContentEncoding("utf-8")
            .build();
    if (mqExchange == null || mqExchange.length == 0) {
        return;
    }
    Arrays.asList(mqExchange).stream().forEach(eMqExchange -> {
        LOGGER.info("send -> {} -> {}", eMqExchange.name(), message);
        this.rabbitTemplate.convertAndSend(eMqExchange.getMqFanoutExchange(), "", message);
    });
}
 
Example #23
Source File: TracingMessagePostProcessorTest.java    From brave with Apache License 2.0 4 votes vote down vote up
@Test public void should_report_span() {
  Message message = MessageBuilder.withBody(new byte[0]).build();
  tracingMessagePostProcessor.postProcessMessage(message);

  assertThat(spans).hasSize(1);
}
 
Example #24
Source File: OrderMessageSenderImpl.java    From POC with Apache License 2.0 4 votes vote down vote up
protected Message getRabbitMQMessage(String orderJson) {
	return MessageBuilder.withBody(orderJson.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON)
			.build();
}
 
Example #25
Source File: TestController.java    From demo_springboot_rabbitmq with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/send", method = RequestMethod.GET)
public String testSend() {
    Message message = MessageBuilder.withBody(new PersonDO("1", "10086", "liubo", 20).toString().getBytes()).setMessageId("123").build();
    rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.ROUTINGKEY, message);
    return "ok";
}