轉載出處:http://blog.csdn.net/liangwenmail/article/details/60877179
1 消息輪詢:默認的情況下,服務器會將消息發送給下一個消費者,使得每個消費者獲得消息數量儘量相同。
2 消息確認機制:默認手動確認,需要消費者反饋給服務器一個確認信號,告知服務器可以在消息隊列中刪除消息,如果服務器未收到確認信號若消費者斷開鏈接,將會把消息發送給下一個消費者,使得消息不會丟失。自動確認,服務器端會在消費者取得消息後在隊列中刪除消息,若此時消費者未處理完消息宕掉了,消息會丟失。如果手動確認忘記反饋確認信號,則消息在服務器佔用的內存越來越多,應該記得反饋確認信號給服務器;
生產者:
- package queue;
- import java.io.IOException;
- import java.util.concurrent.TimeoutException;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class SendNewTask {
- private final static String QUEUE_NAME = “newTask”;
- public static void main(String[] args) throws IOException, TimeoutException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost(”localhost”);
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- String[] msg={”hello…word…”,“hash…map…”};
- String message = getMessage(msg);
- channel.basicPublish(”“, QUEUE_NAME, null, message.getBytes());
- System.out.println(”[x] Sent ’” + message + “’”);
- channel.close();
- connection.close();
- }
- private static String getMessage(String[] strings){
- if (strings.length < 1)
- return “Hello World!”;
- return joinStrings(strings, “ ”);
- }
- private static String joinStrings(String[] strings, String delimiter) {
- int length = strings.length;
- if (length == 0) return “”;
- StringBuilder words = new StringBuilder(strings[0]);
- for (int i = 1; i < length; i++) {
- words.append(delimiter).append(strings[i]);
- }
- return words.toString();
- }
- }
package queue;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class SendNewTask {
private final static String QUEUE_NAME = "newTask";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String[] msg={"hello...word...","hash...map..."};
String message = getMessage(msg);
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("[x] Sent '" + message + "'");
channel.close();
connection.close();
}
private static String getMessage(String[] strings){
if (strings.length < 1)
return "Hello World!";
return joinStrings(strings, " ");
}
private static String joinStrings(String[] strings, String delimiter) {
int length = strings.length;
if (length == 0) return "";
StringBuilder words = new StringBuilder(strings[0]);
for (int i = 1; i < length; i++) {
words.append(delimiter).append(strings[i]);
}
return words.toString();
}
}
消費者 1
- package queue;
- import java.io.IOException;
- import java.util.Calendar;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.ConsumerCancelledException;
- import com.rabbitmq.client.QueueingConsumer;
- import com.rabbitmq.client.ShutdownSignalException;
- public class ReqvWork {
- private final static String QUEUE_NAME = “newTask”;
- public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost(”localhost”);
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(” [*] Waiting for messages. To exit press CTRL+C”);
- QueueingConsumer consumer = new QueueingConsumer(channel); //QueueingConsumer緩存從服務器發來的消息
- //第二個參數true,自動確認機制,如果消息消費者和服務器斷開鏈接會丟失消息;false 關閉自動確認機制,手動確認
- channel.basicConsume(QUEUE_NAME, false, consumer);
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery(); //在另一個來自服務器的消息到來之前它會一直阻塞着
- String message = new String(delivery.getBody());
- doWork(message);
- System.out.println(” [x] Received ’” + message + “’”);
- System.out.println(” [x] done”);
- channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); //手動消息確認,反饋確認信息
- }
- }
- private static void doWork(String task) throws InterruptedException {
- for (char ch: task.toCharArray()) {
- if (ch == ‘.’) Thread.sleep(1000);
- }
- }
- }
package queue;
import java.io.IOException;
import java.util.Calendar;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
public class ReqvWork {
private final static String QUEUE_NAME = "newTask";
public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel); //QueueingConsumer緩存從服務器發來的消息
//第二個參數true,自動確認機制,如果消息消費者和服務器斷開鏈接會丟失消息;false 關閉自動確認機制,手動確認
channel.basicConsume(QUEUE_NAME, false, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery(); //在另一個來自服務器的消息到來之前它會一直阻塞着
String message = new String(delivery.getBody());
doWork(message);
System.out.println(" [x] Received '" + message + "'");
System.out.println(" [x] done");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); //手動消息確認,反饋確認信息
}
}
private static void doWork(String task) throws InterruptedException {
for (char ch: task.toCharArray()) {
if (ch == '.') Thread.sleep(1000);
}
}
}
消費者2
- package queue;
- import java.io.IOException;
- import java.util.Calendar;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.ConsumerCancelledException;
- import com.rabbitmq.client.QueueingConsumer;
- import com.rabbitmq.client.ShutdownSignalException;
- public class ReqvWork1 {
- private final static String QUEUE_NAME = “newTask”;
- public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost(”localhost”);
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(” [*] Waiting for messages. To exit press CTRL+C”);
- QueueingConsumer consumer = new QueueingConsumer(channel); //QueueingConsumer緩存從服務器發來的消息
- //第二個參數true,自動確認機制,如果消息消費者和服務器斷開鏈接會丟失消息;false 關閉自動確認機制,手動確認
- channel.basicConsume(QUEUE_NAME, false, consumer);
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery(); //在另一個來自服務器的消息到來之前它會一直阻塞着
- String message = new String(delivery.getBody());
- doWork(message);
- System.out.println(” [x1] Received ’” + message + “’”);
- System.out.println(” [x1] done”);
- channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); //手動消息確認
- }
- }
- private static void doWork(String task) throws InterruptedException {
- for (char ch: task.toCharArray()) {
- if (ch == ‘.’) Thread.sleep(1000);
- }
- }
- }
package queue;
import java.io.IOException;
import java.util.Calendar;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
public class ReqvWork1 {
private final static String QUEUE_NAME = "newTask";
public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel); //QueueingConsumer緩存從服務器發來的消息
//第二個參數true,自動確認機制,如果消息消費者和服務器斷開鏈接會丟失消息;false 關閉自動確認機制,手動確認
channel.basicConsume(QUEUE_NAME, false, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery(); //在另一個來自服務器的消息到來之前它會一直阻塞着
String message = new String(delivery.getBody());
doWork(message);
System.out.println(" [x1] Received '" + message + "'");
System.out.println(" [x1] done");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); //手動消息確認
}
}
private static void doWork(String task) throws InterruptedException {
for (char ch: task.toCharArray()) {
if (ch == '.') Thread.sleep(1000);
}
}
}