浏览代码

维多利亚

home 2 年之前
父节点
当前提交
0075e91bdf

+ 0 - 63
hx-service/syringe-production/src/main/java/com/fjhx/config/IoTDAConfig.java

@@ -1,63 +0,0 @@
-package com.fjhx.config;
-
-import com.fjhx.config.amqp.AmqpClient;
-import com.fjhx.config.amqp.AmqpClientOptions;
-import com.fjhx.config.amqp.AmqpConstants;
-import com.huaweicloud.sdk.core.auth.ICredential;
-import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
-import com.huaweicloud.sdk.iotda.v5.auth.IoTDACredentials;
-import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-public class IoTDAConfig {
-
-    // REGION_ID:如果是上海一,请填写"cn-east-3";如果是北京四,请填写"cn-north-4";如果是华南广州,请填写"cn-south-4"
-    private static final String REGION_ID = "cn-north-4";
-    // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
-    private static final String ENDPOINT = "a1625d5cc8.iotda.cn-north-4.myhuaweicloud.com";
-
-    private static final String ak = "VPOCS8WGONHWOBOZNVVJ";
-    private static final String sk = "4mvyTAn75I0hvyAm8rKxuXi6TSyGDG5yblW43ci1";
-    private static final String projectId = "0f130b202d00f4e52fc0c01deb21f36a";
-
-    /**
-     * IoTDA 操作
-     */
-    @Bean
-    public IoTDAClient ioTDAClient() {
-        // 创建认证
-        ICredential auth = new IoTDACredentials()
-                .withAk(ak)
-                .withSk(sk)
-                .withProjectId(projectId);
-
-        // 创建IoTDAClient实例并初始化
-        return IoTDAClient.newBuilder()
-                .withCredential(auth)
-                // 基础版:请选择IoTDARegion中的Region对象
-                .withRegion(IoTDARegion.CN_NORTH_4)
-                // 标准版/企业版:需自行创建Region对象
-                // .withRegion(new Region(REGION_ID, ENDPOINT))
-                .build();
-    }
-
-    /**
-     * IoTDA 监听
-     */
-    @Bean
-    public AmqpClient amqpClient() throws Exception {
-        AmqpClientOptions options = AmqpClientOptions.builder()
-                .host(AmqpConstants.HOST)
-                .port(AmqpConstants.PORT)
-                .accessKey(AmqpConstants.ACCESS_KEY)
-                .accessCode(AmqpConstants.ACCESS_CODE)
-                .queuePrefetch(1000) // sdk会在内存中分配该参数大小的队列,用来接收消息,客户端内存较小的情况可以调小该参数。
-                .build();
-        AmqpClient amqpClient = new AmqpClient(options);
-        amqpClient.initialize();
-        return amqpClient;
-    }
-
-}

+ 38 - 10
hx-service/syringe-production/src/main/java/com/fjhx/service/amqp/impl/AmqpDataServiceImpl.java

@@ -2,18 +2,18 @@ package com.fjhx.service.amqp.impl;
 
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.fjhx.config.amqp.AmqpClient;
-import com.fjhx.config.amqp.AmqpConstants;
+import com.fjhx.utils.amqp.AmqpClient;
+import com.fjhx.utils.amqp.AmqpClientOptions;
+import com.fjhx.utils.amqp.AmqpConstants;
 import com.fjhx.constants.IotProductionRedisPrefixConstant;
 import com.fjhx.entity.amqp.AmqpData;
 import com.fjhx.mapper.amqp.AmqpDataMapper;
 import com.fjhx.service.amqp.AmqpDataService;
 import lombok.extern.slf4j.Slf4j;
 import org.springblade.core.redis.cache.BladeRedis;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import javax.jms.JMSException;
 import javax.jms.Message;
@@ -33,16 +33,22 @@ import java.util.stream.Collectors;
  */
 @Service
 @Slf4j
-public class AmqpDataServiceImpl extends ServiceImpl<AmqpDataMapper, AmqpData> implements AmqpDataService, CommandLineRunner {
-
-    @Autowired
-    private AmqpClient amqpClient;
+public class AmqpDataServiceImpl extends ServiceImpl<AmqpDataMapper, AmqpData> implements AmqpDataService {
 
     @Resource
     private BladeRedis redisCache;
 
-    @Override
-    public void run(String... args) throws Exception {
+    @PostConstruct
+    public void listener() throws Exception {
+        AmqpClientOptions options = AmqpClientOptions.builder()
+                .host(AmqpConstants.HOST)
+                .port(AmqpConstants.PORT)
+                .accessKey(AmqpConstants.ACCESS_KEY)
+                .accessCode(AmqpConstants.ACCESS_CODE)
+                .queuePrefetch(1000) // sdk会在内存中分配该参数大小的队列,用来接收消息,客户端内存较小的情况可以调小该参数。
+                .build();
+        AmqpClient amqpClient = new AmqpClient(options);
+        amqpClient.initialize();
         MessageConsumer consumer = amqpClient.newConsumer(AmqpConstants.DEFAULT_QUEUE);
         consumer.setMessageListener(message -> {
             try {
@@ -54,6 +60,28 @@ public class AmqpDataServiceImpl extends ServiceImpl<AmqpDataMapper, AmqpData> i
         });
     }
 
+//    @PostConstruct
+//    public void listener2() throws Exception {
+//        AmqpClientOptions options = AmqpClientOptions.builder()
+//                .host(AmqpConstants.HOST)
+//                .port(AmqpConstants.PORT)
+//                .accessKey("oZvmUesgndzlw75ShbswJOe36algwcvaGBmOAsxc")
+//                .accessCode("Y6JLYH6QV0LX5AJDXREM")
+//                .queuePrefetch(1000) // sdk会在内存中分配该参数大小的队列,用来接收消息,客户端内存较小的情况可以调小该参数。
+//                .build();
+//        AmqpClient amqpClient = new AmqpClient(options);
+//        amqpClient.initialize();
+//        MessageConsumer consumer = amqpClient.newConsumer(AmqpConstants.DEFAULT_QUEUE);
+//        consumer.setMessageListener(message -> {
+//            try {
+//                System.err.println(message);
+//            } catch (Exception e) {
+//                log.warn("message.getBody error,exception is ", e);
+//            }
+//        });
+//
+//    }
+
     private void saveData(Message message) {
         try {
 

+ 1 - 1
hx-service/syringe-production/src/main/java/com/fjhx/config/amqp/AmqpClient.java → hx-service/syringe-production/src/main/java/com/fjhx/utils/amqp/AmqpClient.java

@@ -1,4 +1,4 @@
-package com.fjhx.config.amqp;
+package com.fjhx.utils.amqp;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.qpid.jms.JmsConnection;

+ 1 - 1
hx-service/syringe-production/src/main/java/com/fjhx/config/amqp/AmqpClientOptions.java → hx-service/syringe-production/src/main/java/com/fjhx/utils/amqp/AmqpClientOptions.java

@@ -1,4 +1,4 @@
-package com.fjhx.config.amqp;
+package com.fjhx.utils.amqp;
 
 
 import cn.hutool.core.util.ObjectUtil;

+ 1 - 1
hx-service/syringe-production/src/main/java/com/fjhx/config/amqp/AmqpConstants.java → hx-service/syringe-production/src/main/java/com/fjhx/utils/amqp/AmqpConstants.java

@@ -1,4 +1,4 @@
-package com.fjhx.config.amqp;
+package com.fjhx.utils.amqp;
 
 
 public interface AmqpConstants {