data-dump/src/main/java/com/celnet/datadump/config/SalesforceConnect.java
2025-10-24 11:57:15 +08:00

136 lines
5.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<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")));
}
if ("SOURCE_ORG_SEESION".equals(map1.get("code"))) {
map.put("seesionId", (String) map1.get("value"));
}
}
String username = map.get("username");
ConnectorConfig config = new ConnectorConfig();
config.setUsername(username);
config.setPassword(map.get("password"));
config.setSessionId(map.get("seesionId"));
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<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")));
}
if ("SOURCE_ORG_SEESION".equals(map1.get("code"))) {
map.put("seesionId", (String) 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);
}
}
}
}
}