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

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

主站蜘蛛池模板: 扶绥县| 金乡县| 江油市| 华池县| 江孜县| 静宁县| 深州市| 屏东县| 雷州市| 平乐县| 津市市| 泰宁县| 交城县| 桑日县| 尼木县| 安庆市| 顺义区| 凤冈县| 海门市| 甘洛县| 东乡县| 金平| 郴州市| 双辽市| 彭州市| 敦化市| 南通市| 监利县| 武邑县| 三河市| 乌拉特后旗| 彝良县| 高阳县| 杭州市| 兴安县| 泉州市| 龙井市| 瑞安市| 蓝田县| 于田县| 西安市|