工作队列模式:默认是轮询(你1 我1 执行)
轮询:
生产者:
package Task.lunxun;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Producer {
public static void main(String[] args) {
Connection connection = RabbitMqUtils.getConnection();
Channel channel = null;
try {
channel = connection.createChannel();
channel.queueDeclare("queue1", false, false, false, null);//有就绑定,没有就创建
for (int i = 1; i <= 10; i++) {
channel.basicPublish("", "queue1", null, ("hello rabbit" + i).getBytes());
}
System.out.println("消息发送成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
RabbitMqUtils.closeConnectionAndChannel(channel, connection);
}
}
}
多个消费者:
消费者1:
package Task.lunxun;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer1 {
public static void main(String[] args) {
Connection connection = RabbitMqUtils.getConnection();
Channel channel = null;
try {
channel = connection.createChannel();
channel.queueDeclare("queue1", false, false, false, null);//有就绑定,没有就创建
channel.basicConsume("queue1", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者2" + new String(body));
}
});
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
消费者2:
package Task.lunxun;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer2 {
public static void main(String[] args) {
Connection connection = RabbitMqUtils.getConnection();
Channel channel = null;
try {
channel = connection.createChannel();
channel.queueDeclare("queue1", false, false, false, null);//有就绑定,没有就创建
channel.basicConsume("queue1", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者1" + new String(body));
}
});
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
公平模式(慢就少执行,快就多执行):
生产者:
package Task.fair;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Producer {
public static void main(String[] args) {
Connection connection = RabbitMqUtils.getConnection();
Channel channel =null;
try {
channel = connection.createChannel();
channel.queueDeclare("queue1",false,false,false,null);
for (int i = 1; i <=10 ; i++) {
channel.basicPublish("","queue1",null,("hello rabbit"+i).getBytes());
}
System.out.println("消息发送成功");
} catch (IOException e) {
e.printStackTrace();
}finally {
RabbitMqUtils.closeConnectionAndChannel(channel,connection);
}
}
}
多个消费者:
消费者1:
package Task.fair;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMqUtils.getConnection();
final Channel channel = connection.createChannel();
try {
channel.queueDeclare("queue1", false, false, false, null);
channel.basicQos(1);//只一条一条的读
channel.basicConsume("queue1", false, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者2" + new String(body));
channel.basicAck(envelope.getDeliveryTag(), false);//1.确认队列中那个具体的消息 2.是否开启多个消息同时确认 false:手动确认
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
消费者2:
package Task.fair;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer2 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMqUtils.getConnection();
final Channel channel = connection.createChannel();
try {
channel.queueDeclare("queue1", false, false, false, null);
channel.basicQos(1);//一次只消费一条
channel.basicConsume("queue1", false, new DefaultConsumer(channel) {//必须关闭消息自动确认机制
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者1" + new String(body));
//1.确认队列中那个具体的消息 2.是否开启多个消息同时确认 false:手动确认
channel.basicAck(envelope.getDeliveryTag(), false);
}
});
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}