标签:
在MQTT 官网 (http://mqtt.org/software)中有众多MQTT的实现方式。具体参看官网,Moquette是基于Apache Mina 的模型的一个Java MQTT broker。使用过Mina的同学发现其实broker的启动过程就是一个Mina应用的启动。
在MQTT moquette 中采用MINA作为底层消息的传递方式
本类的目的启动MQTT moquette Broker 的方式,
本文的源代码来自 moquette-broker-0.1-jar-with-dependencies.jar 中的server类
如果想直接启动 moquette-broker-0.1-jar-with-dependencies.jar的jar文件方式
可以执行一些命令实现
java -jar moquette-broker-0.1-jar-with-dependencies.jar
google code 下载MQTT moquette Broker 地址:
http://code.google.com/p/moquette-mqtt/
GIT 下载MQTT moquette client 地址:
https://github.com/fusesource/mqtt-client
在应用程序中使用MQTT的应用:
MQTT moquette 的broker服务启动代码如下:
- package com.etrip.mqtt;
-
- import java.io.File;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
-
- import org.apache.mina.core.service.IoAcceptor;
- import org.apache.mina.core.service.IoServiceStatistics;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- import org.apache.mina.filter.codec.ProtocolCodecFilter;
- import org.apache.mina.filter.codec.demux.DemuxingProtocolDecoder;
- import org.apache.mina.filter.codec.demux.DemuxingProtocolEncoder;
- import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
- import org.dna.mqtt.moquette.messaging.spi.impl.SimpleMessaging;
- import org.dna.mqtt.moquette.proto.ConnAckEncoder;
- import org.dna.mqtt.moquette.proto.ConnectDecoder;
- import org.dna.mqtt.moquette.proto.DisconnectDecoder;
- import org.dna.mqtt.moquette.proto.DisconnectEncoder;
- import org.dna.mqtt.moquette.proto.MQTTLoggingFilter;
- import org.dna.mqtt.moquette.proto.PingReqDecoder;
- import org.dna.mqtt.moquette.proto.PingRespEncoder;
- import org.dna.mqtt.moquette.proto.PubAckDecoder;
- import org.dna.mqtt.moquette.proto.PubAckEncoder;
- import org.dna.mqtt.moquette.proto.PubCompDecoder;
- import org.dna.mqtt.moquette.proto.PubCompEncoder;
- import org.dna.mqtt.moquette.proto.PubCompMessage;
- import org.dna.mqtt.moquette.proto.PubRecDecoder;
- import org.dna.mqtt.moquette.proto.PubRecEncoder;
- import org.dna.mqtt.moquette.proto.PubRelDecoder;
- import org.dna.mqtt.moquette.proto.PubRelEncoder;
- import org.dna.mqtt.moquette.proto.PublishDecoder;
- import org.dna.mqtt.moquette.proto.PublishEncoder;
- import org.dna.mqtt.moquette.proto.SubAckEncoder;
- import org.dna.mqtt.moquette.proto.SubscribeDecoder;
- import org.dna.mqtt.moquette.proto.UnsubAckEncoder;
- import org.dna.mqtt.moquette.proto.UnsubscribeDecoder;
- import org.dna.mqtt.moquette.proto.messages.ConnAckMessage;
- import org.dna.mqtt.moquette.proto.messages.DisconnectMessage;
- import org.dna.mqtt.moquette.proto.messages.PingRespMessage;
- import org.dna.mqtt.moquette.proto.messages.PubAckMessage;
- import org.dna.mqtt.moquette.proto.messages.PubRecMessage;
- import org.dna.mqtt.moquette.proto.messages.PubRelMessage;
- import org.dna.mqtt.moquette.proto.messages.PublishMessage;
- import org.dna.mqtt.moquette.proto.messages.SubAckMessage;
- import org.dna.mqtt.moquette.proto.messages.UnsubAckMessage;
- import org.dna.mqtt.moquette.server.MQTTHandler;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MQTTBrokerProxyServer {
- private static final Logger LOG = LoggerFactory.getLogger(MQTTBrokerProxyServer.class);
-
- public static final String STORAGE_FILE_PATH = System.getProperty("user.home") + File.separator + "moquette_store.hawtdb";
- private IoAcceptor m_acceptor;
- SimpleMessaging messaging;
-
- public static void main(String[] args)
- throws IOException
- {
- new MQTTBrokerProxyServer().startServer();
- }
-
- protected void startServer() throws IOException
- {
-
- DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder();
- decoder.addMessageDecoder(new ConnectDecoder());
- decoder.addMessageDecoder(new PublishDecoder());
- decoder.addMessageDecoder(new PubAckDecoder());
- decoder.addMessageDecoder(new PubRelDecoder());
- decoder.addMessageDecoder(new PubRecDecoder());
- decoder.addMessageDecoder(new PubCompDecoder());
- decoder.addMessageDecoder(new SubscribeDecoder());
- decoder.addMessageDecoder(new UnsubscribeDecoder());
- decoder.addMessageDecoder(new DisconnectDecoder());
- decoder.addMessageDecoder(new PingReqDecoder());
-
-
- DemuxingProtocolEncoder encoder = new DemuxingProtocolEncoder();
-
- encoder.addMessageEncoder(ConnAckMessage.class, new ConnAckEncoder());
- encoder.addMessageEncoder(SubAckMessage.class, new SubAckEncoder());
- encoder.addMessageEncoder(UnsubAckMessage.class, new UnsubAckEncoder());
- encoder.addMessageEncoder(PubAckMessage.class, new PubAckEncoder());
- encoder.addMessageEncoder(PubRecMessage.class, new PubRecEncoder());
- encoder.addMessageEncoder(PubCompMessage.class, new PubCompEncoder());
- encoder.addMessageEncoder(PubRelMessage.class, new PubRelEncoder());
- encoder.addMessageEncoder(PublishMessage.class, new PublishEncoder());
- encoder.addMessageEncoder(PingRespMessage.class, new PingRespEncoder());
- encoder.addMessageEncoder(DisconnectMessage.class,new DisconnectEncoder());
-
- this.m_acceptor = new NioSocketAcceptor();
-
- this.m_acceptor.getFilterChain().addLast("logger", new MQTTLoggingFilter("SERVER LOG"));
-
- this.m_acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(encoder, decoder));
-
- MQTTHandler handler = new MQTTHandler();
-
- this.messaging = SimpleMessaging.getInstance();
- this.messaging.init();
-
- handler.setMessaging(this.messaging);
-
- this.m_acceptor.setHandler(handler);
-
- ((NioSocketAcceptor)this.m_acceptor).setReuseAddress(true);
- ((NioSocketAcceptor)this.m_acceptor).getSessionConfig().setReuseAddress(true);
- this.m_acceptor.getSessionConfig().setReadBufferSize(2048);
- this.m_acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
- this.m_acceptor.getStatistics().setThroughputCalculationInterval(10);
- this.m_acceptor.getStatistics().updateThroughput(System.currentTimeMillis());
-
- this.m_acceptor.bind(new InetSocketAddress(1883));
-
- LOG.info("Server binded"+InetAddress.getLocalHost().getHostAddress());
- try {
- Thread.sleep(100000000000000L);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
-
- Runtime.getRuntime().addShutdownHook(new Thread()
- {
- public void run() {
- MQTTBrokerProxyServer.this.stopServer();
- }
- });
- }
-
- protected void stopServer() {
- LOG.info("Server stopping...");
-
- this.messaging.stop();
-
- IoServiceStatistics statistics = this.m_acceptor.getStatistics();
- statistics.updateThroughput(System.currentTimeMillis());
- System.out.println(String.format("Total read bytes: %d, read throughtput: %f (b/s)", new Object[] { Long.valueOf(statistics.getReadBytes()), Double.valueOf(statistics.getReadBytesThroughput()) }));
- System.out.println(String.format("Total read msgs: %d, read msg throughtput: %f (msg/s)", new Object[] { Long.valueOf(statistics.getReadMessages()), Double.valueOf(statistics.getReadMessagesThroughput()) }));
-
- for (IoSession session : this.m_acceptor.getManagedSessions().values()) {
- if ((session.isConnected()) && (!session.isClosing())) {
- session.close(false);
- }
- }
-
- this.m_acceptor.unbind();
- this.m_acceptor.dispose();
- LOG.info("Server stopped");
- }
- }
由 以上代码可以看出,在发布订阅,心跳检测,连接断开,连接时候都需要创建相关的协议编码器对象类中添加相关的编码器对象。
MQTTHandler类为主要broker处理发布和订阅消息的业务处理器类。
IoServiceStatistics类信息统计类。主要统计在mina应用中读写信息的统计。
上面代码主要讲解MQTT moquette的启动下面主要讲述服务段发布消息和客户端订阅接收信息的实现。
MQTT的学习研究(二)moquette-mqtt 的使用之mqtt broker的启动
标签:
原文地址:http://www.cnblogs.com/yudar/p/4613721.html