springboot 配置mosquitto 消息存活时间
时间: 2023-10-14 14:31:21 浏览: 202
springboot中的配置文件样板
在Spring Boot中配置Mosquitto消息存活时间,可以通过在application.properties或application.yml文件中添加以下属性来实现:
```
spring.mqtt.client.default-topic=<your-topic>
spring.mqtt.client.clean-session=true
spring.mqtt.client.connection-timeout=6000
spring.mqtt.client.keep-alive-interval=300
spring.mqtt.client.max-inflight=10
spring.mqtt.client.ssl.enabled=false
spring.mqtt.client.ssl.key-store=
spring.mqtt.client.ssl.key-store-password=
spring.mqtt.client.ssl.trust-store=
spring.mqtt.client.ssl.trust-store-password=
spring.mqtt.client.user-name=
spring.mqtt.client.password=
spring.mqtt.client.auto-startup=true
spring.mqtt.client.pool.max-wait=2000
spring.mqtt.client.pool.max-idle=10
spring.mqtt.client.pool.min-idle=1
spring.mqtt.client.pool.max-active=50
spring.mqtt.client.pool.time-between-eviction-runs=30000
spring.mqtt.client.pool.min-evictable-idle-time-millis=60000
spring.mqtt.client.pool.test-on-borrow=true
spring.mqtt.client.pool.test-while-idle=true
spring.mqtt.client.pool.test-on-return=true
spring.mqtt.client.reconnection.delay=10
spring.mqtt.client.reconnection.max-attempts=5
spring.mqtt.client.reconnection.multiplier=2.0
spring.mqtt.client.reconnection.randomization-factor=0.5
spring.mqtt.client.reconnection.topic-patterns=
spring.mqtt.client.reconnection.recovery-interval=10
spring.mqtt.client.reconnection.recovery-interval-time-unit=SECONDS
spring.mqtt.client.reconnection.initial-reconnect-delay=10
spring.mqtt.client.reconnection.initial-reconnect-delay-time-unit=SECONDS
spring.mqtt.client.reconnection.use-exponential-back-off=true
spring.mqtt.client.reconnection.back-off-multiplier=2.0
spring.mqtt.client.reconnection.max-reconnect-delay=300
spring.mqtt.client.reconnection.max-reconnect-delay-time-unit=SECONDS
spring.mqtt.client.reconnection.max-reconnect-duration=0
spring.mqtt.client.reconnection.reconnect-executor-thread-size=2
spring.mqtt.client.reconnection.recovery-executor-thread-size=2
spring.mqtt.client.reconnection.auto-startup=true
spring.mqtt.inbound.topics=<your-topic>
spring.mqtt.inbound.qos=1
spring.mqtt.inbound.async=false
spring.mqtt.inbound.pool.max-wait=2000
spring.mqtt.inbound.pool.max-idle=10
spring.mqtt.inbound.pool.min-idle=1
spring.mqtt.inbound.pool.max-active=50
spring.mqtt.inbound.pool.time-between-eviction-runs=30000
spring.mqtt.inbound.pool.min-evictable-idle-time-millis=60000
spring.mqtt.inbound.pool.test-on-borrow=true
spring.mqtt.inbound.pool.test-while-idle=true
spring.mqtt.inbound.pool.test-on-return=true
spring.mqtt.inbound.recovery-interval=10
spring.mqtt.inbound.recovery-interval-time-unit=SECONDS
spring.mqtt.inbound.client-id-prefix=
spring.mqtt.inbound.retain=true
spring.mqtt.inbound.default-retained=false
spring.mqtt.inbound.converter=org.springframework.integration.mqtt.support.DefaultPahoMessageConverter
spring.mqtt.inbound.auto-startup=true
spring.mqtt.outbound.topic=<your-topic>
spring.mqtt.outbound.qos=1
spring.mqtt.outbound.retain=false
spring.mqtt.outbound.async=false
spring.mqtt.outbound.pool.max-wait=2000
spring.mqtt.outbound.pool.max-idle=10
spring.mqtt.outbound.pool.min-idle=1
spring.mqtt.outbound.pool.max-active=50
spring.mqtt.outbound.pool.time-between-eviction-runs=30000
spring.mqtt.outbound.pool.min-evictable-idle-time-millis=60000
spring.mqtt.outbound.pool.test-on-borrow=true
spring.mqtt.outbound.pool.test-while-idle=true
spring.mqtt.outbound.pool.test-on-return=true
spring.mqtt.outbound.client-id-prefix=
spring.mqtt.outbound.converter=org.springframework.integration.mqtt.support.DefaultPahoMessageConverter
spring.mqtt.outbound.auto-startup=true
spring.mqtt.password=
spring.mqtt.url=tcp://localhost:1883
spring.mqtt.username=
```
其中,`spring.mqtt.client.keep-alive-interval`属性是用来设置MQTT客户端与服务器之间的心跳检测时间间隔,单位为秒。如果超过这个时间间隔没有收到客户端的心跳包,服务器会认为客户端已经掉线。您可以将其设置为所需的消息存活时间。例如,如果您希望消息在30秒后过期,则可以将其设置为30秒。
另外,如果您使用的是Spring Integration的MQTT支持,可以使用以下Java配置来设置心跳间隔:
```
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setConnectionTimeout(10);
factory.setKeepAliveInterval(30);
return factory;
}
```
其中,`setKeepAliveInterval()`方法用于设置心跳检测时间间隔,单位为秒。
阅读全文