java-rabbitmq-官网实例02
描述:
1.定义持久化队列,发送持久化消息,消息接受者需要手动应答,MQ才会删除队列中的消息
2.使用 channel.basicQos() 设置多个消费者时,消息将平均分发
运行:
D2_NewTask.main(); //生产者
D2_Worker.main(); //消费者
package com.example.tutorials;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import java.util.Scanner;
/**
* 1.定义持久化队列,发送持久化消息,消息接受者需要手动应答,MQ才会删除队列中的消息
* 2.使用 channel.basicQos() 设置多个消费者时,消息将平均分发
* @create 2017-08-29
* amqp-client 4.2.0
**/
public class D2_NewTask {
private static final String TASK_QUEUE_NAME = "task_queue";
/**
* 生产者, Work queues
* @param argv
* @throws Exception
*/
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//设置登录账号
factory.setHost(ServerInfo.host);
factory.setPort(ServerInfo.port);
factory.setUsername(ServerInfo.uName);
factory.setPassword(ServerInfo.uPwd);
//链接服务器
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//定义一个队列
boolean duiable=true;//队列持久化
boolean exclusive = false;//排他队列
boolean autoDelete=false;//没有consumer时,队列是否自动删除
channel.queueDeclare(TASK_QUEUE_NAME, duiable, exclusive, autoDelete, null);
//发送消息
System.out.println("输入要发送的消息,退出输入 x ");
String message = "";
do{
Scanner scanner = new Scanner(System.in);
message = scanner.next();
channel.basicPublish("", TASK_QUEUE_NAME,
MessageProperties.PERSISTENT_TEXT_PLAIN,//设置消息持久化
message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
}while(!"x".equals(message));
channel.close();
connection.close();
}
}
package com.example.tutorials;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* 1.定义持久化队列,发送持久化消息,消息接受者需要手动应答,MQ才会删除队列中的消息
* 2.使用 channel.basicQos() 设置多个消费者时,消息将平均分发
* @create 2017-08-29
* amqp-client 4.2.0
**/
public class D2_Worker {
private static final String TASK_QUEUE_NAME = "task_queue";
/**
* 消费者, Work queues
* @param argv
* @throws Exception
*/
public static void main(String[] argv) {
//多个消费者,指定了相同名称的队列,向队列发送消失时,消息将推送给随机一个消费者消费。可以 调用channel.basicQos(1) 设置后,消息将以轮询的形式推送到每个消费者
//启动两个线程,模拟消费者
new Thread(new Runnable() {
@Override
public void run() {
try {
startConsumer("consumer_01");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
startConsumer("consumer_02");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 启动一个消费者
* @param consumerName
* @throws Exception
*/
public static void startConsumer(final String consumerName) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//设置登录账号
factory.setHost(ServerInfo.host);
factory.setPort(ServerInfo.port);
factory.setUsername(ServerInfo.uName);
factory.setPassword(ServerInfo.uPwd);
//链接服务器
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
//定义一个队列
boolean duiable=true;//持久化
boolean exclusive = false;//排他队列
boolean autoDelete=false;//没有consumer时,队列是否自动删除
channel.queueDeclare(TASK_QUEUE_NAME, duiable, exclusive, autoDelete, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
//平均分发消息,有多个消费者时,消息将以轮询方式接收到消息
channel.basicQos(1);
final Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleConsumeOk(String consumerTag) {
super.handleConsumeOk(consumerTag);
System.out.println(" ["+consumerName+"] 已经注册成功! ");
System.out.println("----------------------------------------------");
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("----------------------------------------------");
System.out.println(" ["+consumerName+"] Received '" + message + "'");
try {
doWork(message);//消费消息
} finally {
System.out.println(" ["+consumerName+"] Done");
//手动发送应答,使消息在队列中被删除
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//启动消费者,接收消息
boolean autoAck=false;//自动应答,true=自动发送应答;false=手动发送应答;
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
}
/**
* 模拟延时消费消息
* @param task
*/
private static void doWork(String task) {
for (char ch : task.toCharArray()) {
if (ch == '.') {
try {
Thread.sleep(1000);
} catch (InterruptedException _ignored) {
Thread.currentThread().interrupt();
}
}
}
}
}