Browse Source

Merge remote-tracking branch 'origin/feature-1127'

Hu Jie 7 years ago
parent
commit
4f7eb011b1

+ 43 - 0
src/main/java/com/uas/erp/schedular/listen/DiskUtil.java

@@ -0,0 +1,43 @@
+package com.uas.erp.schedular.listen;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+
+/**
+ * 获取mac地址工具类
+ * @Author: huj
+ * @Date: Created in 15:05 2018/11/27.
+ */
+public class DiskUtil {
+
+    public static String getMacAddress(){
+        String macAddress = "";
+        try {
+            InetAddress ia = InetAddress.getLocalHost();
+            // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
+            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
+
+            // 下面代码是把mac地址拼装成String
+            StringBuffer sb = new StringBuffer();
+
+            for (int i = 0; i < mac.length; i++) {
+                if (i != 0) {
+                    sb.append("-");
+                }
+                // mac[i] & 0xFF 是为了把byte转化为正整数
+                String s = Integer.toHexString(mac[i] & 0xFF);
+                sb.append(s.length() == 1 ? 0 + s : s);
+            }
+            // 把字符串所有小写字母改为大写成为正规的mac地址并返回
+            macAddress = sb.toString().toUpperCase();
+        } catch (SocketException e) {
+            e.printStackTrace();
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        }
+        return macAddress;
+    }
+
+}

+ 46 - 0
src/main/java/com/uas/erp/schedular/listen/ListenDomain.java

@@ -0,0 +1,46 @@
+package com.uas.erp.schedular.listen;
+
+/**
+ * @Author: huj
+ * @Date: Created in 15:34 2018/11/27.
+ */
+public class ListenDomain {
+
+    String macAddress;
+
+    String name;
+
+    String description;
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getMacAddress() {
+        return macAddress;
+    }
+
+    public void setMacAddress(String macAddress) {
+        this.macAddress = macAddress;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public ListenDomain(String macAddress, String name) {
+        this.macAddress = macAddress;
+        this.name = name;
+    }
+
+    public ListenDomain() {
+    }
+}

+ 151 - 0
src/main/java/com/uas/erp/schedular/listen/ListenTask.java

@@ -0,0 +1,151 @@
+package com.uas.erp.schedular.listen;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.TypeReference;
+import com.uas.erp.schedular.database.RestJdbcTemplate;
+import com.uas.erp.schedular.entity.Master;
+import com.uas.erp.schedular.service.SettingService;
+import com.uas.erp.schedular.util.ContextHolder;
+import com.uas.erp.schedular.web.ResultListWrap;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @Author: huj
+ * @Date: Created in 14:41 2018/11/27.
+ */
+@Component
+public class ListenTask {
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Autowired
+    private SettingService settingService;
+
+    @Autowired
+    private RestJdbcTemplate jdbcTemplate;
+
+    private String getUrl() {
+        return settingService.getValue("api.database.url");
+    }
+
+    /**
+     * 按账套环境,使用不同接口地址
+     * @return
+     */
+    private String getApiDomain() {
+        String key = null;
+        if (null == ContextHolder.getMaster()) {
+            key = "api.b2b.test.url";
+        } else {
+            key = "test".equals(ContextHolder.getMaster().getMa_env()) ? "api.b2b.test.url" : "api.b2b.url";
+        }
+        return settingService.getValue(key);
+    }
+
+    /**
+     * 每十分钟发送一次
+     */
+    @Scheduled(cron = "0 */10 * * * *")
+    public void listen() {
+        List<Master> masters = new ArrayList<>();
+        try {
+            String resultStr = restTemplate.getForObject(getUrl() + "/v1/master/list?cloudEnabled=true", String.class);
+            ResultListWrap<Master> result = JSON.parseObject(resultStr, new TypeReference<ResultListWrap<Master>>(Master.class) {
+            });
+            if (result.isSuccess()) {
+                masters = result.getContent();
+            }
+        } catch (Exception e) {
+
+        } finally {
+            if (CollectionUtils.isEmpty(masters)) {
+                ListenDomain listenDomain = new ListenDomain(DiskUtil.getMacAddress(), "" );
+                List<ListenDomain> listenDomains = new ArrayList<>();
+                listenDomain.setDescription("h2数据库查询异常或oracle数据库设置异常");
+                listenDomains.add(listenDomain);
+                postForList(getApiDomain() + "/erp/listen", ListenDomain.class, dataWrap(listenDomains));
+                return;
+            }
+        }
+        Iterator<Master> iterator = masters.iterator();
+        while (iterator.hasNext()) {
+            Master checkProd = iterator.next();
+            if (!"prod".equals(checkProd.getMa_env())) {
+                iterator.remove();
+            }
+        }
+        if (!CollectionUtils.isEmpty(masters)) {
+            ContextHolder.setMaster(masters.get(0));
+            int dual = jdbcTemplate.getInt("select 1 from dual");
+            if (dual == 1) {
+                StringBuilder str = new StringBuilder();
+                for (Master master : masters) {
+                    if ("".equals(str.toString())) {
+                        str.append(master.getMa_function());
+                    } else {
+                        str.append("、" + master.getMa_function());
+                    }
+                }
+                ListenDomain listenDomain = new ListenDomain(DiskUtil.getMacAddress(),
+                        str == null ? "" : str.toString());
+                List<ListenDomain> listenDomains = new ArrayList<>();
+                listenDomains.add(listenDomain);
+                postForList(getApiDomain() + "/erp/listen", ListenDomain.class, dataWrap(listenDomains));
+            } else {
+                ListenDomain listenDomain = new ListenDomain(DiskUtil.getMacAddress(), "" );
+                List<ListenDomain> listenDomains = new ArrayList<>();
+                listenDomain.setDescription("oracle查询异常");
+                listenDomains.add(listenDomain);
+                postForList(getApiDomain() + "/erp/listen", ListenDomain.class, dataWrap(listenDomains));
+                return;
+            }
+            ContextHolder.clear();
+        }
+    }
+
+    /**
+     * 封装成平台接口数据格式
+     * @param data
+     * @return
+     */
+    protected static MultiValueMap<String, String> dataWrap(Object data) {
+        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
+        String dataStr =  JSON.toJSONString(data);
+        try {
+            dataStr = URLEncoder.encode(dataStr, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+
+        }
+        map.set("data", dataStr);
+        return map;
+    }
+
+    protected ResponseEntity postForEntity(String url, MultiValueMap<String, String> vars) {
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(vars, headers);
+        return restTemplate.postForEntity(url, request, String.class);
+    }
+
+    protected <T> List<T> postForList(String url, Class<T> objectClass, MultiValueMap<String, String> vars) {
+        ResponseEntity<String> resultEntity = postForEntity(url, vars);
+        return JSON.parseArray(resultEntity.getBody(), objectClass);
+    }
+}

+ 4 - 0
src/main/java/com/uas/erp/schedular/util/ContextHolder.java

@@ -13,6 +13,10 @@ public class ContextHolder {
         threadLocalMaster.set(master);
     }
 
+    public static void clear() {
+        threadLocalMaster.set(null);
+    }
+
     public static Master getMaster() {
         return threadLocalMaster.get();
     }