zhangherong
2025-06-16 c10a845e0cf71bc1861feba99052d7822dffbd7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.jeecg.config.sap;
 
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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 SapRfcConnectionManager {
 
    @Value("${sap.rfc.destination}")
    private String destinationName;
 
    @Value("${sap.rfc.ashost}")
    private String ashost;
 
    @Value("${sap.rfc.sysnr}")
    private String sysnr;
 
    @Value("${sap.rfc.client}")
    private String client;
 
    @Value("${sap.rfc.user}")
    private String user;
 
    @Value("${sap.rfc.passwd}")
    private String passwd;
 
    @Value("${sap.rfc.lang}")
    private String lang;
 
    private String poolSize = "5";
 
    private String idleTimeout = "10000";
 
    private String peekLimit = "10";
 
    private JCoDestination destination;
 
    /**
     *
     * @throws JCoException
     */
    @PostConstruct
    public void init() throws Exception {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, ashost);
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, sysnr);
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, client);
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, user);
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, passwd);
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, lang);
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, peekLimit);
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, poolSize);
        connectProperties.setProperty(DestinationDataProvider.JCO_EXPIRATION_TIME, idleTimeout);
 
        // 创建动态目的地(避免依赖 SM59 配置)
        CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
        provider.addDestination(destinationName, connectProperties);
 
        // 设置全局目的地提供者
//        Environment.registerDestinationDataProvider(provider);
 
        log.info("properties = " + connectProperties);
 
        //生成配置文件,JCoDestinationManager.getDestination()调用时会需要该连接配置文件,后缀名需要为jcoDestination
        FileOutputStream fos = null;
        String suffix = "jcoDestination";
        File cfg = new File(destinationName + "." + suffix);
        if (!cfg.exists()) {
            try {
                fos = new FileOutputStream(cfg, false);
                connectProperties.store(fos, "for tests only !");
                fos.close();
            } catch (Exception var9) {
                throw new Exception("Unable to create the destination file " + cfg.getName(), var9);
            } finally {
                if (null != fos) {
                    fos.close();
                }
            }
        }
 
        // 获取目的地
        this.destination = JCoDestinationManager.getDestination(destinationName);
    }
 
    /**
     *
     * @return
     */
    public JCoDestination getDestination() {
        return destination;
    }
 
    /**
     *
     */
    public void destroy() {
        if (destination != null) {
//            destination;
        }
    }
}