最近项目需要和SAP对接,将项目中的数据写入到SAP系统中,和SAP沟通下决定使用SAP提供的RFC函数,具体调用方法如下:
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.hafx.codesure.tqs.sap.model.SapStock;
import com.hafx.codesure.tqs.util.PropertiesUtils;
import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
/**
* 与SAP RFC对接类
* @author lyh
* @version 2013-8-1
* @see SapJco
* @since
*/
public class SapJco
{
/**
* 日志
*/
private static Logger log = Logger.getLogger(SapJco.class);
/**
* SAP对接函数名
*/
private static String functionName = "zfm_crm_set_Quty";
/**
* 函数对应要写入数据的Table名
*/
private static String tableName = "LT_TABLE";
/**
* SAP连接
*/
private static JCO.Client mConnection = null;
/**
* SAP应答
*/
private static JCO.Repository mRepository;
/**
* Properties
*/
private static Properties p = new Properties();
static
{
p = new PropertiesUtils().getProperties("/config/TQS.properties");
//SAP配置从配置文件中获得
mConnection = JCO.createClient(
p.getProperty("jco.client.client"),// SAP client
p.getProperty("jco.client.user"), // userid
p.getProperty("jco.client.passwd"), // password
"en", // language (null for the default language)
p.getProperty("jco.client.ashost"), // application server host name
p.getProperty("jco.client.sysnr") // system number
);
}
/**
*
* Description: <br>
* Implement: <br>
*
* @param stocks 经销商库存集合
* @return X代表成功 其他代表失败
* @see
*/
public static String callRemote(List<SapStock> stocks)
{
String sapReturn = "";
try
{
/* //SAP配置从配置文件中获得
mConnection = JCO.createClient("121",// SAP client
"jsydj", // userid
"123456jsy", // password
"en", // language (null for the default language)
"192.168.2.22", // application server host name
"01"); // system number
*/
//连接
mConnection.connect();
JCO.Function function = SapJco.createFunction(functionName);
JCO.Table codes = null;
codes = function.getTableParameterList().getTable(tableName);
/* DIS_BP //经销商编码 10位固定
MATNR //物料编码 40位
ZYEAR //年度 4位
ZMONTH //月度 2位
CYCLE //周期 01 02 03 04 一号累加 最后多余的时间放到04周
QUANTITY_QUAN //库存数量 瓶
DATUM //当前日期 yyyyMMdd
UZEIT //当前时间 hhMMss
DIS_BP_DESC //经销商名称
MATNR_DESC //物流名称
*/
for(SapStock stock: stocks)
{
codes.appendRow();
codes.setValue(stock.getDisbp(), "DIS_BP");
codes.setValue(stock.getMatnr(), "MATNR");
codes.setValue(stock.getZyear(), "ZYEAR");
codes.setValue(stock.getZmonth(), "ZMONTH");
codes.setValue(stock.getCycle(), "CYCLE");
codes.setValue(stock.getQuantityquan(), "QUANTITY_QUAN");
codes.setValue(stock.getDatum(), "DATUM");
codes.setValue(stock.getUzeit(), "UZEIT");
codes.setValue(stock.getDisbpdesc(), "DIS_BP_DESC");
codes.setValue(stock.getMatnrdesc(), "MATNR_DESC");
//codes.
}
//执行
mConnection.execute(function);
//获得sap返回值
sapReturn = function.getExportParameterList().getString("EV_FLAG");
log.info("SAP 经销商库存返回值:" + sapReturn);
return sapReturn;
}
catch (Exception ex)
{
log.error("SAP RFC 经销商库存对接出现异常!异常信息:" + ex.getMessage());
}
finally
{
mConnection.disconnect();
}
return sapReturn;
}
/**
*
* Description: SAP 函数<br>
* Implement: <br>
*
* @param name
* @return
* @throws Exception
* @see
*/
private static JCO.Function createFunction(String name)
throws Exception
{
mRepository = new JCO.Repository("ARAsoft", mConnection);
try
{
IFunctionTemplate ft = mRepository.getFunctionTemplate(name.toUpperCase());
if (ft == null)
{
return null;
}
return ft.getFunction();
}
catch (Exception ex)
{
throw new Exception("Problem retrieving JCO.Function object.");
}
}
}
public class PropertiesUtils
{
/**
* 日志
*/
private static final Logger logger = Logger.getLogger(PropertiesUtils.class);
/**
*
* Description: 读入properties文件
*
* @param pPath
* @return Properties
* @throws IOException
* @see
*/
public Properties getProperties(String pPath)
{
Properties p = null;
try
{
InputStream is = getClass().getResourceAsStream(pPath);
if (is == null)
{
logger.error("Path==" + pPath + "== properties file stream is null");
}
p = new Properties();
p.load(is);
}
catch (Exception e)
{
logger.error(e.getMessage());
}
return p;
}
}
需要将sapjco.jar添加到项目中。
同时需要将librfc32.dll和sapjcorfc.dll放到javahome\jre\bin\下,否则会出现如下异常:[no sapjcorfc in java.library.path]. java.library.path