이 콘텐츠는 선택한 언어로 제공되지 않습니다.

4.7.4. Publish to a Direct Exchange


To publish to a direct exchange you have two options.
Create a sender that targets a specific endpoint

The first is to create a sender that routes messages directly to the endpoint that you wish to publish to. Remember that a Direct Exchange requires an exact match, so you are sending to a specific destination. At the same time, bear in mind that multiple queues can bind to the exchange to receive messages routed to the same destination. So it is a specific endpoint that may have multiple consumers.

First, create the endpoint with the following command on the server:
qpid-config add exchange direct finance
Copy to Clipboard Toggle word wrap
Or with the following code:
Python
sender = session.sender('finance;{create:always, node: {type: topic, x-declare: {type: direct}}}')
				
Copy to Clipboard Toggle word wrap
This example creates a sender that will route messages to the reports endpoint on the finance exchange.
Python
sender = session.sender('finance/reports')
sender.send('Message to all consumers bound to finance with key reports')
Copy to Clipboard Toggle word wrap
Any messages now sent using sender will go to queues that have bound to the finance direct exchange using the key reports; with one caveat.
Let's look at our second option for publishing to a Direct Exchange, as it will help to explain this caveat.
Create a sender that targets the exchange

The second option is to create a sender that routes messages to the exchange, and use the message subject to control the routing to the specific endpoint. This way you can dynamically decide where messages will go, for example based on the names of keys that are provided at run-time, perhaps in the body of other messages.

This example demonstrates how this is done:
Python
sender = session.sender('finance; {assert: always, node: {type: topic}}')
msg = Message('Message to all consumers bound to finance with key reports')
msg.subject = 'reports'
sender.send(msg)
Copy to Clipboard Toggle word wrap
With a sender that targets the exchange, we specify where our message will go in the exchange by setting the subject. You can target different endpoints on that exchange by changing the subject before sending the message. For example, to send copies of the same message to finance/reports and finance/records:
Python
sender = session.sender('finance; {assert: always, node: {type: topic}}')
msg = Message('Message for reports and records')
    
msg.subject = 'reports'
sender.send(msg)
    
msg.subject = 'records'
sender.send(msg)
Copy to Clipboard Toggle word wrap
{assert: always, node: {type: topic}} is used to ensure that we don't inadvertently connect to a queue with the name finance bound to the default exchange. Queues and exchanges have separate namespaces, but remember that the default exchange is nameless.
A Caveat

As you can observe in the second case, setting the subject influences where the message is routed. If you use the first method — the sender with the subject in its address — you must be careful not to set the message subject inadvertently. The sender will write the correct subject into the message when you send it if the message subject is blank, but it will not overwrite any message subject that you provide. The first method — the sender with a subject in its address — provides a "default destination" for all messages it sends that do not have a message subject set. You can target other endpoints on the exchange by explicitly setting a subject before sending the message - in which case they go to the exchange for further routing based on your custom subject. Just be aware that setting the message subject determines its routing.

맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat