Java-Mqtt-ActiveMq(3)
Java-Mqtt-ActiveMq(3)
1、maven
<dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.5</version> </dependency>
2、push-code
** * Mqtt服务生产者(推送) * * @author: zy * @date: 2020-09-22 10:34 */ @Component public class MqttProduce { private MqttProduce() { } /** * ActiveMq服务地址 */ private static String MqUrl; @Value("${config.MQ-URL}") public void setMqUrl(String mqUrl) { MqUrl = mqUrl; } /** * ActiveMq客户端id */ private static String MqClinetId; @Value("${config.MQ-SUB-CLIENTID}") public void setMqClinetId(String mqClinetId) { MqClinetId = mqClinetId; } /** * ActiveMq用户名 */ private static String MqUserName; @Value("${config.MQ-USERNAME}") public void setMqUserName(String mqUserName) { MqUserName = mqUserName; } /** * ActiveMq用户密码 */ private static String MqPassword; @Value("${config.MQ-PASSWORD}") public void setMqPassword(String mqPassword) { MqPassword = mqPassword; } private static String TOPIC; private static String MESSAGE; private static MqttClient client; private static MqttTopic topic; private static MqttMessage message; /** * Mqtt-推送 * * @param top Topic * @param msg Message * @throws MqttException */ public static void doPush(String top, String msg) throws MqttException { TOPIC = top; MESSAGE = msg; client = new MqttClient(MqUrl, MqClinetId, new MemoryPersistence()); connect(); message = new MqttMessage(); message.setQos(2); //设置是否在服务器中保存消息体 message.setRetained(true); //设置消息的内容 message.setPayload(MESSAGE.getBytes()); //发布 publish(topic, message); } private static void connect() { //1、设置连接属性 MqttConnectOptions options = new MqttConnectOptions(); //2、设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接;换而言之,设置为false时可以客户端可以接受离线消息 options.setCleanSession(false); //3、用户名密码 options.setUserName(MqUserName); options.setPassword(MqPassword.toCharArray()); //4、设置超时时间 options.setConnectionTimeout(10); //5、设置回话心跳时间 options.setKeepAliveInterval(20); try { //6、设置回调类 client.setCallback(new PushCallback()); //7、连接 client.connect(options); //8、获取activeMQ上名为TOPIC的topic topic = client.getTopic(TOPIC); } catch (Exception e) { e.printStackTrace(); } } public static void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException, MqttException { // 发布的方法 MqttDeliveryToken token = topic.publish(message); // 发布 token.waitForCompletion(); System.out.println("message is published completely! " + token.isComplete()); } }
3、callback-code
/** * @author: zy * @date: 2020-09-22 15:32 */ public class PushCallback implements MqttCallback { @Override public void connectionLost(Throwable cause) { // 连接丢失后,一般在这里面进行重连 System.out.println("连接断开,可以做重连"); } @Override public void deliveryComplete(IMqttDeliveryToken token) { System.out.println("deliveryComplete---------" + token.isComplete()); } @Override public void messageArrived(String topic, MqttMessage message) throws Exception { // subscribe后得到的消息会执行到这里面 System.out.println("接收消息主题 : " + topic); System.out.println("接收消息Qos : " + message.getQos()); System.out.println("接收消息内容 : " + new String(message.getPayload())); } }
往外张望的人在做梦,向内审视的人才是清醒的