package com.celnet.datadump.config; import com.alibaba.fastjson.JSONArray; import com.celnet.datadump.mapper.CustomMapper; import com.celnet.datadump.util.BulkUtil; import com.celnet.datadump.util.EmailUtil; import com.google.common.collect.Lists; import com.sforce.async.BulkConnection; import com.sforce.soap.partner.PartnerConnection; import com.sforce.ws.ConnectionException; import com.sforce.ws.ConnectorConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.concurrent.TimeUnit; /** * @author Red * @description * @date 2022/09/23 */ @Component @Slf4j public class SalesforceConnect { @Resource private CustomMapper customerMapper; /** * 创建连接 * @return */ public PartnerConnection createConnect() { int failCount = 0; //重试连接次数 while (true){ try { List> poll = customerMapper.list("code,value","org_config",null); //遍历poll,找出code值为SOURCE_ORG_URL,SOURCE_ORG_USERNAME,SOURCE_ORG_PASSWORD的value值 Map map = new HashMap<>(); for (Map map1 : poll) { if ("SOURCE_ORG_URL".equals(map1.get("code"))) { map.put("url", (String) map1.get("value")); } if ("SOURCE_ORG_USERNAME".equals(map1.get("code"))) { map.put("username", (String) map1.get("value")); } if ("SOURCE_ORG_PASSWORD".equals(map1.get("code"))) { map.put("password", String.valueOf(map1.get("value"))); } } String username = map.get("username"); ConnectorConfig config = new ConnectorConfig(); config.setUsername(username); config.setPassword(map.get("password")); String url = map.get("url"); config.setAuthEndpoint(url); config.setServiceEndpoint(url); config.setConnectionTimeout(60 * 60 * 1000); config.setReadTimeout(60 * 60 * 1000); return new PartnerConnection(config); } catch (ConnectionException e) { failCount ++; log.error("源ORG连接异常!休眠一分钟再次发起重试~~", e); try { TimeUnit.MINUTES.sleep(1); }catch (InterruptedException ie) { log.error("休眠异常", ie); } if (failCount > 3) { String message = "源ORG三次重试连接错误!"; String format = String.format("ORG连接异常!, \ncause:\n%s", message); EmailUtil.send("DataDump ERROR", format); log.error("exception message", e); } } } } /** * 创建Bulk连接 * @author kris */ public BulkConnection createBulkConnect() { int failCount = 0; while (true){ try { List> poll = customerMapper.list("code,value","org_config",null); //遍历poll,找出code值为SOURCE_ORG_URL,SOURCE_ORG_USERNAME,SOURCE_ORG_PASSWORD的value值 Map map = new HashMap<>(); for (Map map1 : poll) { if ("SOURCE_ORG_URL".equals(map1.get("code"))) { map.put("url", (String) map1.get("value")); } if ("SOURCE_ORG_USERNAME".equals(map1.get("code"))) { map.put("username", (String) map1.get("value")); } if ("SOURCE_ORG_PASSWORD".equals(map1.get("code"))) { map.put("password", String.valueOf(map1.get("value"))); } } return BulkUtil.getBulkConnection(map.get("username"),map.get("password"),map.get("url")); } catch (Exception e) { failCount ++; log.error("源ORG连接异常!休眠一分钟再次发起重试~~", e); try { TimeUnit.MINUTES.sleep(1); }catch (InterruptedException ie) { log.error("休眠异常", ie); } if (failCount > 3) { String message = "源ORG三次重试连接错误!"; String format = String.format("ORG连接异常!, \ncause:\n%s", message); EmailUtil.send("DataDump ERROR", format); log.error("exception message", e); } } } } }