All Projects → tocrhz → mqtt-spring-boot-starter

tocrhz / mqtt-spring-boot-starter

Licence: Apache-2.0 license
MQTT starter for Spring Boot, easier to use.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to mqtt-spring-boot-starter

Opentracing Toolbox
Best-of-breed OpenTracing utilities, instrumentations and extensions
Stars: ✭ 161 (+235.42%)
Mutual labels:  spring-boot-starter
faucet-pipeline-spring-boot-starter
faucet-pipeline for Spring Boot
Stars: ✭ 17 (-64.58%)
Mutual labels:  spring-boot-starter
spring-cloud-marathon
Spring Cloud integration with Mesos and Marathon
Stars: ✭ 29 (-39.58%)
Mutual labels:  spring-boot-starter
Riptide
Client-side response routing for Spring
Stars: ✭ 169 (+252.08%)
Mutual labels:  spring-boot-starter
localstack-spring-boot-starter
SpringBoot Starter for Localstack
Stars: ✭ 38 (-20.83%)
Mutual labels:  spring-boot-starter
memcached-spring-boot
Library that provides support for auto-configuration of Memcached cache in a Spring Boot application.
Stars: ✭ 68 (+41.67%)
Mutual labels:  spring-boot-starter
Grpc Spring Boot Starter
Spring Boot starter module for gRPC framework.
Stars: ✭ 2,190 (+4462.5%)
Mutual labels:  spring-boot-starter
qynat-spring-boot-starter
A springboot-starter that can achieve Intranet penetration. 一款可以实现内网穿透的springboot-starter。
Stars: ✭ 65 (+35.42%)
Mutual labels:  spring-boot-starter
logback-access-spring-boot-starter
Spring Boot Starter for Logback-access.
Stars: ✭ 153 (+218.75%)
Mutual labels:  spring-boot-starter
infinispan-spring-boot
Infinispan Spring Boot starter. Use this starter in your Spring Boot applications to help you use Infinispan+Spring integration in embedded and client/server mode
Stars: ✭ 61 (+27.08%)
Mutual labels:  spring-boot-starter
Rocketmq Spring Boot Starter
rocketmq-spring-boot-starter
Stars: ✭ 178 (+270.83%)
Mutual labels:  spring-boot-starter
weixin-sdk
www.docs4dev.com/
Stars: ✭ 19 (-60.42%)
Mutual labels:  spring-boot-starter
honeymon-spring-boot-starter
Simple Spring Boot Starter and Summary
Stars: ✭ 20 (-58.33%)
Mutual labels:  spring-boot-starter
Tutorial Soap Spring Boot Cxf
Tutorial how to create, test, deploy, monitor SOAP-Webservices using Spring Boot and Apache CXF
Stars: ✭ 167 (+247.92%)
Mutual labels:  spring-boot-starter
cxf-spring-boot-starter
Enterprise & production ready SOAP webservices powered by Spring Boot & Apache CXF
Stars: ✭ 129 (+168.75%)
Mutual labels:  spring-boot-starter
Cas Client Autoconfig Support
Annotation-based configuration support for Apereo CAS Java clients
Stars: ✭ 153 (+218.75%)
Mutual labels:  spring-boot-starter
shiro-pac4j-spring-boot-starter
pac4j + shiro
Stars: ✭ 14 (-70.83%)
Mutual labels:  spring-boot-starter
hiatus-spring-boot
No description or website provided.
Stars: ✭ 23 (-52.08%)
Mutual labels:  spring-boot-starter
neo4j-java-driver-spring-boot-starter
Automatic configuration of Neo4j's Java Driver for Spring Boot applications
Stars: ✭ 33 (-31.25%)
Mutual labels:  spring-boot-starter
openHCAN
Hausautomatisierungsloesung auf CAN-Bus Basis.
Stars: ✭ 16 (-66.67%)
Mutual labels:  paho-mqtt

mqtt-spring-boot-starter

MQTT starter for Spring Boot, easier to use.

Support spring boot version: 2.x

This document is machine translated.

0. 修改记录

2022-02-22 v1.2.7

  1. 配置调整, mqtt.clients 中明确配置的 client-id 优先级更高

2021-11-16 v1.2.6

  1. 去掉默认的异常处理.

2021-11-09

  1. 已知bug: ClientRegistry.setDefault 不生效.

2021-11-08

  1. 新版本 v1.2.5

2021-11-05

  1. 增加一个配置抽象类, 删除几个配置用的接口(实现挪到抽象类里)
  2. 去除@NonNull的使用(该注解springboot1.x中不存在)

1. import

<dependency>
    <groupId>com.github.tocrhz</groupId>
    <artifactId>mqtt-spring-boot-starter</artifactId>
    <version>1.2.6</version>
</dependency>

2. properties

Most of the configuration has default values, they all start with 'mqtt.'. Support multiple client.

e.g.

mqtt.uri=tcp://127.0.0.1:1883
mqtt.client-id=default_client
mqtt.username=username
mqtt.password=password

