搜索

此内容没有您所选择的语言版本。

Chapter 6. Network connections

download PDF

6.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

container.on("connection_open", function (event) {
    console.log("Connection " + event.connection + " is open");
});

var opts = {
    host: "example.com",
    port: 5672
};

container.connect(opts);

The default host is localhost. The default port is 5672.

For information about creating secure connections, Chapter 7, Security.

6.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

var opts = {
    host: "example.com",
    reconnect: false
};

container.connect(opts);

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

var opts = {
    host: "example.com",
    initial_reconnect_delay: 100,
    max_reconnect_delay: 60 * 1000,
    reconnect_limit: 10
};

container.connect(opts);

6.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

var hosts = ["alpha.example.com", "beta.example.com"];
var index = -1;

function failover_fn() {
    index += 1;

    if (index == hosts.length) index = 0;

    return {host: hosts[index].hostname};
};

var opts = {
    host: "example.com",
    connection_details: failover_fn
}

container.connect(opts);

This example implements repeating round-robin failover for a list of hosts. You can use this interface to implement your own failover behavior.

6.4. Accepting incoming connections

AMQ JavaScript can accept inbound network connections, enabling you to build custom messaging servers.

To start listening for connections, use the container.listen() method with options containing the local host address and port to listen on.

Example: Accepting incoming connections

container.on("connection_open", function (event) {
    console.log("New incoming connection " + event.connection);
});

var opts = {
    host: "0.0.0.0",
    port: 5672
};

container.listen(opts);

The special IP address 0.0.0.0 listens on all available IPv4 interfaces. To listen on all IPv6 interfaces, use [::0].

For more information, see the server receive.js example.

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.