Example usage for org.springframework.messaging Message getPayload

List of usage examples for org.springframework.messaging Message getPayload

Introduction

In this page you can find the example usage for org.springframework.messaging Message getPayload.

Prototype

T getPayload();

Source Link

Document

Return the message payload.

Usage

From source file:org.springframework.cloud.stream.converter.TupleJsonMessageConverter.java

@Override
public Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
    String source;//from   w  w w.ja v  a2s.co  m
    if (message.getPayload() instanceof byte[]) {
        source = new String((byte[]) message.getPayload(), Charset.forName("UTF-8"));
    } else {
        source = message.getPayload().toString();
    }
    return TupleBuilder.fromString(source);
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void defaultIncludes() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);//  w w  w  . jav a  2  s .c  o  m
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertTrue(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertEquals("application", applicationMetrics.getName());
    Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void customAppNameAndIndex() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500",
            "--spring.application.name=foo",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);/*  w  w  w . ja  v  a  2  s  .c  o m*/
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertTrue(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertTrue(contains("mem", applicationMetrics.getMetrics()));
    Assert.assertEquals("foo", applicationMetrics.getName());
    Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void usingPrefix() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500",
            "--spring.cloud.stream.metrics.prefix=foo",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);//from  w w w  .  j av a  2  s . c o m
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertTrue(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertTrue(contains("mem", applicationMetrics.getMetrics()));
    Assert.assertEquals("foo.application", applicationMetrics.getName());
    Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void includesExcludesDefaultConfig() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo",
            "--spring.metrics.export.includes=mem**", "--spring.metrics.export.excludes=integration**");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);/*from   ww  w .j a  v a  2 s  .com*/
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertFalse(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertTrue(contains("mem", applicationMetrics.getMetrics()));
    Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void includesExcludesWithApplicationMetricsConfiguration() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false",
            "--spring.metrics.export.triggers.application.delay-millis=500",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo",
            "--spring.metrics.export.triggers.application.includes=mem**",
            "--spring.metrics.export.triggers.application.excludes=integration**");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);//from  www  .j a va2s. c  o  m
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertFalse(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertTrue(contains("mem", applicationMetrics.getMetrics()));
    Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void includesExcludesWithProperties() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo",
            "--spring.metrics.export.includes=integration**",
            "--spring.cloud.stream.metrics.properties=java**,spring.test.env**");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);// ww  w. ja  v  a 2 s  . c  o m
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertFalse(contains("mem", applicationMetrics.getMetrics()));
    Assert.assertTrue(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertFalse(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    Assert.assertTrue(applicationMetrics.getProperties().get("spring.test.env.syntax").equals("testing"));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void propertiesWithPlaceholdersAndExpressions() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--PLATFORM_APP_NAME=123-name-foo",
            "--PLATFORM_APP_ID=123-id-bar",
            "--spring.cloud.application.guid=${PLATFORM_APP_NAME}.${PLATFORM_APP_ID}",
            "--spring.cloud.application.guid.expression=#{'${PLATFORM_APP_NAME}' + '..' + '${PLATFORM_APP_ID}'}",
            "--spring.cloud.application.guid.default.prop=${app.name.not.found:time-source}",
            "--spring.cloud.application.guid.default.env=${APP_NAME_NOT_FOUND:time-source}",
            "--spring.metrics.export.delay-millis=500",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo",
            "--spring.metrics.export.includes=integration**",
            "--spring.cloud.stream.metrics.properties=spring**");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);/*from  w w w . j  a  v  a2s  .  c  om*/
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertTrue(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assertions.assertThat(applicationMetrics.getProperties().get("spring.cloud.application.guid"))
            .isEqualTo("123-name-foo.123-id-bar");
    Assertions.assertThat(applicationMetrics.getProperties().get("spring.cloud.application.guid.expression"))
            .isEqualTo("123-name-foo..123-id-bar");
    Assertions.assertThat(applicationMetrics.getProperties().get("spring.cloud.application.guid.default.prop"))
            .isEqualTo("time-source");
    Assertions.assertThat(applicationMetrics.getProperties().get("spring.cloud.application.guid.default.env"))
            .isEqualTo("time-source");
    Assert.assertFalse(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    Assert.assertTrue(applicationMetrics.getProperties().get("spring.test.env.syntax").equals("testing"));
    applicationContext.close();
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void propertiesFromLowerPrioritySourcesOverridden() throws Exception {
    System.setProperty("spring.cloud.application.guid.test.metrics", "lowPriority");
    try {/*  w  w w  .jav a 2  s  .c om*/
        ConfigurableApplicationContext applicationContext = SpringApplication.run(
                BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false",
                "--spring.cloud.application.guid.test.metrics=highPriority",
                "--spring.metrics.export.delay-millis=500",
                "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo",
                "--spring.metrics.export.includes=integration**",
                "--spring.cloud.stream.metrics.properties=spring**");
        Emitter emitterSource = applicationContext.getBean(Emitter.class);
        MessageCollector collector = applicationContext.getBean(MessageCollector.class);
        Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10,
                TimeUnit.SECONDS);
        Assert.assertNotNull(message);
        ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
        ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
                ApplicationMetrics.class);
        Assert.assertTrue(
                contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
        Assertions
                .assertThat(
                        applicationMetrics.getProperties().get("spring.cloud.application.guid.test.metrics"))
                .isEqualTo("highPriority");
        applicationContext.close();
    } finally {
        System.clearProperty("spring.cloud.application.guid.test.metrics");
    }
}

From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java

@Test
public void overrideAppName() throws Exception {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
            "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500",
            "--spring.application.name=foo",
            "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo",
            "--spring.cloud.stream.metrics.key=foobarfoo");
    Emitter emitterSource = applicationContext.getBean(Emitter.class);
    MessageCollector collector = applicationContext.getBean(MessageCollector.class);
    Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(message);//ww  w .j  av  a2  s  .c  o  m
    ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
    ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
            ApplicationMetrics.class);
    Assert.assertTrue(
            contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
    Assert.assertTrue(contains("mem", applicationMetrics.getMetrics()));
    Assert.assertEquals("foobarfoo", applicationMetrics.getName());
    Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
    applicationContext.close();
}