このコンテンツは選択した言語では利用できません。
Chapter 5. Using the API
This chapter explains how to use the AMQ JavaScript API to perform common messaging tasks.
For more information, see the AMQ JavaScript API reference and AMQ JavaScript example suite.
5.1. Basic operation リンクのコピーリンクがクリップボードにコピーされました!
5.1.1. Handling messaging events リンクのコピーリンクがクリップボードにコピーされました!
AMQ JavaScript is an asynchronous event-driven API. To define how the application handles events, the user registers event-handling functions on the container object. These functions are then called as network activity or timers trigger new events.
Example: Handling messaging events
These are only a few common-case events. The full set is documented in the {ClientAmqpJavaScriptApiLink}.
5.1.2. Creating a container リンクのコピーリンクがクリップボードにコピーされました!
The container is the top-level API object. It is the entry point for creating connections, and it is responsible for running the main event loop. It is often constructed with a global event handler.
Example: Creating a container
var rhea = require("rhea");
var container = rhea.create_container();
var rhea = require("rhea");
var container = rhea.create_container();
Setting the container identity
Each container instance has a unique identity called the container ID. When AMQ JavaScript makes a network connection, it sends the container ID to the remote peer. To set the container ID, pass the id option to the create_container method.
Example: Setting the container identity
var container = rhea.create_container({id: "job-processor-3"});
var container = rhea.create_container({id: "job-processor-3"});
If the user does not set the ID, the library will generate a UUID when the container is constucted.
5.2. Network connections リンクのコピーリンクがクリップボードにコピーされました!
5.2.1. Creating outgoing connections リンクのコピーリンクがクリップボードにコピーされました!
To connect to a remote server, pass connection options containing the host and port to the container.connect() method.
Example: Creating outgoing connections
The default host is localhost. The default port is 5672.
See the Section 5.4, “Security” section for information about creating secure connections.
5.2.2. Configuring reconnect リンクのコピーリンクがクリップボードにコピーされました!
Reconnect allows a client to recover from lost connections. It is used to ensure that the components in a distributed system reestablish communication after temporary network or component failures.
AMQ JavaScript enables reconnect by default. If a connection attempt fails, the client will try again after a brief delay. The delay increases exponentially for each new attempt, up to a default maximum of 60 seconds.
To disable reconnect, set the reconnect connection option to false.
Example: Disabling reconnect
To control the delays between connection attempts, set the initial_reconnect_delay and max_reconnect_delay connection options. Delay options are specified in milliseconds.
To limit the number of reconnect attempts, set the reconnect_limit option.
Example: Configuring reconnect
5.2.3. Configuring failover リンクのコピーリンクがクリップボードにコピーされました!
AMQ JavaScript allows you to configure alternate connection endpoints programatically.
To specify multiple connection endpoints, define a function that returns new connection options and pass the function in the connection_details option. The function is called once for each connection attempt.
Example: Configuring failover
This example implements repeating round-robin failover for a list of hosts. You can use this interface to implement your own failover behavior.
5.3. Senders and receivers リンクのコピーリンクがクリップボードにコピーされました!
The client uses sender and receiver links to represent channels for delivering messages. Senders and receivers are unidirectional, with a source end for the message origin, and a target end for the message destination.
Source and targets often point to queues or topics on a message broker. Sources are also used to represent subscriptions.
5.3.1. Creating queues and topics on demand リンクのコピーリンクがクリップボードにコピーされました!
Some message servers support on-demand creation of queues and topics. When a sender or receiver is attached, the server uses the sender target address or the receiver source address to create a queue or topic with a name matching the address.
The message server typically defaults to creating either a queue (for one-to-one message delivery) or a topic (for one-to-many message delivery). The client can indicate which it prefers by setting the queue or topic capability on the source or target.
To select queue or topic semantics, follow these steps:
- Configure your message server for automatic creation of queues and topics. This is often the default configuration.
-
Set either the
queueortopiccapability on your sender target or receiver source, as in the examples below.
Example: Sending to a queue created on demand
Example: Receiving from a topic created on demand
For more details, see the following examples:
5.3.2. Creating durable subscriptions リンクのコピーリンクがクリップボードにコピーされました!
A durable subscription is a piece of state on the remote server representing a message receiver. Ordinarily, message receivers are discarded when a client closes. However, because durable subscriptions are persistent, clients can detach from them and then re-attach later. Any messages received while detached are available when the client re-attaches.
Durable subscriptions are uniquely identified by combining the client container ID and receiver name to form a subscription ID. These must have stable values so that the subscription can be recovered.
Set the connection container ID to a stable value, such as
client-1:var container = rhea.create_container({id: "client-1"});var container = rhea.create_container({id: "client-1"});Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a receiver with a stable name, such as
sub-1, and configure the receiver source for durability by setting thedurableandexpiry_policyproperties:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
To detach from a subscription, use the receiver.detach() method. To terminate the subscription, use the receiver.close() method.
For more information, see the durable-subscribe.js example.
5.4. Security リンクのコピーリンクがクリップボードにコピーされました!
5.4.1. Securing connections with SSL/TLS リンクのコピーリンクがクリップボードにコピーされました!
AMQ JavaScript uses SSL/TLS to encrypt communication between clients and servers.
To connect to a remote server with SSL/TLS, set the transport connection option to tls.
Example: Enabling SSL/TLS
By default, the client will reject connections to servers with untrusted certificates. This is sometimes the case in test environments. To bypass certificate authorization, set the rejectUnauthorized connection option to false. Be aware that this compromises the security of your connection.
5.4.2. Connecting with a user and password リンクのコピーリンクがクリップボードにコピーされました!
AMQ JavaScript can authenticate connections with a user and password.
To specify the credentials used for authentication, set the username and password connection options.
Example: Connecting with a user and password
5.4.3. Configuring SASL authentication リンクのコピーリンクがクリップボードにコピーされました!
AMQ JavaScript uses the SASL protocol to perform authentication. SASL can use a number of different authentication mechanisms. When two network peers connect, they exchange their allowed mechanisms, and the strongest mechanism allowed by both is selected.
AMQ JavaScript enables SASL mechanisms based on the presence of user and password information. If the user and password are both specified, PLAIN is used. If only a user is specified, ANONYMOUS is used. If neither is specified, SASL is disabled.