Apollo Stomp Client code example

Source: Internet
Author: User
Tags stomp

0. Environment Preparation

0.1. Linux

0.2. java

0.3. Download Apollo binary package, unzip

0.4. Create a broker with the name Userlog

{Apollo_home}/bin/apollo Create Userlog

0.5 start Apollo

CD {Apollo_home}/userlog

Bin/apollo-broker Run

If you need to start as a service, execute the following command

sudo ln-s "/opt/db/apache-apollo-1.7.1/test/bin/apollo-broker-service"/etc/init.d/

/etc/init.d/apollo-broker-service start

1. Ports open for each protocol

INFO | Accepting Connections at:tcp://0.0.0.0:61613

INFO | Accepting Connections at:tls://0.0.0.0:61614

INFO | Accepting Connections at:ws://0.0.0.0:61623/

INFO | Accepting Connections at:wss://0.0.0.0:61624/

INFO | Administration Interface Available AT:HTTPS://127.0.0.1:61681/

INFO | Administration Interface Available at:http://127.0.0.1:61680/

3. Producers

Client C = new Client ("172.16.163.141", 61613, "admin", "password");

Final HASHMAP headers = new HashMap ();

for (int i = 0; i < 5; i++) {

C.send ("/topic/productlogs", "M" + i);

System.out.println ("Send:" + "M" + i);

Thread.Sleep (20);

}

C.disconnect (headers);

4. Consumers

Client C = new Client ("172.16.163.141", 61613, "admin", "password");

Final HASHMAP headers = new HashMap ();

C.subscribe ("/topic/productlogs", new Listener () {

@Override

public void message (MAP headers, String body) {

System.out.println ("Receive:" + body);

}

}, headers);

4.1. Send a message

Start the producer first, when there is no consumer, the message will be discarded . After the message is sent, the toplic will be automatically erased.

Note: when not persisted, after the producer starts the consumer, cannot receive the producer to send the historical message, can only receive from the message which has not lost.

4.2. Consumer News

Start the consumer, then, if there is a message, the message content will be printed.

If the consumer launches 2 copies, each consumer will receive all the messages, such as 5 messages from the producer, and 5 messages, each of which receives 5 messages.

4.3 Message grouping and persistence

Below we have all the customers in advance to divide these 5 messages

Add the following code to the consumer

Headers.put ("Persistent", "true");//The value of persistent is string, otherwise, it will produce unexpected effects (all consumers will not receive any messages)

Headers.put ("id", "dusb_1");//ID is the basis for grouping messages, each group receives a full-volume message. Consumers with the same ID, share all messages for that group.

Headers.put ("Credit", "50,0");

Lab 1:

Start 2 consumers, at this time, the message is equally to the two consumers, for example, the first consumer received 3, the second received 2, that the message is equally.

Lab 2:

At this point, if, shut down all consumers, restart the producer, send 5 messages,

On the Web page durable subs will have 5 messages persisted, then the consumer head ID is dsub_1 consumer, will accept just send the message.

Conclusion: Messages are persisted when the ID group's consumers are dead. The next time the group of consumers starts, the message consumption continues.

Thinking : At this time, if start four consumer, two consumer ID is dusb_1, the other two consumer ID is dusb_2, start producer sends 5 message, how this 5 message will distribute.

Answer:

5 messages are broadcast by ID, that is, 2 consumers with ID dusb_1 receive these 5 messages. The 5 messages were halved among 2 consumers.

2 Consumers with ID dusb_1 also receive these 5 messages, with 5 messages averaging between the two consumers

Think again 1: Take the above experiment, if you disconnect all the consumers in the 2 groups, with the producer, send 5 messages. At this time directly restart the machine, how to persist the message?

Results: Dusb_1 persisted 5 messages, and dsub_2 persisted 5 messages.

Think again 2: If you just did not restart the machine directly, but the start of the ID for dsub_2 consumers to spend. When the group message is consumed, restart the machine directly. How to persist the message.

Result: Dusb_1 persisted 5 messages, dusb_1 No message was persisted because the message had been consumed.

Restart the machine: used to simulate unexpected conditions.

Conclusion: The granularity of persistence is durable subs, which means that persistent must be used with ID. Indicates that this group of messages has been persisted

5. Stomp Message Acknowledgement ACK

Message Confirmation mode: Auto is automatically confirmed by default. That is, whenever a message is received, the message is considered to be consumed successfully and is automatically acknowledged.

Client Acknowledgement: The customer confirms the current message, by the way, to confirm that there is no urgent confirmation before the message. For example, if the confirmation message has not been sent to the server, it is disconnected.

Client acknowledgement: Client-individual only confirms current message

?

6. Message dismissed Nack

ack模式为client-individual:Applies to rejecting the current single message,

Ack mode for client:适用于驳回 those messages that have not been ACK‘ed and NACK‘ed

Additional parameters See: http://www.cnblogs.com/piaolingzxh/p/5450176.html

7. Apollo Web UI

Entrance: https://127.0.0.1:61681/

Default account password: admin password

7.1 Main Concepts

Queue: When both the producer and the consumer are disconnected, they are automatically deleted. Format:/queue/myqueuename

Topic: When both the producer and the consumer are disconnected, they are automatically deleted. Format:/topic/mytopicname

Durable Subs: The basis for grouping messages. The granularity of persistence

7.2 Have a picture of the truth

Looking at durable subs, you can see the two IDs for the experiment, respectively dsub_1,dsub_2

Click Dsub_1 to view persistent information for this ID group

Producers and consumers are

7.3 Practical Tips: Targeting producers, consumer programs

You can see the IP and port of the producer and consumer on the top,

Below, we pass the IP and port locator program,

SSH to IP address

lsof-i:59446 #列出占用该端口的进程PID信息, such as 4133

Ps-ef | grep 4133

This allows you to navigate to the main process file.

Apollo Stomp Client code example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.