|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.mqtt; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import javax.management.MBeanServer; |
| 24 | +import javax.management.MalformedObjectNameException; |
| 25 | +import javax.management.ObjectName; |
| 26 | + |
| 27 | +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| 28 | +import org.junit.ClassRule; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | + |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.integration.config.EnableIntegration; |
| 37 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 38 | +import org.springframework.integration.dsl.IntegrationFlows; |
| 39 | +import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport; |
| 40 | +import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; |
| 41 | +import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter; |
| 42 | +import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler; |
| 43 | +import org.springframework.integration.mqtt.support.MqttHeaders; |
| 44 | +import org.springframework.integration.support.MessageBuilder; |
| 45 | +import org.springframework.jmx.support.MBeanServerFactoryBean; |
| 46 | +import org.springframework.messaging.Message; |
| 47 | +import org.springframework.messaging.MessageChannel; |
| 48 | +import org.springframework.messaging.PollableChannel; |
| 49 | +import org.springframework.test.annotation.DirtiesContext; |
| 50 | +import org.springframework.test.context.junit4.SpringRunner; |
| 51 | + |
| 52 | +/** |
| 53 | + * @author Artem Bilan |
| 54 | + * |
| 55 | + * @since 5.1.2 |
| 56 | + */ |
| 57 | +@RunWith(SpringRunner.class) |
| 58 | +@DirtiesContext |
| 59 | +public class MqttDslTests { |
| 60 | + |
| 61 | + @ClassRule |
| 62 | + public static final BrokerRunning brokerRunning = BrokerRunning.isRunning(1883); |
| 63 | + |
| 64 | + @Autowired |
| 65 | + @Qualifier("mqttOutFlow.input") |
| 66 | + private MessageChannel mqttOutFlowInput; |
| 67 | + |
| 68 | + @Autowired |
| 69 | + private PollableChannel fromMqttChannel; |
| 70 | + |
| 71 | + @Autowired |
| 72 | + private MBeanServer server; |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testMqttChannelAdaptersAndJmx() throws MalformedObjectNameException { |
| 76 | + Set<ObjectName> mbeanNames = this.server.queryNames( |
| 77 | + new ObjectName("org.springframework.integration:type=ManagedEndpoint,*"), null); |
| 78 | + |
| 79 | + assertThat(mbeanNames.size()).isEqualTo(1); |
| 80 | + ObjectName objectName = mbeanNames.iterator().next(); |
| 81 | + assertThat(objectName.toString()).contains("name=\"mqttInFlow.mqtt:inbound-channel-adapter#0\""); |
| 82 | + |
| 83 | + String testPayload = "foo"; |
| 84 | + |
| 85 | + this.mqttOutFlowInput.send( |
| 86 | + MessageBuilder.withPayload(testPayload) |
| 87 | + .setHeader(MqttHeaders.TOPIC, "jmxTests") |
| 88 | + .build()); |
| 89 | + |
| 90 | + Message<?> receive = this.fromMqttChannel.receive(10_000); |
| 91 | + |
| 92 | + assertThat(receive).isNotNull(); |
| 93 | + assertThat(receive.getPayload()).isEqualTo(testPayload); |
| 94 | + } |
| 95 | + |
| 96 | + @Configuration |
| 97 | + @EnableIntegration |
| 98 | + @EnableIntegrationMBeanExport(server = "mbeanServer") |
| 99 | + public static class Config { |
| 100 | + |
| 101 | + @Bean |
| 102 | + public static MBeanServerFactoryBean mbeanServer() { |
| 103 | + return new MBeanServerFactoryBean(); |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public DefaultMqttPahoClientFactory pahoClientFactory() { |
| 108 | + DefaultMqttPahoClientFactory pahoClientFactory = new DefaultMqttPahoClientFactory(); |
| 109 | + MqttConnectOptions connectionOptions = new MqttConnectOptions(); |
| 110 | + connectionOptions.setServerURIs(new String[] { "tcp://localhost:1883" }); |
| 111 | + pahoClientFactory.setConnectionOptions(connectionOptions); |
| 112 | + return pahoClientFactory; |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + public IntegrationFlow mqttOutFlow() { |
| 117 | + return f -> f.handle(new MqttPahoMessageHandler("jmxTestOut", pahoClientFactory())); |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + public IntegrationFlow mqttInFlow() { |
| 122 | + return IntegrationFlows.from( |
| 123 | + new MqttPahoMessageDrivenChannelAdapter("jmxTestIn", |
| 124 | + pahoClientFactory(), "jmxTests")) |
| 125 | + .channel(c -> c.queue("fromMqttChannel")) |
| 126 | + .get(); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments