|
|
@@ -4,8 +4,6 @@ import com.aliyun.fc.runtime.Context;
|
|
|
import com.aliyun.fc.runtime.PojoRequestHandler;
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import com.rabbitmq.client.Connection;
|
|
|
-import com.rabbitmq.client.ConnectionFactory;
|
|
|
-import com.rabbitmq.client.DeliverCallback;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
|
|
|
@@ -13,24 +11,17 @@ import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
-import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
/**
|
|
|
* RabbitMQ 消费者 - 阿里云函数计算实现
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class RabbitConsumer implements PojoRequestHandler<Map<String,Object>, Void> {
|
|
|
+public class RabbitConsumer implements PojoRequestHandler<Map<String, Object>, Void> {
|
|
|
|
|
|
- private static final String RABBITMQ_HOST = "rabbitmq-cn-39y4twfud01-cn-beijing-vpc.mq.amqp.aliyuncs.com";
|
|
|
- private static final int RABBITMQ_PORT = 5672;
|
|
|
- private static final String RABBITMQ_USERNAME = "MjpyYWJiaXRtcS1jbi0zOXk0dHdmdWQwMTpMVEFJNXQ2SDR1OGFxb3A3cGViNkRSOFo=";
|
|
|
- private static final String RABBITMQ_PASSWORD = "MDdCQ0FFNjFEMTQyMEZDN0UyMThDNkM4MDgwMkI5QzgyNjAzQTdGQzoxNzgyMTk2NzQxNjcy";
|
|
|
private static final String QUEUE_NAME = "vector.search.task.img.dul.check.queue";
|
|
|
- private static final String V_HOST = "test";
|
|
|
|
|
|
private static final String PIPELINE_API = "http://1861991840278303.eventbridge.cn-beijing.aliyuncs.com/webhook/putEvents?token=63b99b24a3d545d880fa86f57c5ae56cae00c81d2fc74d058e9ace260515163132f6e27deeaa4bd4bc8e2a21cf6b6d6517b8d1e6d33c4de89f00c3524a54095b";
|
|
|
|
|
|
- private static ConnectionFactory factory;
|
|
|
private static Connection connection;
|
|
|
private static Channel channel;
|
|
|
private static final OkHttpClient httpClient = new OkHttpClient.Builder()
|
|
|
@@ -41,7 +32,7 @@ public class RabbitConsumer implements PojoRequestHandler<Map<String,Object>, Vo
|
|
|
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
|
|
|
@Override
|
|
|
- public Void handleRequest(Map<String,Object> input, Context context) {
|
|
|
+ public Void handleRequest(Map<String, Object> input, Context context) {
|
|
|
log.info("函数计算启动, Request ID: {}", context.getRequestId());
|
|
|
|
|
|
try {
|
|
|
@@ -60,28 +51,18 @@ public class RabbitConsumer implements PojoRequestHandler<Map<String,Object>, Vo
|
|
|
} catch (InterruptedException e) {
|
|
|
Thread.currentThread().interrupt();
|
|
|
}
|
|
|
+
|
|
|
+ RabbitMQHelper.close(channel, connection);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化 RabbitMQ 连接
|
|
|
*/
|
|
|
- private void initRabbitMQ() throws IOException, TimeoutException {
|
|
|
- if (factory == null) {
|
|
|
- factory = new ConnectionFactory();
|
|
|
- factory.setHost(RABBITMQ_HOST);
|
|
|
- factory.setPort(RABBITMQ_PORT);
|
|
|
- factory.setUsername(RABBITMQ_USERNAME);
|
|
|
- factory.setPassword(RABBITMQ_PASSWORD);
|
|
|
- factory.setVirtualHost(V_HOST);
|
|
|
-
|
|
|
- log.info("连接 RabbitMQ: {}:{}", RABBITMQ_HOST, RABBITMQ_PORT);
|
|
|
- connection = factory.newConnection();
|
|
|
- channel = connection.createChannel();
|
|
|
-
|
|
|
- // 声明队列
|
|
|
- channel.queueDeclare(QUEUE_NAME, true, false, false, null);
|
|
|
- log.info("队列声明成功: {}", QUEUE_NAME);
|
|
|
+ private void initRabbitMQ() {
|
|
|
+ if (connection == null) {
|
|
|
+ connection = RabbitMQHelper.createConnection();
|
|
|
+ channel = RabbitMQHelper.createChannel(connection, QUEUE_NAME);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -92,24 +73,21 @@ public class RabbitConsumer implements PojoRequestHandler<Map<String,Object>, Vo
|
|
|
channel.basicQos(1);
|
|
|
|
|
|
log.info("开始消费队列: {}", QUEUE_NAME);
|
|
|
- DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
|
|
- String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
|
|
|
- log.info("收到消息: {}", message);
|
|
|
-
|
|
|
- try {
|
|
|
- processMessage(message);
|
|
|
- channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
|
|
- log.info("消息处理成功并已确认");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("处理消息失败", e);
|
|
|
- channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
|
|
|
- log.warn("消息已重新入队");
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {
|
|
|
- log.info("消费者被取消: {}", consumerTag);
|
|
|
- });
|
|
|
+ channel.basicConsume(QUEUE_NAME, false, (consumerTag, delivery) -> {
|
|
|
+ String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
|
|
|
+ log.info("收到消息: {}", message);
|
|
|
+
|
|
|
+ try {
|
|
|
+ processMessage(message);
|
|
|
+ channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
|
|
+ log.info("消息处理成功并已确认");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理消息失败", e);
|
|
|
+ channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
|
|
|
+ log.warn("消息已重新入队");
|
|
|
+ }
|
|
|
+ },
|
|
|
+ consumerTag -> log.info("消费者被取消: {}", consumerTag));
|
|
|
}
|
|
|
|
|
|
private void processMessage(String message) {
|