This topic shows you how to use a JMS client to connect to Message Queue for RabbitMQ and implement the Publish/Subscribe (Pub/Sub) messaging model.
- JMS Pub/Sub
The JMS Pub/Sub messaging model has the following features:
- Each message can have multiple consumers. After a producer sends a message to a topic, all subscribers of the topic can consume the message.
- Producers and subscribers have timing dependencies.
- If a subscriber creates a nondurable subscription, the subscriber must subscribe to a topic before a producer sends a message to the topic. In addition, the subscriber must remain active to consume all the messages sent to the topic. If a message is sent when the subscriber is inactive, the subscriber cannot consume the message after it becomes active. For more information about the sample code, see Nondurable subscriptions.
- If a subscriber creates a durable subscription, the subscriber must subscribe to a topic before a producer sends a message to the topic. However, the subscriber does not need to remain active to consume all the messages sent to the topic. If a message is sent when the subscriber is inactive, the subscriber can consume the message after it becomes active. For more information about the sample code, see Durable subscriptions.
- Username and password
When you access Message Queue for RabbitMQ from a client, Message Queue for RabbitMQ authenticates your permissions based on your username and password. Message Queue for RabbitMQ allows you to generate usernames and passwords for your client by using the following methods:
Notice When you call an SDK to send or receive messages on a client, we recommend that you use persistent connections. This way, you do not need to create connections every time you send or receive messages. Frequent creation of connections consumes a large number of network and server resources and may even trigger protection against SYN flood attacks. For more information, see Connection.
- Dynamic username and password: Use a permission authentication class provided by Alibaba Cloud to generate dynamic usernames and passwords.
- Static username and password (recommended): Generate static usernames and passwords in the Message Queue for RabbitMQ console. This method is the same as that of open source RabbitMQ.
You must obtain the endpoint of your instance in the Message Queue for RabbitMQ console. When you send or receive messages, you must configure the endpoint on the producer or consumer client. Otherwise, you cannot access the Message Queue for RabbitMQ instance.
- Log on to the Message Queue for RabbitMQ console.
- In the top navigation bar, select the region where your instance resides.
- In the left-side navigation pane, click Instances.
- On the Instances page, select your instance. In the Basic Information section, find the endpoint of the required network type and click the endpoint to
Type Description Example Public endpoint You can access an instance from the Internet to read and write data. By default, pay-as-you-go instances have public endpoints. To use a public endpoint for a subscription instance, you must configure a public endpoint when you create the subscription instance. XXX.mq-amqp.cn-hangzhou-a.aliyuncs.com VPC endpoint You can access an instance from a VPC to read and write data. By default, both pay-as-you-go and subscription instances have VPC endpoints. XXX.mq-amqp.cn-hangzhou-a-internal.aliyuncs.com
Add JMS dependencies
Generate a username and its password
Create the AliyunCredentialsProvider.java file that is used to generate a dynamic username and its password.
Publish a message