java 高并发mqtt服务器_Java使用easymqtt4j快速开发工业级mqtt企业级应用

Java使用easymqtt4j快速开发工业级mqtt企业级应用

easymqtt4j , netty +mqtt +subscriber+ publisher +broker+cluster server for java

easymqtt4j特点:

1、spring integration 集成模式,自由灵活。

2、完全支持mqtt 3.1、3.1.1国际标准协议,支持tcp\websocket等等,可配置。

3、客户端完全支持接入主流broker服务如:Eclipse Paho,Mosquitto,JBoss A-MQ 6.1, Apache ActiveMQ 5.10-SNAPSHOT,Apache Camel 2.13.0,HiveMQ,EMQ,mosquitto,moquette,JMQTT,mqttwk等等。

4、发布、订阅接口简单&统一Gateway。

5、完全支持event事件EventGateway,灵活自由控制。

6、支持handleEvent、connectionLost、 messageArrived、deliveryComplete。

7、支持preSend、postSend、afterSendCompletion。

8、支持preReceive、postReceive、afterReceiveCompletion。

使用方法&步骤:

1、引用jar

2、实现 MqttSubscriberGateway消息队列订阅 接口

3、实现 MqttEventGateway 事件 接口

4、MqttPublisherGateway消息发送 接口 ( 使用请参考 MqttScheduleTask 消息定时发送)

easymqtt4j-clientcom.zengfr.easymqtt4j${project.version}

#spring.mqtt.host.username=adminspring.mqtt.host.password=password#spring.mqtt.host.uris=ws://api.easylink.io:1983spring.mqtt.host.uris=tcp://147.14.141.51:1883#spring.mqtt.subscriber.id=subscriberId123spring.mqtt.subscriber.topics=topic/#,testtopic/#spring.mqtt.subscriber.completionTimeout=3000#spring.mqtt.publisher.id=publisherId456spring.mqtt.publisher.defaulttopic=topic0spring.mqtt.publisher.completionTimeout=3000

importcom.zengfr.easymqtt4j.client.geteway.MqttSubscriberGateway;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.messaging.Message;importorg.springframework.stereotype.Component;

/*** Created by zengfr on 2020/5/12.*/@Componentpublic classMqttSubscriberGatewayImpl implementsMqttSubscriberGateway {

staticLogger logger= LoggerFactory.getLogger(MqttSubscriberGatewayImpl.class);@Overridepublic booleanhandlerMqttMessage(Message> msg,String topic,String qos,String id,String timestamp) {

logger.info(String.format("收到:%s",msg));

return false;}

}

importcom.zengfr.easymqtt4j.client.geteway.MqttPublisherGateway;importcom.zengfr.easymqtt4j.client.util.MqttUtil;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.annotation.EnableAsync;importorg.springframework.scheduling.annotation.EnableScheduling;importorg.springframework.scheduling.annotation.Scheduled;

/*** Created by zengfr on 2020/5/12.*/@Configuration@EnableScheduling@EnableAsyncpublic classMqttScheduleTask {

private staticLogger logger= LoggerFactory.getLogger(MqttScheduleTask.class);@AutowiredprivateMqttPublisherGateway publisherGateway;

@Scheduled(fixedRate= 1000*1,initialDelay= 1000*5)

public voidsendMqtt1() {

logger.info("发送开始");String d= MqttUtil.getNowString();publisherGateway.publish("testtopic/0",0,"hello 10 "+ d);logger.info("发送结束");}

@Scheduled(fixedRate= 1000*3,initialDelay= 1000*5)

public voidsendMqtt2() throwsInterruptedException {

intcount = 11;for(inti = 0;i < count;i++) {

logger.info("发送开始");String d= MqttUtil.getNowString();publisherGateway.publish("topic/"+i,i%3,"hello 00 "+ d);publisherGateway.publish("testtopic/"+i,i%3,"hello 10 "+ d);logger.info("发送结束");}

}

}

importcom.zengfr.easymqtt4j.client.geteway.MqttEventGateway;importcom.zengfr.easymqtt4j.client.util.MqttMsgUtil;importorg.eclipse.paho.client.mqttv3.IMqttDeliveryToken;importorg.eclipse.paho.client.mqttv3.MqttException;importorg.eclipse.paho.client.mqttv3.MqttMessage;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.context.ApplicationEvent;importorg.springframework.integration.mqtt.event.MqttIntegrationEvent;importorg.springframework.messaging.Message;importorg.springframework.messaging.MessageChannel;importorg.springframework.stereotype.Component;

/*** Created by zengfr on 2020/5/12.*/@Componentpublic classMqttEventGatewayImpl implementsMqttEventGateway {

staticLogger logger= LoggerFactory.getLogger(MqttEventGatewayImpl.class);

@Overridepublic voidhandleEvent(MqttIntegrationEvent event) {

logger.info("event: {}",event);}

@Overridepublic voidhandleEvent(ApplicationEvent event) {

logger.info("event: {}",event);}

@Overridepublic voidconnectionLost(String clientId,Throwable cause) {

logger.info("connectionLost: {} {}",clientId,cause);}

@Overridepublic voidmessageArrived(String clientId,String topic,MqttMessage message) {

logger.info("messageArrived: {} {} {}",clientId,topic,message);}

@Overridepublic voiddeliveryComplete(String clientId,IMqttDeliveryToken token) {

try{

logger.info("deliveryComplete: {} {}",clientId,MqttMsgUtil.tokenToString(token));} catch(MqttException e) {

logger.error("error",e);}

}

@Overridepublic voidpreSend(String clientId,Message> message,MessageChannel channel) {

}

@Overridepublic voidpostSend(String clientId,Message> message,MessageChannel channel, booleansent) {

}

@Overridepublic voidafterSendCompletion(String clientId,Message> message,MessageChannel channel, booleansent,Exception ex) {

}

@Overridepublic booleanpreReceive(String clientId,MessageChannel channel) {

return false;}

@OverridepublicMessage> postReceive(String clientId,Message> message,MessageChannel channel) {

return null;}

@Overridepublic voidafterReceiveCompletion(String clientId,Message> message,MessageChannel channel,Exception ex) {

}

}

其他 参考 关键字:

MQTT的集成和使用基于MQTT协议在物联网系统中数据交互的重要角色,入门java项目中mqtt初始Java

Java连接MQTT 订阅和发布 Java JAVA开发MQTT总结

所以写的一个demo,在这里记录下来,方便有人使用的时候查阅,不涉及mqtt的具体讲解,只是贴代码和运行过程

java 实现mqtt发送和接收消息 客户端代码 Java

mqtt的特点就是可以用很少的网络和硬件资源来保证高并发量的数据传输,其传输的稳定性也可以手动设置Qos(消息质量)。

mqtt服务器多种多样,常见的有ActiveMqtt    EMQ  等,不过无论是什么服务器,其底层机制都是一样的。

mqtt客户端可以由java、c语言等多种预言实现,java来示例

MQTT简单demo(java)   简单的谈了一些MQTT协议的一些知识, 知识具体的Java实现

具体 配置参考:

你可能感兴趣的:(java,高并发mqtt服务器)