修改和SAP连接的方式,使用工厂类,避免重复连接SAP系统。

highway
Yangwl 12 months ago
parent e1e5b8f1fc
commit da12afa045

@ -67,7 +67,6 @@ public class DestinationManager {
properties.store(fos, "for test only!"); properties.store(fos, "for test only!");
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
// System.out.println("Create Data file fault,error msg:" + e.toString());
throw new RuntimeException("无法创建目标文件" + cfg.getName()); throw new RuntimeException("无法创建目标文件" + cfg.getName());
} }
} }

@ -23,12 +23,14 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component
@Service @Service
public class SapBomServiceImpl implements SapBomService { public class SapBomServiceImpl implements SapBomService {
private static final String END_DATE="99991231"; private static final String END_DATE="99991231";
@ -41,13 +43,16 @@ public class SapBomServiceImpl implements SapBomService {
@Autowired @Autowired
private SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
@Autowired
private JCoDestination dest;
@Override @Override
public R bomSync(SapBomQuery sapBomQuery) { public R bomSync(SapBomQuery sapBomQuery) {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); //JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_BOM"); JCoFunction func = repository.getFunction("ZPPR_MES_BOM");
if (func == null) { if (func == null) {

@ -20,6 +20,7 @@ import com.sap.conn.jco.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.validation.Validator; import javax.validation.Validator;
@ -28,6 +29,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Component
@Service @Service
public class SapItemSyncImpl implements SapItemSyncService { public class SapItemSyncImpl implements SapItemSyncService {
@ -41,13 +43,16 @@ public class SapItemSyncImpl implements SapItemSyncService {
@Autowired @Autowired
protected Validator validator; protected Validator validator;
@Autowired
private JCoDestination dest;
private static final Logger log = LoggerFactory.getLogger(SapItemSyncImpl.class); private static final Logger log = LoggerFactory.getLogger(SapItemSyncImpl.class);
@Override @Override
public R itemSync(SapItemQuery sapItemQuery) { public R itemSync(SapItemQuery sapItemQuery) {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZMMR_MES_MAT_GET"); JCoFunction func = repository.getFunction("ZMMR_MES_MAT_GET");
@ -200,7 +205,7 @@ public class SapItemSyncImpl implements SapItemSyncService {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_GOODSMVT_CREATE"); JCoFunction func = repository.getFunction("ZPPR_MES_GOODSMVT_CREATE");
// 配置传入参数 // 配置传入参数

@ -9,17 +9,22 @@ import com.op.sap.util.SAPConnUtils;
import com.op.system.api.domain.sap.SapMaterialPreparation; import com.op.system.api.domain.sap.SapMaterialPreparation;
import com.sap.conn.jco.*; import com.sap.conn.jco.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Component
@Service @Service
public class SapMaterialPreparationServiceImpl implements SapMaterialPreparationService { public class SapMaterialPreparationServiceImpl implements SapMaterialPreparationService {
@Autowired @Autowired
SapMaterialPreparationMapper sapMaterialPreparationMapper; SapMaterialPreparationMapper sapMaterialPreparationMapper;
@Autowired
private JCoDestination dest;
@Override @Override
public R<List<SapMaterialPreparation>> MaterialPreparation(String shopOrder) { public R<List<SapMaterialPreparation>> MaterialPreparation(String shopOrder) {
// 连接 // 连接
@ -27,7 +32,7 @@ public class SapMaterialPreparationServiceImpl implements SapMaterialPreparation
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_MATERIAL_PREPARATION"); JCoFunction func = repository.getFunction("ZPPR_MES_MATERIAL_PREPARATION");
if (func == null) { if (func == null) {

@ -15,6 +15,8 @@ import com.op.system.api.domain.sap.SapShopOrderQuery;
import com.op.system.api.model.SapProOrder; import com.op.system.api.model.SapProOrder;
import com.sap.conn.jco.*; import com.sap.conn.jco.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,10 +25,13 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Component
@Service @Service
public class SapOrderServiceImpl implements SapOrderService { public class SapOrderServiceImpl implements SapOrderService {
private static final String END_DATE="99991231"; private static final String END_DATE="99991231";
@Autowired
private JCoDestination dest;
@Override @Override
public R<List<SapProOrder>> shopOrderSync(SapShopOrderQuery sapShopOrderQuery) { public R<List<SapProOrder>> shopOrderSync(SapShopOrderQuery sapShopOrderQuery) {
@ -34,7 +39,7 @@ public class SapOrderServiceImpl implements SapOrderService {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); //JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_PRO_TO"); JCoFunction func = repository.getFunction("ZPPR_MES_PRO_TO");
if (func == null) { if (func == null) {
@ -147,7 +152,7 @@ public class SapOrderServiceImpl implements SapOrderService {
String L_MSG=null; String L_MSG=null;
try { try {
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_PRODORD_CREATE"); JCoFunction func = repository.getFunction("ZPPR_MES_PRODORD_CREATE");
if (func == null) { if (func == null) {
@ -200,7 +205,7 @@ public class SapOrderServiceImpl implements SapOrderService {
String L_MSG=null; String L_MSG=null;
try { try {
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_PRODORD_CLOSE"); JCoFunction func = repository.getFunction("ZPPR_MES_PRODORD_CLOSE");
if (func == null) { if (func == null) {
@ -232,7 +237,7 @@ public class SapOrderServiceImpl implements SapOrderService {
String MESSAGE=null; String MESSAGE=null;
try { try {
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_PRODORDCONF_CREATE_TT"); JCoFunction func = repository.getFunction("ZPPR_MES_PRODORDCONF_CREATE_TT");
if (func == null) { if (func == null) {

@ -14,6 +14,7 @@ import com.op.sap.util.SAPConnUtils;
import com.op.system.api.domain.sap.*; import com.op.system.api.domain.sap.*;
import com.sap.conn.jco.*; import com.sap.conn.jco.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.validation.Validator; import javax.validation.Validator;
import java.rmi.ServerException; import java.rmi.ServerException;
@ -23,6 +24,7 @@ import java.util.List;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component
@Service @Service
public class SapRouterServiceImpl implements SapRouterService { public class SapRouterServiceImpl implements SapRouterService {
private static final String END_DATE="99991231"; private static final String END_DATE="99991231";
@ -41,6 +43,9 @@ public class SapRouterServiceImpl implements SapRouterService {
@Autowired @Autowired
protected Validator validator; protected Validator validator;
@Autowired
private JCoDestination dest;
@Override @Override
public R sapRouterSync(SapRouterQuery sapRouterQuery) { public R sapRouterSync(SapRouterQuery sapRouterQuery) {
int successNum = 0; int successNum = 0;
@ -50,7 +55,7 @@ public class SapRouterServiceImpl implements SapRouterService {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZPPR_MES_ROUTING"); JCoFunction func = repository.getFunction("ZPPR_MES_ROUTING");
if (func == null) { if (func == null) {

@ -14,6 +14,7 @@ import com.op.system.api.domain.sap.SapSupplier;
import com.op.system.api.domain.sap.SapWorkCenter; import com.op.system.api.domain.sap.SapWorkCenter;
import com.sap.conn.jco.*; import com.sap.conn.jco.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -22,19 +23,22 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component
@Service @Service
public class SapWorkCenterServiceImpl implements SapWorkCenterService { public class SapWorkCenterServiceImpl implements SapWorkCenterService {
private static final String END_DATE="99991231"; private static final String END_DATE="99991231";
@Autowired @Autowired
private SapBomMapper sapBomMapper; private SapBomMapper sapBomMapper;
@Autowired
private JCoDestination dest;
@Override @Override
public R sapWorkCenterSync(SapWorkCenterQuery sapWorkCenterQuery) { public R sapWorkCenterSync(SapWorkCenterQuery sapWorkCenterQuery) {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZMES_GET_ARBPL"); JCoFunction func = repository.getFunction("ZMES_GET_ARBPL");
if (func == null) { if (func == null) {
@ -111,7 +115,7 @@ public class SapWorkCenterServiceImpl implements SapWorkCenterService {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZMES_GET_LFB1"); JCoFunction func = repository.getFunction("ZMES_GET_LFB1");
if (func == null) { if (func == null) {
@ -188,7 +192,7 @@ public class SapWorkCenterServiceImpl implements SapWorkCenterService {
try { try {
// 获取调用 RFC 函数对象 // 获取调用 RFC 函数对象
//获取连接 //获取连接
JCoDestination dest = SAPConnUtils.connect(); // JCoDestination dest = SAPConnUtils.connect();
JCoRepository repository = dest.getRepository(); JCoRepository repository = dest.getRepository();
JCoFunction func = repository.getFunction("ZMES_GET_KNA1"); JCoFunction func = repository.getFunction("ZMES_GET_KNA1");
if (func == null) { if (func == null) {

@ -4,20 +4,29 @@ package com.op.sap.util;
import com.sap.conn.jco.JCoDestination; import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager; import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException; import com.sap.conn.jco.JCoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class SAPConnUtils { public class SAPConnUtils {
protected Logger logger = LoggerFactory.getLogger(getClass());
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
public static JCoDestination connect() {
@Bean
public JCoDestination connect() {
JCoDestination destination = null; JCoDestination destination = null;
try { try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
// System.out.println(destination.getAttributes()); if (destination!=null){
logger.info("连接SAP成功");
}
} catch (JCoException e) { } catch (JCoException e) {
// TODO: handle exception logger.info("连接SAP失败错误: " + e.toString());
throw new RuntimeException("连接SAP失败错误: " + e.toString()); throw new RuntimeException("连接SAP失败错误: " + e.toString());
// System.out.println("连接SAP失败错误: " + e.toString());
} }
return destination; return destination;
} }
} }

Loading…
Cancel
Save