data-dump/src/main/java/com/celnet/datadump/config/SalesforceConnect.java

144 lines
5.8 KiB
Java
Raw Normal View History

2025-03-28 17:38:34 +08:00
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;
2025-03-28 17:38:34 +08:00
import com.google.common.collect.Lists;
import com.sforce.async.BulkConnection;
2025-03-28 17:38:34 +08:00
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import lombok.extern.slf4j.Slf4j;
2025-10-30 10:42:10 +08:00
import org.apache.commons.lang3.StringUtils;
2025-03-28 17:38:34 +08:00
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;
2025-08-19 10:44:47 +08:00
import java.util.concurrent.TimeUnit;
2025-03-28 17:38:34 +08:00
/**
* @author Red
* @description
* @date 2022/09/23
*/
@Component
@Slf4j
public class SalesforceConnect {
@Resource
private CustomMapper customerMapper;
/**
* 创建连接
* @return
*/
2025-08-19 10:44:47 +08:00
public PartnerConnection createConnect() {
int failCount = 0;
//重试连接次数
while (true){
try {
List<Map<String, Object>> poll = customerMapper.list("code,value","org_config",null);
//遍历poll,找出code值为SOURCE_ORG_URLSOURCE_ORG_USERNAMESOURCE_ORG_PASSWORD的value值
Map<String, String> map = new HashMap<>();
for (Map<String, Object> 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")));
}
2025-10-30 10:42:10 +08:00
if ("SOURCE_ORG_TOKEN".equals(map1.get("code"))) {
map.put("token", map1.get("value") == null ? "" :(String) map1.get("value"));
2025-10-24 11:56:39 +08:00
}
2025-03-28 17:38:34 +08:00
}
2025-08-19 10:44:47 +08:00
String username = map.get("username");
ConnectorConfig config = new ConnectorConfig();
config.setUsername(username);
2025-10-30 10:42:10 +08:00
if (StringUtils.isNotEmpty(map.get("token"))){
config.setPassword(map.get("password") + map.get("token"));
}else {
config.setPassword(map.get("password"));
}
2025-08-19 10:44:47 +08:00
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);
2025-03-28 17:38:34 +08:00
}
2025-08-19 10:44:47 +08:00
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);
2025-03-28 17:38:34 +08:00
}
}
}
2025-08-19 10:44:47 +08:00
}
/**
* 创建Bulk连接
* @author kris
*/
public BulkConnection createBulkConnect() {
2025-08-19 10:44:47 +08:00
int failCount = 0;
while (true){
try {
List<Map<String, Object>> poll = customerMapper.list("code,value","org_config",null);
//遍历poll,找出code值为SOURCE_ORG_URLSOURCE_ORG_USERNAMESOURCE_ORG_PASSWORD的value值
Map<String, String> map = new HashMap<>();
for (Map<String, Object> 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")));
}
2025-10-30 10:42:10 +08:00
if ("SOURCE_ORG_TOKEN".equals(map1.get("code"))) {
map.put("token", map1.get("value") == null ? "" :(String) map1.get("value"));
2025-10-24 11:56:39 +08:00
}
}
2025-10-30 10:42:10 +08:00
if (StringUtils.isNotEmpty(map.get("token"))){
return BulkUtil.getBulkConnection(map.get("username"),map.get("password") + map.get("token"),map.get("url"));
}else {
return BulkUtil.getBulkConnection(map.get("username"),map.get("password"),map.get("url"));
}
2025-08-19 10:44:47 +08:00
} 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);
}
}
2025-03-28 17:38:34 +08:00
}
}
}