设备接入 IoTDA设备接入 IoTDA

更新时间:2021/08/16 GMT+08:00
分享

Java SDK接入示例

本文介绍使用AMQP协议的JMS客户端接入华为云物联网平台,接收服务端订阅消息的示例。

开发环境要求

本示例使用的开发环境为JDK 1.8及以上版本。

获取SDK

AMQP SDK为开源SDK。如果您使用Java开发语言,推荐使用Apache Qpid JMS客户端。请访问Qpid JMS 0.50.0下载客户端和查看使用说明。

添加Maven依赖

<!-- amqp 1.0 qpid client -->
 <dependency>
   <groupId>org.apache.qpid</groupId>
   <artifactId>qpid-jms-client</artifactId>
   <version>0.50.0</version>
 </dependency>

代码示例

您可以单击这里获取Java SDK接入示例,Demo中涉及的参数说明,请参考AMQP客户端接入说明

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.huawei.iot.amqp;

import org.apache.qpid.jms.JmsConnection;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.apache.qpid.jms.JmsConnectionListener;
import org.apache.qpid.jms.JmsQueue;
import org.apache.qpid.jms.message.JmsInboundMessageDispatch;
import org.apache.qpid.jms.transports.TransportOptions;
import org.apache.qpid.jms.transports.TransportSupport;

