通过MQ实现异步调用第三方接口并等待MQ获取接口返回值 代码示例
时间: 2023-12-29 08:03:22 浏览: 149
以下是一个基于Java的MQ异步调用第三方接口示例:
```java
// 生产者代码,将请求发送到MQ
public class Producer {
private final static String QUEUE_NAME = "third-party-api";
public static void main(String[] argv) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 构造请求
String message = "http://third-party-api.com/api?param1=value1¶m2=value2";
// 发送请求
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println("Sent message: " + message);
// 关闭通道和连接
channel.close();
connection.close();
}
}
// 消费者代码,监听MQ消息并发送请求到第三方接口
public class Consumer {
private final static String QUEUE_NAME = "third-party-api";
public static void main(String[] argv) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 创建消费者,并设置回调函数处理消息
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("Received message: " + message);
// 发送请求到第三方接口
String response = sendRequest(message);
// 将接口返回值发送到MQ
channel.basicPublish("", properties.getReplyTo(), null, response.getBytes("UTF-8"));
}
};
// 监听队列,自动确认消息
channel.basicConsume(QUEUE_NAME, true, consumer);
}
// 发送请求到第三方接口,并等待接口返回值
private static String sendRequest(String request) {
// TODO: 发送请求到第三方接口,并等待接口返回值
return "response";
}
}
// 发送请求的代码,监听MQ返回值并返回到主线程
public class RequestSender {
private final static String RESPONSE_QUEUE_NAME = "third-party-api-response";
public static void main(String[] argv) throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
// 创建连接
Connection connection = factory.newConnection();
// 创建通道
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(RESPONSE_QUEUE_NAME, false, false, false, null);
// 创建消费者,并设置回调函数处理消息
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String response = new String(body, "UTF-8");
System.out.println("Received response: " + response);
// 将接口返回值返回到主线程
synchronized (this) {
this.notify();
}
}
};
// 监听队列,手动确认消息
channel.basicConsume(RESPONSE_QUEUE_NAME, false, consumer);
// 发送请求并等待返回值
String request = "http://third-party-api.com/api?param1=value1¶m2=value2";
String correlationId = UUID.randomUUID().toString();
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.correlationId(correlationId)
.replyTo(RESPONSE_QUEUE_NAME)
.build();
channel.basicPublish("", "third-party-api", props, request.getBytes("UTF-8"));
synchronized (consumer) {
consumer.wait();
}
// 关闭通道和连接
channel.close();
connection.close();
}
}
```
说明:
1. 生产者发送请求到MQ,并设置回调队列名。
2. 消费者监听MQ消息,收到消息后发送请求到第三方接口,并将接口返回值发送到回调队列。
3. 发送者发送请求到MQ,并设置回调队列名和Correlation ID。
4. 发送者等待消费者返回接口返回值,并将接口返回值返回到主线程。
阅读全文