[英]Mqtt-Integration in Spring Boot
i try to integrate mqtt in my Spring Boot Java Project. 我尝试将mqtt集成到我的Spring Boot Java Project中。
My depdency: 我的态度:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>jackson-module-kotlin</artifactId>
<groupId>com.fasterxml.jackson.module</groupId>
</exclusion>
</exclusions>
</dependency>
I created a Java-Class named MqttClient. 我创建了一个名为MqttClient的Java类。 Here i try this:
我在这里尝试:
IMqttClient publisher = new org.eclipse.paho.client.mqttv3.MqttClient("pfad", publisherId);
I get a Error here: org.eclipse.paho.client.mqttv3.MqttClient 我在这里收到错误:org.eclipse.paho.client.mqttv3.MqttClient
Error:Unhandled exception: org.eclipse.paho.client.mqttv3.MqttException 错误:未处理的异常:org.eclipse.paho.client.mqttv3.MqttException
Any idea whats wrong? 知道怎么了吗?
Can't figure out what is the problem? 无法找出问题所在? I did solve the same problem in following fashion.
我确实按照以下方式解决了相同的问题。 See if it works for you.
看看是否适合您。 I have a mqtt gateway class defined as below.
我有一个如下定义的mqtt网关类。
import org.springframework.integration.annotation.MessagingGateway;
@MessagingGateway(defaultRequestChannel = "mqttPromiseOutboundChannel")
public interface MqttPromiseGateway {
void sendToMqtt(String data);
}
When I want to send a message to Mqtt, I autowire the defined gateway class. 当我想向Mqtt发送消息时,我会自动连接定义的网关类。
@Autowired
private MqttPromiseGateway mqttPromiseGateway;
Now I can send a message over that Mqtt channel using that gateway. 现在,我可以使用该网关通过该Mqtt通道发送消息。
mqttPromiseGateway.sendToMqtt(content);
We also define a service for MqttProducer. 我们还为MqttProducer定义了服务。
@Service
public class DemoMqttProducer {
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs(new String[] { "tcp://localhost:1883" });
options.setUserName("myusername");
options.setPassword("mypassword#".toCharArray());
factory.setConnectionOptions(options);
return factory;
}
@Bean
@ServiceActivator(inputChannel = "mqttPromiseOutboundChannel")
public MessageHandler mqttPromiseOutbound() {
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler("testClient", mqttClientFactory());
messageHandler.setAsync(true);
messageHandler.setDefaultTopic("Promise");
return messageHandler;
}
@Bean
public MessageChannel mqttOutboundChannel() {
return new DirectChannel();
}
@Bean
public MessageChannel mqttPromiseOutboundChannel() {
return new DirectChannel();
}
@Bean
public IntegrationFlow mqttInFlow() {
return IntegrationFlows.from(mqttInbound())
.transform(p -> p + ", received from MQTT")
.handle(logger())
.get();
}
@Bean
public MessageProducerSupport mqttInbound() {
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("siSampleConsumer",
mqttClientFactory(), "Promise");
adapter.setCompletionTimeout(5000);
adapter.setConverter(new DefaultPahoMessageConverter());
adapter.setQos(1);
return adapter;
}
}
The method mqttInflow will get the incoming message and send it to logger. 方法mqttInflow将获取传入的消息并将其发送到记录器。 If you want to handle it differently, you need to change that method.
如果要以不同的方式处理它,则需要更改该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.