RabbitConsumer.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package net.yaoyi.pipeline.rabbitmq;
  2. import com.aliyun.fc.runtime.Context;
  3. import com.aliyun.fc.runtime.PojoRequestHandler;
  4. import com.rabbitmq.client.Channel;
  5. import com.rabbitmq.client.Connection;
  6. import lombok.extern.slf4j.Slf4j;
  7. import okhttp3.*;
  8. import java.io.IOException;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.Map;
  11. import java.util.concurrent.TimeUnit;
  12. /**
  13. * RabbitMQ 消费者 - 阿里云函数计算实现
  14. */
  15. @Slf4j
  16. public class RabbitConsumer implements PojoRequestHandler<Map<String, Object>, Void> {
  17. private static final String QUEUE_NAME = "vector.search.task.img.dul.check.queue";
  18. private static final String PIPELINE_API = "http://1861991840278303.eventbridge.cn-beijing-vpc.aliyuncs.com/webhook/putEvents?token=62ed79329bb74d0d8ede5cc5d0487a45897a6ff99f5d4d61946daf9f6e9c7fbc766b9d55ef2241edb2c2cb3aee651ac101409d2783f341aa8e8d27fdb9869d86";
  19. private static Connection connection;
  20. private static Channel channel;
  21. private static final OkHttpClient httpClient = new OkHttpClient.Builder()
  22. .connectTimeout(10, TimeUnit.SECONDS)
  23. .readTimeout(30, TimeUnit.SECONDS)
  24. .writeTimeout(30, TimeUnit.SECONDS)
  25. .build();
  26. private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
  27. @Override
  28. public Void handleRequest(Map<String, Object> input, Context context) {
  29. log.info("函数计算启动, Request ID: {}", context.getRequestId());
  30. try {
  31. // 初始化 RabbitMQ 连接
  32. initRabbitMQ();
  33. // 启动消费者
  34. startConsumer();
  35. log.info("RabbitMQ 消费者启动成功");
  36. } catch (Exception e) {
  37. log.error("启动 RabbitMQ 消费者失败", e);
  38. throw new RuntimeException("启动失败: " + e.getMessage(), e);
  39. }
  40. try {
  41. Thread.sleep(3000);
  42. } catch (InterruptedException e) {
  43. Thread.currentThread().interrupt();
  44. }
  45. RabbitMQHelper.close(channel, connection);
  46. return null;
  47. }
  48. /**
  49. * 初始化 RabbitMQ 连接
  50. */
  51. private void initRabbitMQ() {
  52. if (connection == null) {
  53. connection = RabbitMQHelper.createConnection();
  54. channel = RabbitMQHelper.createChannel(connection, QUEUE_NAME);
  55. }
  56. }
  57. /**
  58. * 启动消费者
  59. */
  60. private void startConsumer() throws IOException {
  61. channel.basicQos(1);
  62. log.info("开始消费队列: {}", QUEUE_NAME);
  63. channel.basicConsume(QUEUE_NAME, false, (consumerTag, delivery) -> {
  64. String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
  65. log.info("收到消息: {}", message);
  66. try {
  67. processMessage(message);
  68. channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
  69. log.info("消息处理成功并已确认");
  70. } catch (Exception e) {
  71. log.error("处理消息失败", e);
  72. channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
  73. log.warn("消息已重新入队");
  74. }
  75. },
  76. consumerTag -> log.info("消费者被取消: {}", consumerTag));
  77. }
  78. private void processMessage(String message) {
  79. RequestBody body = RequestBody.create(message, JSON);
  80. Request request = new Request.Builder().url(PIPELINE_API).put(body).build();
  81. try (Response response = httpClient.newCall(request).execute()) {
  82. if (response.isSuccessful()) {
  83. String responseBody = response.body() != null ? response.body().string() : "";
  84. log.info("Pipline API 请求成功:{}", responseBody);
  85. } else {
  86. log.info("Pipline API 请求失败:{}", response.code());
  87. }
  88. } catch (Exception e) {
  89. log.error("Pipline API 请求异常", e);
  90. }
  91. }
  92. }