java调用rfc

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;


@Component
@Slf4j
public class CrmGetConnection {
    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
    @Autowired
    private CrmParameters crmParameters;

    /**
     * 初始化SAP连接
     */
    @PostConstruct
    public void initProperties() {
        Properties connectProperties = new Properties();
        // SAP服务器
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, crmParameters.getJco_ashost());
        connectProperties.setProperty(DestinationDataProvider.JCO_MSSERV, crmParameters.getJco_msserv());//
        // SAP系统编号
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  crmParameters.getJco_sysnr());
        // SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, crmParameters.getJco_client());
        // SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   crmParameters.getJco_user());
        // SAP密码
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, crmParameters.getJco_passwd());
        // SAP登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   crmParameters.getJco_lang());
        // 最大连接数
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, crmParameters.getJco_pool_capacity());
        // 最大连接线程
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, crmParameters.getJco_peak_limit());
        //connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER ,"");

        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);

    }

    /**
     * 创建SAP接口属性文件。
     * @param name  ABAP管道名称
     * @param suffix    属性文件后缀
     * @param properties    属性文件内容
     */
    private static void createDataFile(String name, String suffix, Properties properties){
        File cfg = new File(name+"."+suffix);
        if(cfg.exists()){
            cfg.deleteOnExit();
        }
        try{
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "for tests only !");
            fos.close();
        }catch (Exception e){
            System.out.println("Create Data file fault, error msg: " + e.toString());
            throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
        }
    }

    /**
     * 获取SAP连接
     * @return  SAP连接对象
     */
    public JCoDestination connect(){
        JCoDestination destination = null;
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        } catch (JCoException e) {
        }
        return destination;
    }

}

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class CrmParameters {
    // sap服务器
    @Value("${crm.jco_ashost}")
    private String jco_ashost;
    // sap系统编号
    @Value("${crm.jco_sysnr}")
    private String jco_sysnr;
    // sap集团
    @Value("${crm.jco_client}")
    private String jco_client;
    // sap用户名
    @Value("${crm.jco_user}")
    private String jco_user;
    // sap密码
    @Value("${crm.jco_passwd}")
    private String jco_passwd;
    // sap登录语言
    @Value("${crm.jco_lang}")
    private String jco_lang;
    // 最大连接数
    @Value("${crm.jco_pool_capacity}")
    private String jco_pool_capacity;
    // 最大连接线程
    @Value("${crm.jco_peak_limit}")
    private String jco_peak_limit;
    //
    @Value("${crm.flag_f}")
    private String flag_f;
    @Value("${crm.jco_msserv}")
    private String jco_msserv;
}

#crm10.0.64.138 crmprd.sanygroup.com
crm.jco_ashost=172.16.9.172
crm.jco_msserv=8443
#00
crm.jco_sysnr=01
crm.jco_client=800
crm.jco_user=CC_RFC
crm.jco_passwd=63gg5bG#
crm.jco_lang=ZH
crm.jco_pool_capacity=3
crm.jco_peak_limit=10
crm.flag_f=海外

具体引用

public String bindDeviceToUser(String deviceNum[], Integer userId,String language,String bindingTime) {
        DeviceInfosDao mapper = sqlSessionTemplate.getMapper(DeviceInfosDao.class);
        JCoFunction function = null;
        JCoStructure jCoStructure=null;
        JCoParameterList jCoParameterList=null;
        String balance="";
        //String balanceType="15SY0136F0528";
        String EV_MTYPE=null;
        String EQU_NAME=null;
        String EQU_NO=null;
        String EQU_TYPE=null;
        String prodGroupDesc=null;
        String regionDesc=null;
        String agentDesc=null;
        String countryDesc=null;
        String crm_language=language;
        if("zh_CN".equals(language)){
            crm_language="ZH";
        }else if("en_US".equals(language)){
            crm_language="EN";
        }
        //连接sap
        JCoDestination destination = crmGetConnection.connect();
        if(null!=deviceNum){
            for(int i=0;i<deviceNum.length;i++){
                if(mapper.checkDeviceInfo(deviceNum[i],userId)>0)
                    //throw new ExceptionUtil(ResultEnum.CHECK_DEVICE);
                    return "C";
                try {
                    //
                    function = destination.getRepository().getFunction("ZFM_R_CC_GET_EQUIPMENT_INFO");
                    //入参 字符串
                    jCoParameterList=function.getImportParameterList();
                    jCoParameterList.setValue("IV_EQU_NO", deviceNum[i]);
                    //传入对象
                    JCoStructure structure = jCoParameterList.getStructure("IS_BASE");
                    structure.setValue("USER",crmParameters.getJco_user());
                    structure.setValue("LANGU",crm_language);
                    structure.setValue("FLAG_F",crmParameters.getFlag_f());
                    jCoParameterList.setValue("IS_BASE", structure);
                    //jCoParameterList.getField();
                    // 开启一个事务
                    //JCoContext.begin(destination);
                    function.execute(destination);

                    //返回
                    EV_MTYPE=function.getExportParameterList().getString("EV_MTYPE");
                    String EV_MSG=function.getExportParameterList().getString("EV_MSG");
                    jCoStructure= function.getExportParameterList().getStructure("ES_EQU_INFO");
                    EQU_NO= (String) jCoStructure.getValue("EQU_NO");
                    EQU_NAME= (String) jCoStructure.getValue("EQU_NAME");
                    EQU_TYPE= (String) jCoStructure.getValue("EQU_TYPE");
                    prodGroupDesc= (String) jCoStructure.getValue("PROD_GROUP_DESC");
                    regionDesc= (String) jCoStructure.getValue("REGION_DESC");
                    countryDesc= (String) jCoStructure.getValue("COUNTRY_DESC");
                    agentDesc= (String) jCoStructure.getValue("AGENT_DESC");
                    //System.out.println("返回--->"+EV_MTYPE+"--->"+EV_MSG+"--->"+EQU_NO+"--->"+EQU_NAME+"--->"+EQU_TYPE);
                    //关闭事务
                    //JCoContext.end(destination);
                    log.info("绑定设备:crm返回消息="+EV_MSG);
                } catch (JCoException e) {
                    e.printStackTrace();
                }
                if("S".equals(EV_MTYPE)){
                    DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
                    deviceInfoEntity.setDeviceName(EQU_NAME);
                    deviceInfoEntity.setDeviceNum(EQU_NO);
                    deviceInfoEntity.setDeviceType(EQU_TYPE);
                    deviceInfoEntity.setProdGroupDesc(prodGroupDesc);
                    deviceInfoEntity.setRegionDesc(regionDesc);
                    deviceInfoEntity.setCountryDesc(countryDesc);
                    deviceInfoEntity.setAgentDesc(agentDesc);

                    if(mapper.checkDuplicateDataOfDevice(deviceNum[i])==0) {
                        mapper.addDeviceInfo(deviceInfoEntity);
                    }
                    mapper.bindDeviceToUser(deviceNum[i],userId,bindingTime);
                /*if(true){
                    throw new ExceptionUtil(ResultEnum.CHECK_DEVICE);
                }*/
                }else if("E".equals(EV_MTYPE)){
                    return "E";
                }else if(null==EV_MTYPE){
                    return "sapErr";
                }
            }
            return "S";
        }
        return "设备号不能为空!";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值