このコンテンツは選択した言語では利用できません。
Using the AMQ Python Client
For Use with AMQ Clients 1.2
Abstract
Chapter 1. Overview リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python is a Python library for writing messaging applications. It allows you to write client and server applications that send and receive AMQP messages.
AMQ Python is part of AMQ Clients, a suite of messaging libraries supporting multiple languages and platforms. See Introducing Red Hat JBoss AMQ 7 for an overview of the clients and other AMQ components. See AMQ Clients 1.2 Release Notes for information about this release.
AMQ Python is based on the Proton API from Apache Qpid.
1.1. Key Features リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python is a flexible and capable messaging API. It enables any application to speak AMQP 1.0.
- An event-driven API that simplifies integration with existing applications
- Access to all the features and capabilities of AMQP 1.0
- SSL/TLS and SASL for secure communication
- Seamless conversion between AMQP and language-native data types
- Heartbeating and automatic reconnect for reliable network connections
1.2. Supported Standards and Protocols リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python supports the following industry-recognized standards and network protocols.
- Version 1.0 of the Advanced Message Queueing Protocol (AMQP)
- Versions 1.0, 1.1, and 1.2 of the Transport Layer Security (TLS) protocol, the successor to SSL
- Modern TCP with IPv6
1.3. Supported Configurations リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python supports the following OS and language versions. See Red Hat JBoss AMQ 7 Supported Configurations for more information.
AMQ Python is supported on Red Hat Enterprise Linux 6 and 7 with Python 2.6 and 2.7.
1.4. Terms and Concepts リンクのコピーリンクがクリップボードにコピーされました!
This section introduces the core API entities and describes how they operate together.
| Entity | Description |
|---|---|
| Container | A top-level container of connections |
| Connection | A channel for communication between two peers on a network |
| Session | A serialized context for producing and consuming messages |
| Sender | A channel for sending messages to a target |
| Receiver | A channel for receiving messages from a source |
| Source | A named point of origin for messages |
| Target | A named destination for messages |
| Message | A mutable holder of application content |
| Delivery | A message transfer |
AMQ Python sends and receives messages. Messages are transferred between connected peers over senders and receivers. Senders and receivers are established over sessions. Sessions are established over connections. Connections are established between two uniquely identified containers. Though a connection can have multiple sessions, often this is not needed. The API allows you to ignore sessions unless you require them.
A sending peer creates a sender to send messages. The sender has a target that identifies a queue or topic at the remote peer. A receiving peer creates a receiver to receive messages. The receiver has a source that identifies a queue or topic at the remote peer.
The sending of a message is called a delivery. The message is the content sent, including all metadata such as headers and annotations. The delivery is the protocol exchange associated with the transfer of that content.
To indicate that a delivery is complete, either the sender or the receiver settles it. When the other side learns that it has been settled, it will no longer communicate about that delivery. The receiver can also indicate whether it accepts or rejects the message.
1.5. Document Conventions リンクのコピーリンクがクリップボードにコピーされました!
In this document, sudo is used for any command that requires root privileges. You should always exercise caution when using sudo, as any changes can affect the entire system.
For more information about using sudo, see The sudo Command.
Chapter 2. Installation リンクのコピーリンクがクリップボードにコピーされました!
This chapter guides you through the steps required to install AMQ Python in your environment.
2.1. Prerequisites リンクのコピーリンクがクリップボードにコピーされました!
To begin installation, use your subscription to access AMQ distribution archives and package repositories.
2.2. Installing on Red Hat Enterprise Linux リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python is distributed as a set of RPM packages for Red Hat Enterprise Linux. Follow these steps to install them.
Use the
subscription-managercommand to subscribe to the required package repositories.Red Hat Enterprise Linux 6
sudo subscription-manager repos --enable=a-mq-clients-1-for-rhel-6-server-rpms
$ sudo subscription-manager repos --enable=a-mq-clients-1-for-rhel-6-server-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Red Hat Enterprise Linux 7
sudo subscription-manager repos --enable=a-mq-clients-1-for-rhel-7-server-rpms
$ sudo subscription-manager repos --enable=a-mq-clients-1-for-rhel-7-server-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
yumcommand to install thepython-qpid-protonandpython-qpid-proton-docspackages.sudo yum install python-qpid-proton python-qpid-proton-docs
$ sudo yum install python-qpid-proton python-qpid-proton-docsCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 3. Getting Started リンクのコピーリンクがクリップボードにコピーされました!
This chapter guides you through a simple exercise to help you get started using AMQ Python. Before starting, make sure you have completed the steps in the Chapter 2, Installation chapter for your environment.
3.1. Preparing the Broker リンクのコピーリンクがクリップボードにコピーされました!
The example programs require a running broker with a queue named examples. Follow these steps to define the queue and start the broker.
- Install the broker.
- Create a broker instance. Enable anonymous access.
Start the broker instance and check the console for any critical errors logged during startup.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
artemis queuecommand to create a queue calledexamples.BROKER_INSTANCE_DIR/bin/artemis queue create --name examples --auto-create-address --anycast
$ BROKER_INSTANCE_DIR/bin/artemis queue create --name examples --auto-create-address --anycastCopy to Clipboard Copied! Toggle word wrap Toggle overflow You are prompted to answer a series of questions. For yes|no questions, type
N; otherwise, press Enter to accept the default value.
3.2. Running Hello World リンクのコピーリンクがクリップボードにコピーされました!
The Hello World example sends a message to the examples queue on the broker and then fetches it back. On success it prints Hello World! to the console.
Using a new terminal window, change directory to the Python examples directory and run the helloworld.py example.
cd /usr/share/proton-0.18.0/examples/python/ python helloworld.py
$ cd /usr/share/proton-0.18.0/examples/python/
$ python helloworld.py
Hello World!
Chapter 4. Examples リンクのコピーリンクがクリップボードにコピーされました!
This chapter demonstrates the use of AMQ Python through example programs. To run them, make sure you have completed the steps in the Chapter 2, Installation chapter for your environment and you have a running and configured broker.
See the Qpid Proton Python examples for more sample programs.
4.1. Sending Messages リンクのコピーリンクがクリップボードにコピーされました!
This client program connects to a server using CONNECTION_URL, creates a sender for target ADDRESS, sends a message containing MESSAGE_BODY, closes the connection, and exits.
Example: Sending Messages
Running the Example
To run the example program, copy it to a local file and invoke it using the python command.
python send.py amqp://localhost queue1 hello
$ python send.py amqp://localhost queue1 hello
4.2. Receiving Messages リンクのコピーリンクがクリップボードにコピーされました!
This client program connects to a server using CONNECTION_URL, creates a receiver for source ADDRESS, and receives messages until it is terminated or it reaches COUNT messages.
Example: Receiving Messages
Running the Example
To run the example program, copy it to a local file and invoke it using the python command.
python receive.py amqp://localhost queue1
$ python receive.py amqp://localhost queue1
Chapter 5. Using the API リンクのコピーリンクがクリップボードにコピーされました!
This chapter explains how to use the AMQ Python API to perform common messaging tasks.
5.1. Basic Operation リンクのコピーリンクがクリップボードにコピーされました!
5.1.1. Handling Messaging Events リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python is an asynchronous event-driven API. To define how the application handles events, the user implements callback methods on the MessagingHandler class. These methods 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 API reference.
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
handler = ExampleHandler() container = Container(handler) container.run()
handler = ExampleHandler()
container = Container(handler)
container.run()
Setting the Container Identity
Each container instance has a unique identity called the container ID. When AMQ Python makes a connection, it sends the container ID to the remote peer. To set the container ID, pass it to the Container constructor.
Example: Setting the Container Identity
container = Container(handler, "job-processor-3")
container = Container(handler, "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. Connection URLs リンクのコピーリンクがクリップボードにコピーされました!
Connection URLs encode the information used to establish new connections.
Connection URL Syntax
scheme://host[:port]
scheme://host[:port]
-
Scheme - The connection transport, either
amqpfor unencrypted TCP oramqpsfor TCP with SSL/TLS encryption. - Host - The remote network host. The value can be a hostname or a numeric IP address. IPv6 addresses must be enclosed in square brackets.
-
Port - The remote network port. This value is optional. The default value is 5672 for the
amqpscheme and 5671 for theamqpsscheme.
Connection URL Examples
amqps://example.com amqps://example.net:56720 amqp://127.0.0.1 amqp://[::1]:2000
amqps://example.com
amqps://example.net:56720
amqp://127.0.0.1
amqp://[::1]:2000
5.2.2. Creating Outgoing Connections リンクのコピーリンクがクリップボードにコピーされました!
To connect to a remote server, call the Container.connect() method with a connection URL. This is typically done inside the MessagingHandler.on_start() method.
Example: Creating Outgoing Connections
See the Section 5.3, “Security” section for information about creating secure connections.
5.2.3. 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 Python enables reconnect by default. If a connection is lost or 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 10 seconds.
To disable reconnect, set the reconnect connection option to False.
Example: Disabling Reconnect
container.connect("amqp://example.com", reconnect=False)
container.connect("amqp://example.com", reconnect=False)
To control the delays between connection attempts, define a class implementing the reset and next methods and set the reconnect connection option to an instance of that class.
Example: Configuring Reconnect
The next method returns the next delay in seconds. The reset method is called once before the reconnect process begins.
5.2.4. Configuring Failover リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python allows you to configure multiple connection endpoints. If connecting to one fails, the client attempts to connect to the next in the list. If the list is exhausted, the process starts over.
To specify multiple connection endpoints, set the urls connection option to a list of connection URLs.
Example: Configuring Failover
urls = ["amqp://alpha.example.com", "amqp://beta.example.com"] container.connect(urls=urls)
urls = ["amqp://alpha.example.com", "amqp://beta.example.com"]
container.connect(urls=urls)
It is an error to use the url and urls options at the same time.
5.3. Security リンクのコピーリンクがクリップボードにコピーされました!
5.3.1. Securing Connections with SSL/TLS リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python uses SSL/TLS to encrypt communication between clients and servers.
To connect to a remote server with SSL/TLS, use a connection URL with the amqps scheme.
Example: Enabling SSL/TLS
container.connect("amqps://example.com")
container.connect("amqps://example.com")
5.3.2. Connecting with a User and Password リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python can authenticate connections with a user and password.
To specify the credentials used for authentication, set the user and password options on the connect method.
Example: Connecting with a User and Password
container.connect("amqps://example.com", user="alice", password="secret")
container.connect("amqps://example.com", user="alice", password="secret")
5.3.3. Configuring SASL Authentication リンクのコピーリンクがクリップボードにコピーされました!
AMQ Python 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.
The client uses Cyrus SASL to perform authentication. Cyrus SASL uses plug-ins to support specific SASL mechanisms. Before you can use a particular SASL mechanism, the relevant plug-in must be installed. For example, you need the cyrus-sasl-plain plug-in in order to use SASL PLAIN authentication.
To see a list of Cyrus SASL plug-ins in Red Hat Enterprise Linux, use the yum search cyrus-sasl command. To install a Cyrus SASL plug-in, use the yum install PLUG-IN command.
By default, AMQ Python allows all of the mechanisms supported by the local SASL library configuration. To restrict the allowed mechanisms and thereby control what mechanisms can be negotiated, use the allowed_mechs connection option. It takes a string containing a space-separated list of mechanism names.
Example: Configuring SASL Authentication
container.connect("amqps://example.com", allowed_mechs="ANONYMOUS")
container.connect("amqps://example.com", allowed_mechs="ANONYMOUS")
This example forces the connection to authenticate using the ANONYMOUS mechanism even if the server we connect to offers other options. Valid mechanisms include ANONYMOUS, PLAIN, SCRAM-SHA-256, SCRAM-SHA-1, GSSAPI, and EXTERNAL.
AMQ Python enables SASL by default. To disable it, set the sasl_enabled connection option to false.
Example: Disabling SASL
event.container.connect("amqps://example.com", sasl_enabled=False)
event.container.connect("amqps://example.com", sasl_enabled=False)
5.3.4. Authenticating Using Kerberos リンクのコピーリンクがクリップボードにコピーされました!
Kerberos is a network protocol for centrally managed authentication based on the exchange of encrypted tickets. See Using Kerberos for more information.
- Configure Kerberos in your operating system. See Configuring Kerberos to set up Kerberos on Red Hat Enterprise Linux.
Enable the
GSSAPISASL mechanism in your client application.container.connect("amqps://example.com", allowed_mechs="GSSAPI")container.connect("amqps://example.com", allowed_mechs="GSSAPI")Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
kinitcommand to authenticate your user credentials and store the resulting Kerberos ticket.kinit USER@REALM
$ kinit USER@REALMCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Run the client program.
5.4. More Information リンクのコピーリンクがクリップボードにコピーされました!
For more information, see the API reference.
Chapter 6. Interoperability リンクのコピーリンクがクリップボードにコピーされました!
This chapter discusses how to use AMQ Python in combination with other AMQ components. For an overview of the compatibility of AMQ components, see the product introduction.
6.1. Interoperating with Other AMQP Clients リンクのコピーリンクがクリップボードにコピーされました!
AMQP messages are composed using the AMQP type system. This common format is one of the reasons AMQP clients in different languages are able to interoperate with each other.
When sending messages, AMQ Python automatically converts language-native types to AMQP-encoded data. When receiving messages, the reverse conversion takes place.
More information about AMQP types is available at the interactive type reference maintained by the Apache Qpid project.
| AMQ Python Type | AMQP Type | Description |
|---|---|---|
|
| An empty value | |
|
| A true or false value | |
|
| A single Unicode character | |
|
| A sequence of Unicode characters | |
|
| A sequence of bytes | |
|
| A signed 8-bit integer | |
|
| A signed 16-bit integer | |
|
| A signed 32-bit integer | |
|
| A signed 64-bit integer | |
|
| An unsigned 8-bit integer | |
|
| An unsigned 16-bit integer | |
|
| An unsigned 32-bit integer | |
|
| An unsigned 64-bit integer | |
|
| A 32-bit floating point number | |
|
| A 64-bit floating point number | |
|
| A sequence of values of a single type | |
|
| A sequence of values of variable type | |
|
| A mapping from distinct keys to values | |
|
| A universally unique identifier | |
|
| A 7-bit ASCII string from a constrained domain | |
|
| An absolute point in time |
| AMQ Python | AMQ C++ | AMQ JavaScript | AMQ .NET |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
| - |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| - |
| - |
|
|
|
|
|
|
|
|
|
|
|
|
| - |
|
|
|
|
|
|
|
|
|
|
|
6.2. Interoperating with AMQ JMS リンクのコピーリンクがクリップボードにコピーされました!
AMQP defines a standard mapping to the JMS messaging model. This section discusses the various aspects of that mapping. For more information, see the AMQ JMS Interoperability chapter.
JMS Message Types
AMQ Python provides a single message type whose body type can vary. By contrast, the JMS API uses different message types to represent different kinds of data. The table below indicates how particular body types map to JMS message types.
For more explicit control of the resulting JMS message type, you can set the x-opt-jms-msg-type message annotation. See the AMQ JMS Interoperability chapter for more information.
| AMQ Python Body Type | JMS Message Type |
|---|---|
|
| |
|
| |
|
| |
| Any other type |
6.3. Connecting to AMQ Broker リンクのコピーリンクがクリップボードにコピーされました!
AMQ Broker is designed to interoperate with AMQP 1.0 clients. Check the following to ensure the broker is configured for AMQP messaging.
- Port 5672 in the network firewall is open.
- The AMQ Broker AMQP acceptor is enabled. See Configuring Network Access.
- The necessary addresses are configured on the broker. See Addresses, Queues, and Topics.
- The broker is configured to permit access from your client, and the client is configured to send the required credentials. See Broker Security.
6.4. Connecting to AMQ Interconnect リンクのコピーリンクがクリップボードにコピーされました!
AMQ Interconnect works with any AMQP 1.0 client. Check the following to ensure the components are configured correctly.
- Port 5672 in the network firewall is open.
- The router is configured to permit access from your client, and the client is configured to send the required credentials. See Interconnect Security.
Appendix A. Using Your Subscription リンクのコピーリンクがクリップボードにコピーされました!
AMQ is provided through a software subscription. To manage your subscriptions, access your account at the Red Hat Customer Portal.
Accessing Your Account
- Go to access.redhat.com.
- If you do not already have an account, create one.
- Log in to your account.
Activating a Subscription
- Go to access.redhat.com.
- Navigate to My Subscriptions.
- Navigate to Activate a subscription and enter your 16-digit activation number.
Downloading Zip and Tar Files
To access zip or tar files, use the customer portal to find the relevant files for download. If you are using RPM packages, this step is not required.
- Go to access.redhat.com.
- Navigate to DOWNLOADS.
- Locate the Red Hat JBoss AMQ entry in the JBOSS INTEGRATION AND AUTOMATION category.
- Select the desired component type from the drop-down menu on the right side of the entry.
- Select the Download link for your component.
Registering Your System for Packages
To install RPM packages on Red Hat Enterprise Linux, your system must be registered. If you are using zip or tar files, this step is not required.
- Go to access.redhat.com.
- Navigate to Registration Assistant.
- Select your OS version and continue to the next page.
- Use the listed command in your system terminal to complete the registration.
To learn more see How to Register and Subscribe a System to the Red Hat Customer Portal.
Revised on 2017-12-15 13:52:51 EST