27
27
import io .netty .channel .socket .nio .NioServerSocketChannel ;
28
28
import io .netty .handler .codec .mqtt .MqttDecoder ;
29
29
import io .netty .handler .codec .mqtt .MqttEncoder ;
30
- import org .apache .rocketmq .iot .common .configuration . MQTTBridgeConfiguration ;
30
+ import org .apache .rocketmq .iot .common .config . MqttBridgeConfig ;
31
31
import org .apache .rocketmq .iot .common .data .Message ;
32
32
import org .apache .rocketmq .iot .connection .client .ClientManager ;
33
33
import org .apache .rocketmq .iot .protocol .mqtt .handler .MessageDispatcher ;
38
38
import org .apache .rocketmq .iot .protocol .mqtt .handler .downstream .impl .MqttDisconnectMessageHandler ;
39
39
import org .apache .rocketmq .iot .protocol .mqtt .handler .downstream .impl .MqttMessageForwarder ;
40
40
import org .apache .rocketmq .iot .protocol .mqtt .handler .downstream .impl .MqttPingreqMessageHandler ;
41
+ import org .apache .rocketmq .iot .protocol .mqtt .handler .downstream .impl .MqttPublishMessageHandler ;
41
42
import org .apache .rocketmq .iot .protocol .mqtt .handler .downstream .impl .MqttSubscribeMessageHandler ;
42
43
import org .apache .rocketmq .iot .protocol .mqtt .handler .downstream .impl .MqttUnsubscribeMessagHandler ;
44
+ import org .apache .rocketmq .iot .storage .message .MessageStore ;
45
+ import org .apache .rocketmq .iot .storage .rocketmq .PublishProducer ;
46
+ import org .apache .rocketmq .iot .storage .rocketmq .RocketMQPublishProducer ;
47
+ import org .apache .rocketmq .iot .storage .rocketmq .RocketMQSubscribeConsumer ;
48
+ import org .apache .rocketmq .iot .storage .rocketmq .SubscribeConsumer ;
43
49
import org .apache .rocketmq .iot .storage .subscription .SubscriptionStore ;
44
50
import org .apache .rocketmq .iot .storage .subscription .impl .InMemorySubscriptionStore ;
45
51
import org .slf4j .Logger ;
46
52
import org .slf4j .LoggerFactory ;
47
53
48
54
public class MQTTBridge {
55
+ private Logger logger = LoggerFactory .getLogger (MQTTBridge .class );
56
+
57
+ private MqttBridgeConfig bridgeConfig ;
49
58
50
59
private ServerBootstrap serverBootstrap ;
51
60
private NioEventLoopGroup bossGroup ;
52
61
private NioEventLoopGroup workerGroup ;
62
+
53
63
private MessageDispatcher messageDispatcher ;
54
64
private SubscriptionStore subscriptionStore ;
55
65
private ClientManager clientManager ;
56
66
private MqttConnectionHandler connectionHandler ;
57
- private Logger logger = LoggerFactory .getLogger (MQTTBridge .class );
67
+ private MessageStore messageStore ;
68
+ private PublishProducer publishProducer ;
69
+ private SubscribeConsumer subscribeConsumer ;
58
70
59
71
public MQTTBridge () {
60
72
init ();
61
73
}
62
74
63
75
private void init () {
64
- bossGroup = new NioEventLoopGroup (MQTTBridgeConfiguration .threadNumOfBossGroup ());
65
- workerGroup = new NioEventLoopGroup (MQTTBridgeConfiguration .threadNumOfWorkerGroup ());
76
+ this .bridgeConfig = new MqttBridgeConfig ();
77
+
78
+ subscriptionStore = new InMemorySubscriptionStore ();
79
+ if (bridgeConfig .isEnableRocketMQStore ()) {
80
+ this .publishProducer = new RocketMQPublishProducer (bridgeConfig );
81
+ this .subscribeConsumer = new RocketMQSubscribeConsumer (bridgeConfig , subscriptionStore );
82
+ }
83
+
84
+ clientManager = new ClientManagerImpl ();
85
+ messageDispatcher = new MessageDispatcher (clientManager );
86
+ connectionHandler = new MqttConnectionHandler (clientManager , subscriptionStore , subscribeConsumer );
87
+ registerMessageHandlers ();
88
+
89
+ bossGroup = new NioEventLoopGroup (bridgeConfig .getBossGroupThreadNum ());
90
+ workerGroup = new NioEventLoopGroup (bridgeConfig .getWorkerGroupThreadNum ());
66
91
serverBootstrap = new ServerBootstrap ();
67
92
serverBootstrap .group (bossGroup , workerGroup )
68
- .localAddress (MQTTBridgeConfiguration . port ())
93
+ .localAddress (bridgeConfig . getBrokerPort ())
69
94
.channel (NioServerSocketChannel .class )
70
- .option (ChannelOption .SO_BACKLOG , MQTTBridgeConfiguration . socketBacklog ())
95
+ .option (ChannelOption .SO_BACKLOG , bridgeConfig . getSocketBacklogSize ())
71
96
.childHandler (new ChannelInitializer <SocketChannel >() {
72
97
@ Override protected void initChannel (SocketChannel ch ) throws Exception {
73
98
ChannelPipeline pipeline = ch .pipeline ();
@@ -78,29 +103,35 @@ private void init() {
78
103
pipeline .addLast ("connection-manager" , connectionHandler );
79
104
}
80
105
});
81
- subscriptionStore = new InMemorySubscriptionStore ();
82
- clientManager = new ClientManagerImpl ();
83
- messageDispatcher = new MessageDispatcher (clientManager );
84
- connectionHandler = new MqttConnectionHandler (clientManager , subscriptionStore );
85
- registerMessageHandlers ();
106
+
86
107
}
87
108
88
109
private void registerMessageHandlers () {
89
110
messageDispatcher .registerHandler (Message .Type .MQTT_CONNECT , new MqttConnectMessageHandler (clientManager ));
90
111
messageDispatcher .registerHandler (Message .Type .MQTT_DISCONNECT , new MqttDisconnectMessageHandler (clientManager ));
91
- messageDispatcher .registerHandler (Message .Type .MQTT_PUBLISH , new MqttMessageForwarder (subscriptionStore ));
112
+ if (bridgeConfig .isEnableRocketMQStore ()) {
113
+ messageDispatcher .registerHandler (Message .Type .MQTT_PUBLISH , new MqttPublishMessageHandler (messageStore , publishProducer ));
114
+ // TODO: mqtt cluster inner forwarder, need management of offset and client
115
+ } else {
116
+ messageDispatcher .registerHandler (Message .Type .MQTT_PUBLISH , new MqttMessageForwarder (subscriptionStore ));
117
+ }
92
118
// TODO qos 1/2 PUBLISH
93
119
// TODO qos 1: PUBACK
94
120
// TODO qos 2: PUBREC
95
121
// TODO qos 2: PUBREL
96
122
// TODO qos 2: PUBCOMP
97
123
messageDispatcher .registerHandler (Message .Type .MQTT_PINGREQ , new MqttPingreqMessageHandler ());
98
- messageDispatcher .registerHandler (Message .Type .MQTT_SUBSCRIBE , new MqttSubscribeMessageHandler (subscriptionStore ));
99
- messageDispatcher .registerHandler (Message .Type .MQTT_UNSUBSCRIBE , new MqttUnsubscribeMessagHandler (subscriptionStore ));
124
+ messageDispatcher .registerHandler (Message .Type .MQTT_SUBSCRIBE , new MqttSubscribeMessageHandler (subscriptionStore , subscribeConsumer ));
125
+ messageDispatcher .registerHandler (Message .Type .MQTT_UNSUBSCRIBE , new MqttUnsubscribeMessagHandler (subscriptionStore , subscribeConsumer ));
100
126
}
101
127
102
128
public void start () {
129
+ logger .info ("start the MQTTServer with config " + bridgeConfig );
103
130
try {
131
+ if (bridgeConfig .isEnableRocketMQStore ()) {
132
+ publishProducer .start ();
133
+ subscribeConsumer .start ();
134
+ }
104
135
ChannelFuture channelFuture = serverBootstrap .bind ().sync ();
105
136
channelFuture .channel ().closeFuture ().sync ();
106
137
} catch (Exception e ) {
@@ -109,12 +140,15 @@ public void start() {
109
140
logger .info ("shutdown the MQTTServer" );
110
141
shutdown ();
111
142
}
112
-
113
143
}
114
144
115
145
public void shutdown () {
116
146
bossGroup .shutdownGracefully ();
117
147
workerGroup .shutdownGracefully ();
148
+ if (bridgeConfig .isEnableRocketMQStore ()) {
149
+ publishProducer .shutdown ();
150
+ subscribeConsumer .shutdown ();
151
+ }
118
152
}
119
153
120
154
public static void main (String [] args ) {
0 commit comments