RabbitConsumer.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.aliyuncs.com/webhook/putEvents?token=62ed79329bb74d0d8ede5cc5d0487a45897a6ff99f5d4d61946daf9f6e9c7fbc766b9d55ef2241edb2c2cb3aee651ac101409d2783f341aa8e8d27fdb9869d86";
  19. private static final OkHttpClient httpClient = new OkHttpClient.Builder()
  20. .connectTimeout(10, TimeUnit.SECONDS)
  21. .readTimeout(30, TimeUnit.SECONDS)
  22. .writeTimeout(30, TimeUnit.SECONDS)
  23. .build();
  24. private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
  25. @Override
  26. public Void handleRequest(Map<String, Object> input, Context context) {
  27. log.info("函数计算启动, Request ID: {}", context.getRequestId());
  28. try (Connection connection = RabbitMQHelper.createConnection();
  29. Channel channel = RabbitMQHelper.createChannel(connection, QUEUE_NAME)) {
  30. startConsumer(channel);
  31. log.info("RabbitMQ 消费者启动成功");
  32. Object val = input.get("waitSeconds");
  33. if (val instanceof Integer) {
  34. Thread.sleep(((int) val) * 1000);
  35. } else {
  36. Thread.sleep(2 * 60 * 1000);
  37. }
  38. } catch (Exception e) {
  39. log.error("启动 RabbitMQ 消费者失败", e);
  40. throw new RuntimeException("启动失败: " + e.getMessage(), e);
  41. }
  42. return null;
  43. }
  44. /**
  45. * 启动消费者
  46. */
  47. private void startConsumer(Channel channel) throws IOException {
  48. channel.basicQos(1);
  49. log.info("开始消费队列: {}", QUEUE_NAME);
  50. channel.basicConsume(QUEUE_NAME, false, (consumerTag, delivery) -> {
  51. String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
  52. log.info("收到消息: {}", message);
  53. try {
  54. processMessage(message);
  55. channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
  56. log.info("消息处理成功并已确认");
  57. } catch (Exception e) {
  58. log.error("处理消息失败", e);
  59. channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
  60. log.warn("消息已重新入队");
  61. }
  62. },
  63. consumerTag -> log.info("消费者被取消: {}", consumerTag));
  64. }
  65. private void processMessage(String message) {
  66. RequestBody body = RequestBody.create(message, JSON);
  67. Request request = new Request.Builder().url(PIPELINE_API).put(body).build();
  68. try (Response response = httpClient.newCall(request).execute()) {
  69. if (response.isSuccessful()) {
  70. String responseBody = response.body() != null ? response.body().string() : "";
  71. log.info("Pipline API 请求成功:{}", responseBody);
  72. } else {
  73. log.info("Pipline API 请求失败:{}, body:{}", response.code(), response.body() != null ? response.body().string() : "");
  74. }
  75. } catch (Exception e) {
  76. log.error("Pipline API 请求异常", e);
  77. }
  78. }
  79. }