官术网_书友最值得收藏!

  • RabbitMQ Cookbook
  • Sigismondo Boschi Gabriele Santomaggio
  • 580字
  • 2021-07-19 18:52:47

Connecting to the broker

Every application that uses AMQP needs to establish a connection with the AMQP broker. By default, RabbitMQ (as well as any other AMQP broker up to version 1.0) works over TCP as a reliable transport protocol on port 5672, that is, the IANA-assigned port.

We are now going to discuss how to create the connection. In all the subsequent recipes we will refer to the connection and channel as the results of the operations presented here.

Getting ready

To use this recipe we need to set up the Java development environment as mentioned in the Introduction section.

How to do it…

In order to create a Java client that connects to the RabbitMQ broker, you need to perform the following steps:

  1. Import the needed classes from the Java RabbitMQ client library in the program namespace:
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
  2. Create an instance of the client ConnectionFactory:
    ConnectionFactory factory = new ConnectionFactory();
  3. Set the ConnectionFactory options:
    factory.setHost(rabbitMQhostname);
  4. Connect to the RabbitMQ broker:
    Connection connection = factory.newConnection();
  5. Create a channel from the freshly created connection:
    Channel channel = connection.createChannel();
  6. As soon as we are done with RabbitMQ, release the channel and the connection:
    channel.close();
    connection.close();

How it works…

Using the Java client API, the application must create an instance of ConnectionFactory and set the host where RabbitMQ should be running with the setHost() method.

After the Java imports (step 1), we have instantiated the factory object (step 2). In this example we have just set the hostname that we have chosen to optionally get from the command line (step 3), but you can find more information regarding connection options in the section There's more….

In step 4 we have actually established the TCP connection to the RabbitMQ broker.

Tip

In this recipe we have used the default connection parameters user: guest, password: guest, and vhost: /; we will discuss these parameters later.

However, we are not yet ready to communicate with the broker; we need to set up a communication channel (step 5). This is an advanced concept of AMQP; using this abstraction, it is possible to let many different messaging sessions use the same logical connection.

Actually, all the communication operations of the Java client library are performed by the methods of a channel instance.

If you are developing multithreaded applications, it is highly recommended to use a different channel for each thread. If many threads use the same channel, they will serialize their execution in the channel method calls, leading to possible performance degradation.

Tip

The best practice is to open a connection and share it with different threads. Each thread creates, uses, and destroys its own independent channel(s).

There's more…

It is possible to specify many different optional properties for any RabbitMQ connection. You can find them all in the online documentation at (http://www.rabbitmq.com/releases/rabbitmq-java-client/current-javadoc). These options are all self-explanatory, except for the AMQP virtual host.

Virtual hosts are administrative containers; they allow to configure many logically independent brokers hosts within one single RabbitMQ instance, to let many different independent applications share the same RabbitMQ server. Each virtual host can be configured with its independent set of permissions, exchanges, and queues and will work in a logically separated environment.

It's possible to specify connection options by using just a connection string, also called connection URI, with the factory.setUri() method:

ConnectionFactory factory = new ConnectionFactory();
String uri="amqp://user:pass@hostname:port/vhost";
factory.setUri(uri);

Tip

The URI must conform to the syntax specified in RFC3986 (http://www.ietf.org/rfc/rfc3986.txt).

主站蜘蛛池模板: 资溪县| 平和县| 平武县| 云阳县| 军事| 八宿县| 铁力市| 洛隆县| 托里县| 黎川县| 夏邑县| 澄迈县| 大埔区| 济宁市| 衡东县| 孟连| 洛阳市| 大足县| 梅州市| 剑川县| 藁城市| 曲阳县| 交城县| 兴隆县| 泸西县| 开平市| 崇阳县| 克山县| 河北省| 七台河市| 临泽县| 东方市| 繁昌县| 长治县| 思南县| 泗阳县| 赫章县| 浑源县| 遂平县| 乐都县| 山西省|