# mqtt.clients 中明确配置的 client-id 优先级更高
mqtt.clients.multi_client_1.client-id=new_client
mqtt.clients.multi_client_1.uri=tcp://127.0.0.1:1883
mqtt.clients.multi_client_1.username=username
mqtt.clients.multi_client_1.password=password

mqtt.clients.multi_client_2.uri=tcp://127.0.0.1:1883
mqtt.clients.multi_client_2.username=username
mqtt.clients.multi_client_2.password=password

3. usage

subscribe

Add @MqttSubscribe annotation to any public method.

e.g.

@Component
public class MqttMessageHandler {
    
    /**
     * topic = test/+
     */
    @MqttSubscribe("test/+")
    public void sub(String topic, MqttMessage message, @Payload String payload) {
        logger.info("receive from    : {}", topic);
        logger.info("message payload : {}", new String(message.getPayload(), StandardCharsets.UTF_8));
        logger.info("string payload  : {}", payload);
    }

    /**
     * clientId = multi_client_1
     * topic = test/+
     */
    @MqttSubscribe(value = "test/+", clients = "multi_client_1")
    public void sub(String topic, MqttMessage message, @Payload String payload) {
        logger.info("receive from    : {}", topic);
        logger.info("message payload : {}", new String(message.getPayload(), StandardCharsets.UTF_8));
        logger.info("string payload  : {}", payload);
    }

    /**
     * subscribe = $queue/test/+
     * topic = test/+
     * pattern = ^test/([^/]+)$
     */
    @MqttSubscribe(value="test/{id}", shared=true)
    public void sub(String topic, @NamedValue("id") String id, @Payload UserInfo userInfo) {
        logger.info("receive from   : {}", topic);
        logger.info("named value id : {}", id);
        logger.info("object payload : {}", userInfo);
    }

    /**
     * subscribe = $share/gp/test/+
     * topic = test/+
     * pattern = ^test/([^/]+)$
     */
    @MqttSubscribe(value="test/{id}", shared=true, groups="gp")
    public void sub(String topic, @NamedValue("id") String id, @Payload UserInfo userInfo) {
        logger.info("receive from   : {}", topic);
        logger.info("named value id : {}", id);
        logger.info("object payload : {}", userInfo);
    }
}

publish

Just inject MqttPublisher and call the send method.

e.g.

@Component
public class DemoService {

    private final MqttPublisher publisher;

    public DemoService(MqttPublisher publisher) {
        this.publisher = publisher;
    }

    public void sendTest(){
        publisher.send("test/send", "test message, default QOS is 0.");
        publisher.send("test/send", "Specify QOS as 1.", 1);
        publisher.send("test/send", "Specify QOS as 2.", 2, false);
        publisher.send("multi_client_1", "test/send", "test message, default QOS is 0.");
    }
}

4. extension point.

payload serialize or deserialize

Implements PayloadSerialize and PayloadDeserialize, or implements ConverterFactory<byte[], Object> and Converter<Object, byte[]> is the same.

e.g.

@Slf4j
@Configuration
public class MqttPayloadConfig {

    @Bean
    public PayloadSerialize payloadSerialize(ObjectMapper objectMapper) {
        return source -> {
            try {
                return objectMapper.writeValueAsBytes(source);
            } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
                log.warn("Payload serialize error: {}", e.getMessage(), e);
            }
            return null;
        };
    }

    @Bean
    public PayloadDeserialize payloadDeserialize(ObjectMapper objectMapper) {
        return new PayloadDeserialize() {
            @Override
            @SuppressWarnings("unchecked")
            public <T> Converter<byte[], T> getConverter(Class<T> targetType) {
                return source -> {
                    try {
                        if (targetType == String.class) {
                            return (T) new String(source, StandardCharsets.UTF_8);
                        }
                        return objectMapper.readValue(source, targetType);
                    } catch (IOException e) {
                        log.warn("Payload deserialize error: {}", e.getMessage(), e);
                    }
                    return null;
                };
            }
        };
    }
}

配置

通过 MqttConfigurer 抽象类, 可以在创建客户端前, 连接前, 订阅前自定义操作.

e.g.

@Component
public class MyMqttConfigurer extends MqttConfigurer { 
    
    /**
     * 在创建客户端之前, 增删改客户端配置.
     * <p>清除的原有客户端, 增加客户端 "client01" </p>
     */
    public void beforeCreate(ClientRegistry registry) {
        registry.clear();
        registry.add("client01", "tcp://localhost:1883");
    }


    /**
     * 创建客户端.
     *
     * @param clientId 客户端ID
     * @param options  MqttConnectOptions
     */
    public IMqttAsyncClient postCreate(String clientId, MqttConnectOptions options) throws MqttException {
        return new MqttAsyncClient(options.getServerURIs()[0], clientId, new MemoryPersistence());
    }

    /**
     * 在创建客户端后, 订阅主题前, 修改订阅的主题.
     * <p>清除 client01 的原有订阅, 增加订阅 "/test/abc"</p>
     * 
     */
    public void beforeSubscribe(String clientId, Set<TopicPair> topicPairs) {
        if("client01".equals(clientId)){
            topicPairs.clear();
            topicPairs.add(TopicPair.of("/test/abc", 0));
        }
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].