 
                    
                
         
    
    
    
	
	
	
- package com.etrip.push;  
-   
- import com.ibm.mqtt.MqttAdvancedCallback;  
- import com.ibm.mqtt.MqttClient;  
- import com.ibm.mqtt.MqttException;  
- import com.ibm.mqtt.MqttSimpleCallback;  
- public class MQTTPubSub {  
-          private final static String CONNECTION_STRING = "tcp://192.168.208.46:1883";  
-          private final static boolean CLEAN_START = true;  
-          private final static short KEEP_ALIVE = 30;
-          private final static String CLIENT_ID = "client1";  
-          public String PUBLISH_TOPICS="tokudu/china";  
-          private final static String[] TOPICS = {  
-               "Test/TestTopics/Topic1",  
-               "Test/TestTopics/Topic2",  
-               "Test/TestTopics/Topic3",  
-               "tokudu/china"  
-          };  
-          private final static int[] QOS_VALUES = {0, 0, 2, 0};  
-            
-          
-          private MqttClient mqttClient = null;  
-            
-          public MQTTPubSub(){  
-               try {  
-                    
-                    mqttClient = new MqttClient(CONNECTION_STRING);  
-                    
-                    SimpleCallbackHandler simpleCallbackHandler = new SimpleCallbackHandler();  
-                    
-                    mqttClient.registerAdvancedHandler(new AdvancedCallbackHandler());
-                    
-                    mqttClient.connect(CLIENT_ID, CLEAN_START, KEEP_ALIVE);  
-                  
-                    mqttClient.subscribe(TOPICS, QOS_VALUES);  
-                    
-                    mqttClient.publish(PUBLISH_TOPICS, "keepalive".getBytes(), QOS_VALUES[0], true);  
-               } catch (MqttException e) {  
-                   e.printStackTrace();  
-               }  
-          }  
-           
-          
-          class SimpleCallbackHandler implements MqttSimpleCallback{  
-           
-               
-               @Override  
-               public void connectionLost() throws Exception {  
-                    System.out.println("客户机和broker已经断开");  
-               }  
-               
-               
-               @Override  
-               public void publishArrived(String topicName, byte[] payload, int Qos, boolean retained) throws Exception {  
-                    System.out.println("订阅主题: " + topicName);  
-                    System.out.println("消息数据: " + new String(payload));  
-                    System.out.println("消息级别(0,1,2): " + Qos);  
-                    System.out.println("是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): " + retained);  
-               }  
-             
-          }  
-            
-          
-          class AdvancedCallbackHandler implements MqttAdvancedCallback{  
-   
-             @Override  
-             public void connectionLost() throws Exception {  
-                 
-                   
-             }  
-   
-             
-             @Override  
-             public void publishArrived(String topicName, byte[] payload, int Qos,  
-                     boolean retained) throws Exception {  
-                    System.out.println("订阅主题: " + topicName);  
-                    System.out.println("消息数据: " + new String(payload));  
-                    System.out.println("消息级别(0,1,2): " + Qos);  
-                    System.out.println("是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): " + retained);  
-             }  
-   
-             @Override  
-             public void published(int arg0) {  
-                 
-                   
-             }  
-   
-             @Override  
-             public void subscribed(int Qos, byte[] payload) {  
-                    System.out.println("消息数据: " + new String(payload));  
-                    System.out.println("消息级别(0,1,2): " + Qos);  
-             }  
-   
-             @Override  
-             public void unsubscribed(int arg0) {  
-                 
-                   
-             }  
-             
-          }  
-            
-          
-          public static void main(String[] args) {  
-                new MQTTPubSub();  
-          }  
-   
- }  
 
	posted on 
2015-07-02 12:25 
Yudar 
阅读(
1403) 
评论() 
 
收藏 
举报