(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)结果就是不管谁忙谁清闲,都不会多给消息,总是你一个我一个,公平分发。