RabbitMQ 에서 가장 많이 사용 하 는 3 대 모드 인 스 턴 스 분석
직접 모드
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class DirectProducer {
public static void main(String[] args) throws Exception {
//1. ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
//2.
Connection connection = factory.newConnection();
//3. Connection Channel
Channel channel = connection.createChannel();
//4.
String exchangeName = "test_direct_exchange";
String routingKey = "item.direct";
//5.
String msg = "this is direct msg";
channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
System.out.println("Send message : " + msg);
//6.
channel.close();
connection.close();
}
}
import com.rabbitmq.client.*;
import java.io.IOException;
public class DirectConsumer {
public static void main(String[] args) throws Exception {
//1. ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(3000);
//2.
Connection connection = factory.newConnection();
//3. Connection Channel
Channel channel = connection.createChannel();
//4.
String exchangeName = "test_direct_exchange";
String queueName = "test_direct_queue";
String routingKey = "item.direct";
channel.exchangeDeclare(exchangeName, "direct", true, false, null);
channel.queueDeclare(queueName, false, false, false, null);
// ,
channel.queueBind(queueName, exchangeName, routingKey);
//5.
Consumer consumer = new DefaultConsumer(channel) {
@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(" [x] Received '" + message + "'");
}
};
//6. Channel
channel.basicConsume(queueName, true, consumer);
}
}
Send message : this is direct msg
[x] Received 'this is direct msg'
Topic 모드마스크 를 사용 하여 모호 한 매 칭 을 할 수 있 습 니 다.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class TopicProducer {
public static void main(String[] args) throws Exception {
//1. ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
//2.
Connection connection = factory.newConnection();
//3. Connection Channel
Channel channel = connection.createChannel();
//4.
String exchangeName = "test_topic_exchange";
String routingKey1 = "item.update";
String routingKey2 = "item.delete";
String routingKey3 = "user.add";
//5.
String msg = "this is topic msg";
channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
System.out.println("Send message : " + msg);
//6.
channel.close();
connection.close();
}
}
import com.rabbitmq.client.*;
import java.io.IOException;
public class TopicConsumer {
public static void main(String[] args) throws Exception {
//1. ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(3000);
//2.
Connection connection = factory.newConnection();
//3. Connection Channel
Channel channel = connection.createChannel();
//4.
String exchangeName = "test_topic_exchange";
String queueName = "test_topic_queue";
String routingKey = "item.#";
channel.exchangeDeclare(exchangeName, "topic", true, false, null);
channel.queueDeclare(queueName, false, false, false, null);
// ,
channel.queueBind(queueName, exchangeName, routingKey);
//5.
Consumer consumer = new DefaultConsumer(channel) {
@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(" [x] Received '" + message + "'");
}
};
//6. Channel
channel.basicConsume(queueName, true, consumer);
}
}
Send message : this is topc msg
[x] Received 'this is topc msg'
[x] Received 'this is topc msg'
Fanout 모드루트 키 를 처리 하지 않 고 간단하게 대기 열 을 교환기 에 연결 해서 교환기 에 보 내 는 메 시 지 는 이 교환기 와 연 결 된 모든 대기 열 에 전 송 됩 니 다.
팬 아웃 교환기 가 메 시 지 를 전달 하 는 것 이 가장 빠르다.
import com.rabbitmq.client.*;
import java.io.IOException;
public class FanoutConsumer {
public static void main(String[] args) throws Exception {
//1. ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(3000);
//2.
Connection connection = factory.newConnection();
//3. Connection Channel
Channel channel = connection.createChannel();
//4.
String exchangeName = "test_fanout_exchange";
String queueName = "test_fanout_queue";
String routingKey = "item.#";
channel.exchangeDeclare(exchangeName, "fanout", true, false, null);
channel.queueDeclare(queueName, false, false, false, null);
// ,
channel.queueBind(queueName, exchangeName, routingKey);
//5.
Consumer consumer = new DefaultConsumer(channel) {
@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(" [x] Received '" + message + "'");
}
};
//6. Channel
channel.basicConsume(queueName, true, consumer);
}
}
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class FanoutProducer {
public static void main(String[] args) throws Exception {
//1. ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
//2.
Connection connection = factory.newConnection();
//3. Connection Channel
Channel channel = connection.createChannel();
//4.
String exchangeName = "test_fanout_exchange";
String routingKey1 = "item.update";
String routingKey2 = "";
String routingKey3 = "ookjkjjkhjhk";// routingkey
//5.
String msg = "this is fanout msg";
channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
System.out.println("Send message : " + msg);
//6.
channel.close();
connection.close();
}
}
Send message : this is fanout msg
[x] Received 'this is fanout msg'
[x] Received 'this is fanout msg'
[x] Received 'this is fanout msg'
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
RPM 패키지 설치 RabbitMQRabbitMQ 의 설 치 는 매우 간단 하 다. RabbitMQ 는 Erlang 에 의존 하기 때문에 Erlang 을 먼저 설치 하고 의존 관 계 를 해결 한 후에 RabbitMQ 를 설치 할 수 있다.Erlang...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.