工作队列
This commit is contained in:
23
src/main/java/io/jiulinxiri/rabbitmq/two/Task01.java
Normal file
23
src/main/java/io/jiulinxiri/rabbitmq/two/Task01.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package io.jiulinxiri.rabbitmq.two;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import io.jiulinxiri.rabbitmq.utils.RabbitMqUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
public class Task01 {
|
||||||
|
public static final String QUEUE_NAME = "hello";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, TimeoutException {
|
||||||
|
Channel channel = RabbitMqUtils.getChannel();
|
||||||
|
channel.queueDeclare(QUEUE_NAME,false, false,false,null);
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
while (scanner.hasNext()) {
|
||||||
|
String message = scanner.next();
|
||||||
|
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
||||||
|
System.out.println(message + " 消息发送完成!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/main/java/io/jiulinxiri/rabbitmq/two/Worker01.java
Normal file
31
src/main/java/io/jiulinxiri/rabbitmq/two/Worker01.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package io.jiulinxiri.rabbitmq.two;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.CancelCallback;
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import com.rabbitmq.client.DeliverCallback;
|
||||||
|
import io.jiulinxiri.rabbitmq.utils.RabbitMqUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消费者 01
|
||||||
|
* 工作线程 01
|
||||||
|
*/
|
||||||
|
public class Worker01 {
|
||||||
|
public static final String QUEUE_NAME = "hello";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, TimeoutException {
|
||||||
|
Channel channel = RabbitMqUtils.getChannel();
|
||||||
|
DeliverCallback deliverCallback = (coustomerTag, message) -> {
|
||||||
|
System.out.println("Worker01 接收到的消息: " + new String(message.getBody()));
|
||||||
|
};
|
||||||
|
|
||||||
|
CancelCallback cancelCallback = (coustomerTag) -> {
|
||||||
|
System.out.println("Worker01 消费中断");
|
||||||
|
};
|
||||||
|
|
||||||
|
System.out.println("Worker01 等待接收消息...");
|
||||||
|
channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/main/java/io/jiulinxiri/rabbitmq/two/Worker02.java
Normal file
31
src/main/java/io/jiulinxiri/rabbitmq/two/Worker02.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package io.jiulinxiri.rabbitmq.two;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.CancelCallback;
|
||||||
|
import com.rabbitmq.client.Channel;
|
||||||
|
import com.rabbitmq.client.DeliverCallback;
|
||||||
|
import io.jiulinxiri.rabbitmq.utils.RabbitMqUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消费者 02
|
||||||
|
* 工作线程 02
|
||||||
|
*/
|
||||||
|
public class Worker02 {
|
||||||
|
public static final String QUEUE_NAME = "hello";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, TimeoutException {
|
||||||
|
Channel channel = RabbitMqUtils.getChannel();
|
||||||
|
DeliverCallback deliverCallback = (coustomerTag, message) -> {
|
||||||
|
System.out.println("Worker02 接收到的消息: " + new String(message.getBody()));
|
||||||
|
};
|
||||||
|
|
||||||
|
CancelCallback cancelCallback = (coustomerTag) -> {
|
||||||
|
System.out.println("Worker02 消费中断");
|
||||||
|
};
|
||||||
|
|
||||||
|
System.out.println("Worker02 等待接收消息...");
|
||||||
|
channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package io.jiulinxiri.rabbitmq.utils;
|
||||||
|
|
||||||
|
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 RabbitMqUtils {
|
||||||
|
public static Channel getChannel() throws IOException, TimeoutException {
|
||||||
|
// 创建连接工程
|
||||||
|
ConnectionFactory factory = new ConnectionFactory();
|
||||||
|
// 设置连接地址
|
||||||
|
factory.setHost("shop.jiulinxiri.io");
|
||||||
|
// 设置用户名
|
||||||
|
factory.setUsername("jiulinxiri");
|
||||||
|
// 设置密码
|
||||||
|
factory.setPassword("123Abc@@");
|
||||||
|
// 建立连接
|
||||||
|
Connection connection = factory.newConnection();
|
||||||
|
// 获取信道
|
||||||
|
return connection.createChannel();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user