MQ 入门【2】--一个生产者对应多个消费者(轮询机制)

(1)连接

package com.utils;

import java.io.IOException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ConnectionUtils {

	public static Connection getConnetions() throws IOException {

		ConnectionFactory factory = new ConnectionFactory();

		factory.setHost("127.0.0.1");
		factory.setPort(5672);
		factory.setVirtualHost("/vhost_zq");

		factory.setUsername("develop");
		factory.setPassword("123456");
		return factory.newConnection();

	}

}

(2)生产者

package com.producer;

import java.io.IOException;

import com.mmmm.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Send {

	private static final String QUENE_NAME = "test_simple_quene";
	/*
	 * 一个生产者对应多个消费者
	 */

	public static void main(String[] args) throws IOException {

		Connection con = ConnectionUtils.getConnetions();
		Channel channel = con.createChannel();
		channel.queueDeclare(QUENE_NAME, false, false, false, null);

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

			String msg = "hello world!"+i;

			channel.basicPublish("", QUENE_NAME, null, msg.getBytes());
			System.out.println("send:" + msg);
		}
		
		channel.close();
		con.close();

	}

}

(3)消费者

package com.consumer;

import java.io.IOException;

import com.mmmm.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

public class Resive1 {
	private static final String QUENE_NAME = "test_simple_quene";

	public static void main(String[] args)
			throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {

		Connection con = ConnectionUtils.getConnetions();

		Channel channel = con.createChannel();
		channel.queueDeclare(QUENE_NAME, false, false, false, null);

		DefaultConsumer consumer = new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				String msg = new String(body, "utf-8");
				System.out.println("[1]resieve msg:" + msg);

				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					System.out.println("[1] done");
				}
			}

		};

		boolean autoAck = true;
		channel.basicConsume(QUENE_NAME, autoAck, consumer);

	}
}

(4)消费者2

package com.consumer;

import java.io.IOException;

import com.mmmm.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

public class Resive2 {
	private static final String QUENE_NAME = "test_simple_quene";

	public static void main(String[] args)
			throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {

		Connection con = ConnectionUtils.getConnetions();

		Channel channel = con.createChannel();
		channel.queueDeclare(QUENE_NAME, false, false, false, null);

		DefaultConsumer consumer = new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				String msg = new String(body, "utf-8");
				System.out.println("[2]resieve msg:" + msg);

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					System.out.println("[2] done");
				}
			}

		};

		boolean autoAck = true;
		channel.basicConsume(QUENE_NAME, autoAck, consumer);

	}
}

结果:消费者1和消费者2处理的消息个数是一样的。

消费者1:偶数;

消费者2:奇数

这种方式叫做轮询分发(round-robin)结果就是不管谁忙谁清闲,都不会多给消息,总是你一个我一个,公平分发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值