Unlock Low‑Latency IoT Messaging with mica‑mqtt: A Java AIO MQTT Component
The article introduces mica‑mqtt, a simple, low‑latency, high‑performance Java AIO MQTT library for IoT, outlines its rich feature set, shared‑subscription modes, typical use cases, recent updates, and provides step‑by‑step Spring Boot and non‑Spring Boot integration examples with configuration and code snippets.
1. Introduction
mica-mqtt is a simple, low‑latency, high‑performance MQTT IoT open‑source component built on java aio . It is easy to integrate into existing services and to extend, reducing the cost of developing custom IoT platforms.
2. Features
Supports MQTT v3.1, v3.1.1 and v5.0 protocols.
Supports MQTT over WebSocket (compatible with mqtt.js).
Provides HTTP REST API (documentation in [1]).
Provides MQTT client library.
Provides MQTT server implementation.
Shared subscription for client and server (VIP version uses topic‑tree storage, performance independent of topic count, works with millions of topics).
Supports MQTT will messages.
Supports retained messages.
Custom message processing and forwarding for clustering.
Alibaba Cloud MQTT connection demo.
Supports GraalVM native compilation.
Spring Boot starter for quick integration (
mica-mqtt-spring-boot-starter).
Prometheus + Grafana metrics via the starter.
Cluster implementation based on Redis pub/sub (see
mica-mqtt-brokermodule).
3. Use Cases
IoT cloud MQTT broker.
Edge‑side IoT message communication.
Group chat IM.
Message push services.
Simple, easy‑to‑use MQTT client.
4. Changelog
v2.2.4 – 2023‑09‑02
Merged last year’s shared‑subscription service and improvements.
Optimized topic validation.
When the same clientId subscribes to the same matching topic, the highest QoS is selected.
5. Shared Subscription
mica-mqtt supports two shared‑subscription modes:
Shared subscription using the prefix
$queue/. Multiple clients subscribe to
$queue/topic; only one client receives the message.
Group subscription using the prefix
$share/<group>/. Each group receives the message, but within a group only one client receives it.
Note: If a topic starts with “/”, e.g.,
/topic/test, the subscription must be
$share/group1//topic/test.
6. Quick Start
Spring Boot
Client dependency:
<code><dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-client-spring-boot-starter</artifactId>
<version>${mica-mqtt.version}</version>
</dependency>
</code>Client configuration (application.yml):
<code>mqtt:
client:
enabled: true
ip: 127.0.0.1
port: 1883
name: Mica-Mqtt-Client
clientId: 000001
user-name: mica
password: 123456
timeout: 5
reconnect: true
re-interval: 5000
version: mqtt_3_1_1
read-buffer-size: 8KB
max-bytes-in-message: 10MB
buffer-allocator: heap
keep-alive-secs: 60
clean-session: true
ssl:
enabled: false
</code>Subscription example:
<code>@Service
public class MqttClientSubscribeListener {
private static final Logger logger = LoggerFactory.getLogger(MqttClientSubscribeListener.class);
@MqttClientSubscribe("/test/#")
public void subQos0(String topic, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.AT_LEAST_ONCE)
public void subQos1(String topic, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
public void thingSubRegister(String topic, byte[] payload) {
// variable substitution supported since 1.3.8
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}
</code>Server dependency:
<code><dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-server-spring-boot-starter</artifactId>
<version>${mica-mqtt.version}</version>
</dependency>
</code>Server configuration (application.yml):
<code>mqtt:
server:
enabled: true
port: 1883
name: Mica-Mqtt-Server
buffer-allocator: HEAP
heartbeat-timeout: 120000
read-buffer-size: 8KB
max-bytes-in-message: 10MB
auth:
enable: false
username: mica
password: mica
debug: true
stat-enable: true
web-port: 8083
websocket-enable: true
http-enable: false
http-basic-auth:
enable: false
username: mica
password: mica
ssl:
enabled: false
</code>Non‑Spring‑Boot Projects
Client dependency:
<code><dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-client</artifactId>
<version>${mica-mqtt.version}</version>
</dependency>
</code>Server dependency:
<code><dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-mqtt-server</artifactId>
<version>${mica-mqtt.version}</version>
</dependency>
</code>7. Documentation and Examples
MQTT basics, mqttx, and mica-mqtt video tutorials.
mica-mqtt-client documentation.
mica-mqtt-server documentation.
mica-mqtt-client-spring-boot-starter documentation.
mica-mqtt-server-spring-boot-starter documentation.
mica-mqtt HTTP API documentation.
mica-mqtt release notes.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.