Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0628c6892a | |||
| d0ef5e2950 |
@@ -1,34 +0,0 @@
|
|||||||
package cn.hezhaohui.mq;
|
|
||||||
|
|
||||||
import com.rabbitmq.client.Channel;
|
|
||||||
import com.rabbitmq.client.Connection;
|
|
||||||
import com.rabbitmq.client.ConnectionFactory;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Publisher {
|
|
||||||
|
|
||||||
private static final String EXCHANGE_NAME = "direct_logs";
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception{
|
|
||||||
ConnectionFactory factory = new ConnectionFactory();
|
|
||||||
try (Connection connection = factory.newConnection();
|
|
||||||
Channel channel = connection.createChannel();
|
|
||||||
Scanner scanner = new Scanner(System.in)) {
|
|
||||||
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
|
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
String msg = scanner.nextLine();
|
|
||||||
String type = scanner.nextLine();
|
|
||||||
if (msg.equals("exit")) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
channel.basicPublish(EXCHANGE_NAME, type, null, msg.getBytes(StandardCharsets.UTF_8));
|
|
||||||
System.out.println("[" + type + "] " + msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+7
-13
@@ -9,26 +9,20 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class Subscriber {
|
public class Recv {
|
||||||
|
private final static String QUEUE_NAME = "hello";
|
||||||
private static final String EXCHANGE_NAME = "direct_logs";
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, TimeoutException {
|
public static void main(String[] args) throws IOException, TimeoutException {
|
||||||
ConnectionFactory factory = new ConnectionFactory();
|
ConnectionFactory connectionFactory = new ConnectionFactory();
|
||||||
Connection connection = factory.newConnection();
|
Connection connection = connectionFactory.newConnection();
|
||||||
Channel channel = connection.createChannel();
|
Channel channel = connection.createChannel();
|
||||||
|
|
||||||
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
|
System.out.println(" [*] Waiting for message");
|
||||||
String queueName = channel.queueDeclare().getQueue();
|
|
||||||
|
|
||||||
channel.queueBind(queueName, EXCHANGE_NAME, "warn");
|
|
||||||
|
|
||||||
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||||
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
|
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
|
||||||
System.out.println("[Receive] " +
|
System.out.println(" [x] Received '" + message + "'");
|
||||||
delivery.getEnvelope().getRoutingKey() + ": " + message);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
|
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package cn.hezhaohui.mq;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import com.rabbitmq.client.Connection;
|
||||||
|
import com.rabbitmq.client.ConnectionFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
public class Send {
|
||||||
|
private final static String QUEUE_NAME = "hello";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ConnectionFactory connectionFactory = new ConnectionFactory();
|
||||||
|
try (Connection connection = connectionFactory.newConnection();
|
||||||
|
Channel channel = connection.createChannel()){
|
||||||
|
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
||||||
|
String message = "Hello World!";
|
||||||
|
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
||||||
|
System.out.println(" [x] Sent '" + message + "'");
|
||||||
|
} catch (IOException | TimeoutException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user