In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Now, we will send some strings and treat them as complex tasks. We don't have a really complex task like the image being resized or the pdf file being rendered, so we use the sleep () method to simulate this. We add a dot (.) to the string to indicate the complexity of the task, and a dot (.) will take 1 second. Such as "Hello..." It will take three seconds.
If you have not set up your project, see the settings in the first tutorial. We will follow the same pattern as the first tutorial: create a package (tut2) and create Tut2Config, Tut2Receiver, and Tut2Sender.
Code integration
First create a new package (tut2), where we will place our three classes. In the configuration class Tut2Config, we set up two configuration files-- tut2 and work-queues. We use Spring to expose the queue Queue as a bean. We configure the consumer and define two bean to correspond to the worker processes receiver1 and receiver2 in the figure above.
Configuration class
@ Profile ({"tut2", "work-queues"}) @ Configurationpublic class Tut2Config {@ Bean public Queue queue () {return new Queue ("work-queues");} / * defines two consumers and gives them different identities * / @ Profile ("receiver") private static class ReceiverConfig {@ Bean public Tut2Receiver receiver1 () {return new Tut2Receiver (1) } @ Bean public Tut2Receiver receiver2 () {return new Tut2Receiver (2);} @ Profile ("sender") @ Bean public Tut2Sender sender () {return new Tut2Sender ();}}
Producer
Let's simply modify the producer's code to artificially increase the duration of the task by adding a period (.), and each dot (.) in the string will increase the time spent by 1 second.
Public class Tut2Sender {@ Autowired private AmqpTemplate template; @ Autowired private Queue queue; int dots = 0; int count = 0; @ Scheduled (fixedDelay = 1000, initialDelay = 500) public void send () {StringBuilder builder = new StringBuilder ("Hello"); if (dots++ = = 3) {dots = 1;} for (int I = 0; I < dots; iTunes +) {builder.append ('.') } builder.append (Integer.toString (+ + count)); String message = builder.toString (); template.convertAndSend (queue.getName (), message); System.out.println ("[x] Sent'" + message + ");}}
Consumer
Our consumer Tut2Receiver simulates a time-consuming fake task through the doWork () method, which needs to simulate an one-second operation for each period (.) in the message body. And we added an instance number to the consumer to know which instance consumed the message and how long it took to process it.
@ RebbitListener (queues = "work-queues") public class Tut2Receiver {private int instance; public Tut2Receiver (int instance) {this.instance = instance;} @ RabbitHandler public void receive (String in) throws InterruptedException {StopWatch watch = new StopWatch (); watch.start (); System.out.println ("instance" + this.instance + "[x] Received'" + in + ""); doWork (in) Watch.stop (); System.out.println ("instance" + this.instance + "[x] Done in" + watch.getTotalTimeSeconds () + "s");} private void doWork (String in) throws InterruptedException {for (char ch: in.toCharArray ()) {if (ch = ='.') {Thread.sleep (1000) }
Running
Maven compilation
Mvn clean package-Dmaven.test.skip=true
Running
Java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-spring.profiles.active=tut2,sender-tutorial.client.duration=60000
Java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-spring.profiles.active=tut2,receiver-tutorial.client.duration=60000
Output
/ / Sender
Ready... Running for 10000ms
[X] Sent 'Hello.1'
[X] Sent 'Hello... 2'
[X] Sent 'Hello... 3'
[X] Sent 'Hello.4'
[X] Sent 'Hello... 5'
[X] Sent 'Hello... 6'
[X] Sent 'Hello.7'
[X] Sent 'Hello... 8'
[X] Sent 'Hello... 9'
/ / Receiver
Ready... Running for 10000ms
Instance 1 [x] Received 'Hello.1'
Instance 2 [x] Received 'Hello... 2'
Instance 1 [x] Done in 1.005s
Instance 1 [x] Received 'Hello... 3'
Instance 2 [x] Done in 2.007s
Instance 2 [x] Received 'Hello.4'
Instance 2 [x] Done in 1.005s
Instance 1 [x] Done in 3.01s
Instance 1 [x] Received 'Hello... 5'
Instance 2 [x] Received 'Hello... 6'
Instance 1 [x] Done in 2.006s
Instance 1 [x] Received 'Hello.7'
Instance 1 [x] Done in 1.002s
Instance 1 [x] Received 'Hello... 9'
Instance 2 [x] Done in 3.01s
Instance 2 [x] Received 'Hello... 8'
Prefetch
As can be seen from the output on the consumer side, the task number obtained by instance 1 is always odd (Hello.1,Hello … 3Hello... 5 instance Hello.7), while the task number obtained by Hello2 is always even. To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59
If it feels like the output this time is just a coincidence, you can try it a few more times or get more output by adjusting the duration of the tutorial.client.duration=, and the result must be the same.
The problem designed here is the scheduling strategy problem mentioned earlier in the basic concept. To achieve fair scheduling (Fair dispatch) is to set the value of prefetch, which can be implemented in two ways.
Global Settin
You can set spring.rabbitmq.listener.simple.prefetch=1 in application.yml, which will affect all consumers who use the default SimpleRabbitListenerContainerFactory in this Spring Boot application.
Many people on the Internet say that the actual measurement of changing the configuration of pring.rabbitmq.listener.prefetc has been invalid, and it should be the problem of the version. The version I use (RabbitMQ:3.7.4,Spring Boot: 2.0.1.RELEASE) has a spring.rabbitmq.listener.direct.prefetch that can be configured in addition to spring.rabbitmq.listener.simple.prefetch.
After changing the configuration and running again, you can see that instance 1 can get "Hello … 6" and "Hello … 12".
Ready... Running for 60000ms
Instance 1 [x] Received 'Hello.1'
Instance 2 [x] Received 'Hello... 2'
Instance 1 [x] Done in 1.004s
Instance 1 [x] Received 'Hello... 3'
Instance 2 [x] Done in 2.008s
Instance 2 [x] Received 'Hello.4'
Instance 2 [x] Done in 1.004s
Instance 2 [x] Received 'Hello... 5'
Instance 1 [x] Done in 3.012s
Instance 1 [x] Received 'Hello... 6'
Instance 2 [x] Done in 2.007s
Instance 2 [x] Received 'Hello.7'
Instance 2 [x] Done in 1.004s
Instance 2 [x] Received 'Hello... 8'
Instance 1 [x] Done in 3.011s
Instance 1 [x] Received 'Hello... 9'
Instance 2 [x] Done in 2.007s
Instance 2 [x] Received 'Hello.10'
Instance 2 [x] Done in 1.006s
Instance 2 [x] Received 'Hello... 11'
Instance 1 [x] Done in 3.01s
Instance 1 [x] Received 'Hello... 12'
Specific consumers
The above is the consumers who have changed the overall situation. If it is only aimed at specific consumers, how to deal with it?
We can do this by customizing RabbitListenerContainerFactory.
@ Beanpublic RabbitListenerContainerFactory prefetchOneRabbitListenerContainerFactory (ConnectionFactory rabbitConnectionFactory) {SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory (); factory.setConnectionFactory (rabbitConnectionFactory); factory.setPrefetchCount (1); return factory;}
Then specify containerFactory on a specific consumer
@ RebbitListener (queues = "hello", containerFactory = "prefetchTenRabbitListenerContainerFactory") public void receive (String in) {System.out.println ("[x] Received'" + in + "")}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.