|
33 | 33 | import org.eclipse.paho.client.mqttv3.internal.ConnectActionListener; |
34 | 34 | import org.eclipse.paho.client.mqttv3.internal.DisconnectedMessageBuffer; |
35 | 35 | import org.eclipse.paho.client.mqttv3.internal.ExceptionHelper; |
| 36 | +import org.eclipse.paho.client.mqttv3.internal.HighResolutionTimer; |
| 37 | +import org.eclipse.paho.client.mqttv3.internal.SystemHighResolutionTimer; |
36 | 38 | import org.eclipse.paho.client.mqttv3.internal.NetworkModule; |
37 | 39 | import org.eclipse.paho.client.mqttv3.internal.NetworkModuleService; |
38 | 40 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttDisconnect; |
@@ -433,6 +435,124 @@ public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence |
433 | 435 | */ |
434 | 436 | public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence persistence, |
435 | 437 | MqttPingSender pingSender, ScheduledExecutorService executorService) throws MqttException { |
| 438 | + this(serverURI, clientId, persistence, pingSender, executorService, null); |
| 439 | + } |
| 440 | + /** |
| 441 | + * Create an MqttAsyncClient that is used to communicate with an MQTT |
| 442 | + * server. |
| 443 | + * <p> |
| 444 | + * The address of a server can be specified on the constructor. |
| 445 | + * Alternatively a list containing one or more servers can be specified |
| 446 | + * using the {@link MqttConnectOptions#setServerURIs(String[]) |
| 447 | + * setServerURIs} method on MqttConnectOptions. |
| 448 | + * |
| 449 | + * <p> |
| 450 | + * The <code>serverURI</code> parameter is typically used with the the |
| 451 | + * <code>clientId</code> parameter to form a key. The key is used to store |
| 452 | + * and reference messages while they are being delivered. Hence the |
| 453 | + * serverURI specified on the constructor must still be specified even if a |
| 454 | + * list of servers is specified on an MqttConnectOptions object. The |
| 455 | + * serverURI on the constructor must remain the same across restarts of the |
| 456 | + * client for delivery of messages to be maintained from a given client to a |
| 457 | + * given server or set of servers. |
| 458 | + * |
| 459 | + * <p> |
| 460 | + * The address of the server to connect to is specified as a URI. Two types |
| 461 | + * of connection are supported <code>tcp://</code> for a TCP connection and |
| 462 | + * <code>ssl://</code> for a TCP connection secured by SSL/TLS. For example: |
| 463 | + * </p> |
| 464 | + * <ul> |
| 465 | + * <li><code>tcp://localhost:1883</code></li> |
| 466 | + * <li><code>ssl://localhost:8883</code></li> |
| 467 | + * </ul> |
| 468 | + * <p> |
| 469 | + * If the port is not specified, it will default to 1883 for |
| 470 | + * <code>tcp://</code>" URIs, and 8883 for <code>ssl://</code> URIs. |
| 471 | + * </p> |
| 472 | + * |
| 473 | + * <p> |
| 474 | + * A client identifier <code>clientId</code> must be specified and be less |
| 475 | + * that 65535 characters. It must be unique across all clients connecting to |
| 476 | + * the same server. The clientId is used by the server to store data related |
| 477 | + * to the client, hence it is important that the clientId remain the same |
| 478 | + * when connecting to a server if durable subscriptions or reliable |
| 479 | + * messaging are required. |
| 480 | + * <p> |
| 481 | + * A convenience method is provided to generate a random client id that |
| 482 | + * should satisfy this criteria - {@link #generateClientId()}. As the client |
| 483 | + * identifier is used by the server to identify a client when it reconnects, |
| 484 | + * the client must use the same identifier between connections if durable |
| 485 | + * subscriptions or reliable delivery of messages is required. |
| 486 | + * </p> |
| 487 | + * <p> |
| 488 | + * In Java SE, SSL can be configured in one of several ways, which the |
| 489 | + * client will use in the following order: |
| 490 | + * </p> |
| 491 | + * <ul> |
| 492 | + * <li><strong>Supplying an <code>SSLSocketFactory</code></strong> - |
| 493 | + * applications can use |
| 494 | + * {@link MqttConnectOptions#setSocketFactory(SocketFactory)} to supply a |
| 495 | + * factory with the appropriate SSL settings.</li> |
| 496 | + * <li><strong>SSL Properties</strong> - applications can supply SSL |
| 497 | + * settings as a simple Java Properties using |
| 498 | + * {@link MqttConnectOptions#setSSLProperties(Properties)}.</li> |
| 499 | + * <li><strong>Use JVM settings</strong> - There are a number of standard |
| 500 | + * Java system properties that can be used to configure key and trust |
| 501 | + * stores.</li> |
| 502 | + * </ul> |
| 503 | + * |
| 504 | + * <p> |
| 505 | + * In Java ME, the platform settings are used for SSL connections. |
| 506 | + * </p> |
| 507 | + * <p> |
| 508 | + * A persistence mechanism is used to enable reliable messaging. For |
| 509 | + * messages sent at qualities of service (QoS) 1 or 2 to be reliably |
| 510 | + * delivered, messages must be stored (on both the client and server) until |
| 511 | + * the delivery of the message is complete. If messages are not safely |
| 512 | + * stored when being delivered then a failure in the client or server can |
| 513 | + * result in lost messages. A pluggable persistence mechanism is supported |
| 514 | + * via the {@link MqttClientPersistence} interface. An implementer of this |
| 515 | + * interface that safely stores messages must be specified in order for |
| 516 | + * delivery of messages to be reliable. In addition |
| 517 | + * {@link MqttConnectOptions#setCleanSession(boolean)} must be set to false. |
| 518 | + * In the event that only QoS 0 messages are sent or received or |
| 519 | + * cleanSession is set to true then a safe store is not needed. |
| 520 | + * </p> |
| 521 | + * <p> |
| 522 | + * An implementation of file-based persistence is provided in class |
| 523 | + * {@link MqttDefaultFilePersistence} which will work in all Java SE based |
| 524 | + * systems. If no persistence is needed, the persistence parameter can be |
| 525 | + * explicitly set to <code>null</code>. |
| 526 | + * </p> |
| 527 | + * |
| 528 | + * @param serverURI |
| 529 | + * the address of the server to connect to, specified as a URI. |
| 530 | + * Can be overridden using |
| 531 | + * {@link MqttConnectOptions#setServerURIs(String[])} |
| 532 | + * @param clientId |
| 533 | + * a client identifier that is unique on the server being |
| 534 | + * connected to |
| 535 | + * @param persistence |
| 536 | + * the persistence class to use to store in-flight message. If |
| 537 | + * null then the default persistence mechanism is used |
| 538 | + * @param pingSender |
| 539 | + * Custom {@link MqttPingSender} implementation. |
| 540 | + * @param executorService |
| 541 | + * used for managing threads. If null no executor service is used. |
| 542 | + * @param highResolutionTimer |
| 543 | + * used for providing time values for keepalive ping scheduling. If {@code null}, a default timer is used |
| 544 | + * @throws IllegalArgumentException |
| 545 | + * if the URI does not start with "tcp://", "ssl://" or |
| 546 | + * "local://" |
| 547 | + * @throws IllegalArgumentException |
| 548 | + * if the clientId is null or is greater than 65535 characters |
| 549 | + * in length |
| 550 | + * @throws MqttException |
| 551 | + * if any other problem was encountered |
| 552 | + */ |
| 553 | + public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence persistence, |
| 554 | + MqttPingSender pingSender, ScheduledExecutorService executorService, |
| 555 | + HighResolutionTimer highResolutionTimer) throws MqttException { |
436 | 556 | final String methodName = "MqttAsyncClient"; |
437 | 557 |
|
438 | 558 | log.setResourceName(clientId); |
@@ -461,13 +581,17 @@ public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence |
461 | 581 | this.persistence = new MemoryPersistence(); |
462 | 582 | } |
463 | 583 |
|
| 584 | + if (highResolutionTimer == null) { |
| 585 | + highResolutionTimer = new SystemHighResolutionTimer(); |
| 586 | + } |
| 587 | + |
464 | 588 | this.executorService = executorService; |
465 | 589 |
|
466 | 590 | // @TRACE 101=<init> ClientID={0} ServerURI={1} PersistenceType={2} |
467 | 591 | log.fine(CLASS_NAME, methodName, "101", new Object[] { clientId, serverURI, persistence }); |
468 | 592 |
|
469 | 593 | this.persistence.open(clientId, serverURI); |
470 | | - this.comms = new ClientComms(this, this.persistence, pingSender, this.executorService); |
| 594 | + this.comms = new ClientComms(this, this.persistence, pingSender, this.executorService, highResolutionTimer); |
471 | 595 | this.persistence.close(); |
472 | 596 | this.topics = new Hashtable(); |
473 | 597 |
|
|
0 commit comments