import javax.jms.Connection;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.net.URI;
import java.util.Hashtable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class HwIotAmqpJavaClientDemo {
    /**
     * 异步线程池,参数可以根据业务特点作调整,也可以用其他异步方式来处理。
     */
    private final static ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
            Runtime.getRuntime().availableProcessors() * 2, 60,
            TimeUnit.SECONDS, new LinkedBlockingQueue<>(5000));
    /**
     * 连接凭证接入键值
     */
    private static String accessKey = "${yourAccessKey}";
  
    /**
     * 连接凭证接入码
     */
    private static String password = "${yourAccessCode}";

    /**
     * 队列名,可以使用默认队列 DefaultQueue
     */
    private static String queueName = "${yourQueue}";

    /**
     * 按照qpid-jms的规范,组装连接URL。
     */
    private static String connectionUrl = "amqps://${UUCID}.iot-amqps.cn-north-4.myhuaweicloud.com:5671?amqp.vhost=default&amqp.idleTimeout=8000&amqp.saslMechanisms=PLAIN";

    private static Connection connection;
    private static Session session;
    private static MessageConsumer consumer;
    private static long lastReconnectTime = 0;
    private static AtomicBoolean isReconnecting = new AtomicBoolean(false);

    public static void main(String[] args) throws Exception {
        createConsumer();

        // 处理消息有两种方式
        // 1,主动拉数据(推荐),参照receiveMessage()
        receiveMessage();

        // 2, 添加监听,参照consumer.setMessageListener(messageListener), 服务端主动推数据给客户端,但得考虑接受的数据速率是客户能力能够承受住的
        // consumer.setMessageListener(messageListener);
        // 防止主程序退出,这里休眠了60s,60s后程序会结束
        // Thread.sleep( 60 * 1000L);
        // shutdown();
    }

    private static void createConsumer() throws Exception {
        long timeStamp = System.currentTimeMillis();
        //UserName组装方法,请参见文档:AMQP客户端接入说明。
        String userName = "accessKey=" + accessKey + "|timestamp=" + timeStamp;
        Hashtable<String, String> hashtable = new Hashtable<>();
        hashtable.put("connectionfactory.HwConnectionURL", connectionUrl);
        hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
        Context context = new InitialContext(hashtable);
        JmsConnectionFactory cf = (JmsConnectionFactory) context.lookup("HwConnectionURL");

        //信任服务端
        TransportOptions to = new TransportOptions();
        to.setTrustAll(true);
        cf.setSslContext(TransportSupport.createJdkSslContext(to));

        // 创建连接
        Connection connection = cf.createConnection(userName, password);
        ((JmsConnection) connection).addConnectionListener(myJmsConnectionListener);

        // 创建 Session
        // Session.CLIENT_ACKNOWLEDGE: 收到消息后,需要手动调用message.acknowledge()。
        // Session.AUTO_ACKNOWLEDGE: SDK自动ACK(推荐)。
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.start();
        connection.setExceptionListener(exceptionListener);
        // 创建 consumer
        consumer = session.createConsumer(new JmsQueue(queueName));
    }

    private static void receiveMessage() {
        while (true) {
            try {
                if (consumer != null) {
                    Message message = consumer.receive();
                    // 建议异步处理收到的消息,确保receiveMessage函数里没有耗时逻辑。
                    executorService.execute(() -> processMessage(message));
                } else {
                    Thread.sleep(1000L);
                }
            } catch (Exception e) {
                System.out.println("receiveMessage hand an exception: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * 客户端断开需要重连
     */
    private static void reconnect() {
        if (isReconnecting.compareAndSet(false, true)) {
            while (true) {
                try {
                    // 防止重连次数太多,重连时间间隔15s
                    if (System.currentTimeMillis() - lastReconnectTime < 15 * 1000L) {
                        Thread.sleep(15 * 1000L);
                    }
                    lastReconnectTime = System.currentTimeMillis();
                    shutdown();
                    createConsumer();                   
                    isReconnecting.set(false);
                    break;
                } catch (Exception e) {
                    System.out.println("reconnect hand an exception: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 在这里处理您收到消息后的具体业务逻辑。
     */
    private static void processMessage(Message message) {
        try {
            String body = message.getBody(String.class);
            String content = new String(body);
            System.out.println("receive an message, the content is " + content);
        } catch (Exception e) {
            System.out.println("processMessage occurs error: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void shutdown() {
        if (consumer != null) {
            try {
                consumer.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.setExceptionListener(null);
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

    private static MessageListener messageListener = new MessageListener() {
        @Override
        public void onMessage(Message message) {
            try {
                // 建议异步处理收到的消息,确保onMessage函数里没有耗时逻辑。
                // 如果业务处理耗时过程过长阻塞住线程,可能会影响SDK收到消息后的正常回调。
                executorService.submit(() -> processMessage(message));
            } catch (Exception e) {
                System.out.println("submit task occurs exception: " + e.getMessage());
                e.printStackTrace();
            }
        }
    };

    private static ExceptionListener exceptionListener = new ExceptionListener() {
        @Override
        public void onException(JMSException e) {
            e.printStackTrace();
            System.out.println("connection has an exception:" + e);
            // 链接发生异常,需要重连
            reconnect();
        }
    };

    private static JmsConnectionListener myJmsConnectionListener = new JmsConnectionListener() {
        /**
         * 连接成功建立。
         */
        @Override
        public void onConnectionEstablished(URI remoteURI) {
            System.out.println("onConnectionEstablished, remoteUri:" + remoteURI);
        }

        /**
         * 尝试过最大重试次数之后,最终连接失败。
         */
        @Override
        public void onConnectionFailure(Throwable error) {
            System.out.println("onConnectionFailure, " + error.getMessage());
        }

        /**
         * 连接中断。
         */
        @Override
        public void onConnectionInterrupted(URI remoteURI) {
            System.out.println("onConnectionInterrupted, remoteUri:" + remoteURI);
        }

        /**
         * 连接中断后又自动重连上。
         */
        @Override
        public void onConnectionRestored(URI remoteURI) {
            System.out.println("onConnectionRestored, remoteUri:" + remoteURI);
        }

        @Override
        public void onInboundMessage(JmsInboundMessageDispatch envelope) {
            System.out.println("onInboundMessage, " + envelope);
        }

        @Override
        public void onSessionClosed(Session session, Throwable cause) {
            System.out.println("onSessionClosed, session=" + session + ", cause =" + cause);
        }

        @Override
        public void onConsumerClosed(MessageConsumer consumer, Throwable cause) {
            System.out.println("MessageConsumer, consumer=" + consumer + ", cause =" + cause);
        }

        @Override
        public void onProducerClosed(MessageProducer producer, Throwable cause) {
            System.out.println("MessageProducer, producer=" + producer + ", cause =" + cause);
        }
    };
}
分享:

    相关文档

    相关产品