可以在文件夹中创建我的 JcoDestination 文件吗?(JCO3 Java – SAP RFC 连接)

huangapple go评论68阅读模式
英文:

Can I Create My JcoDestination File In Folder ? (JCO3 Java - SAP RFC Connection)

问题

当我调用getDestination("connect")时,我可以连接并运行我的函数而没有问题。

但是我将有多个配置文件,因为我将连接到不同的SAP服务器。

我想将所有这些文件收集到一个文件夹中,但当我写getDestination("src/JcoDestinations/connect")时,它不读取我放在文件夹中的配置文件,也不连接。

如何将所有连接的jcoDestination配置文件收集在一起并运行?

我得到的错误信息是:

(106) JCO_ERROR_RESOURCE: 目标 'connect' 不存在。–

我尝试过:

File file = new File("src/JcoDestinations/connect");
JCoDestination destination = JCoDestinationManager.getDestination(file.toString());

和:

File file = new File("C:\\connect");
JCoDestination destination = JCoDestinationManager.getDestination(file.toString());

但这些对我不起作用。

英文:

I want to access an RFC function on sap from my java program.

When I call getDestination("connect") I can connect and run my function without problems.

But I will have more than one config file because I will be connecting to different SAP servers.

I want to collect all of these files in one folder, but when I write getDestination("src/JcoDestinations/connect") it does not read my config file that I put in the folder and does not connect.

How do I get all my connect jcoDestination config files together and run?

The error I get is this :
>(106) JCO_ERROR_RESOURCE: Destination 'connect' does not exist. –

I tried:

File file = new File("src/JcoDestinations/connect");
JCoDestination destination = JCoDestinationManager.getDestination(file.toString());

and:

File file = new File("C:\\connect");
JCoDestination destination = JCoDestinationManager.getDestination(file.toString());

these did not work for me.

答案1

得分: 2

你可以定义一个Custom DestinationDataProvider来保存多个连接配置文件。在这里,我们不使用属性文件,但我认为这个示例应该可以工作。

这里是一个DataProvider,用于保存不同的属性文件,存储在一个HashMap中。方法JCoDestinationManager.getDestination调用DataProvider内部的方法getDestinationProperties,并期望得到一个Properties对象作为返回值。方法addConnectionProperties用于向HashMap中添加新的Properties对象。

你还可以实现一个方法来读取一个大的属性文件,但我有点懒 可以在文件夹中创建我的 JcoDestination 文件吗?(JCO3 Java – SAP RFC 连接)

下面是一个示例:

public class SapSystemDestinationDataProvider implements DestinationDataProvider {

    private DestinationDataEventListener el;
    private final HashMap<String, Properties> connectionProperties = new HashMap<>();

    @Override
    public Properties getDestinationProperties(String destinationName) {
        if (connectionProperties.size() > 0) {
            Properties con = connectionProperties.get(destinationName.toLowerCase().trim());
            if (con != null) {
                return con;
            }
        }
        return null;
    }

    @Override
    public boolean supportsEvents() {
        return true;
    }

    @Override
    public void setDestinationDataEventListener(DestinationDataEventListener dl) {
        this.el = dl;
    }

    public void addConnectionProperties(String destName, Properties properties) {
        connectionProperties.put(destName.toLowerCase().trim(), properties);
    }
}

然后在你的主程序中注册这个提供者:

SapSystemDestinationDataProvider sapSystemProvider = new SapSystemDestinationDataProvider();
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(sapSystemProvider);

现在,从磁盘上读取所有的属性文件并将它们放入Custom DestinationDataProvider中:

try (InputStream input = new FileInputStream("path/to/connection_1.properties")) {
    Properties prop = new Properties();
    prop.load(input);
    try (InputStream input2 = new FileInputStream("path/to/connection2.properties")) {
        Properties prop2 = new Properties();
        prop2.load(input2);

        sapSystemProvider.addConnectionProperties("CONNECTION_1", prop);
        sapSystemProvider.addConnectionProperties("CONNECTION_2", prop2);
    }
}

现在你有两个连接。你可以通过JCoDestinationManager.getDestination来访问这些连接:

JCoDestination dest1 = JCoDestinationManager.getDestination("CONNECTION_1");
JCoDestination dest2 = JCoDestinationManager.getDestination("CONNECTION_2");

我从未尝试过这个,因为我使用外部数据库来构建Properties对象,但我认为它应该可以工作。让我知道它是如何工作的 可以在文件夹中创建我的 JcoDestination 文件吗?(JCO3 Java – SAP RFC 连接)

英文:

you can define a Custom DestinationDataProvider to hold more than one Connection Profile. We dont use Property - Files here ,but I think this example should work.

Here a DataProvider to hold different Property Files, stored in a HashMap. The method JCoDestinationManager.getDestination calls the method getDestinationProperties inside the DataProvider and expects to get a Properties Object back. The method addConnectionProperties adds new Properties Objects to the HashMap.

Here you can also implement a method to read one big Property file.
But I'm too lazy for that 可以在文件夹中创建我的 JcoDestination 文件吗?(JCO3 Java – SAP RFC 连接)

Here an example

public class SapSystemDestinationDataProvider implements DestinationDataProvider {

private DestinationDataEventListener el;
private final HashMap&lt;String, Properties&gt; connectionProperties = new HashMap&lt;&gt;();


   @Override
   public Properties getDestinationProperties(String destinationName) {
      if (connectionProperties.size() &gt; 0) {
         Properties con = connectionProperties.get(destinationName.toLowerCase().trim());
         if (con != null) {
            return con;
          }
      }
      return null;
   }


   @Override
   public boolean supportsEvents() {
       return true;
   }

    @Override
    public void setDestinationDataEventListener(DestinationDataEventListener dl) {
        this.el = dl;
    }

    public void addConnectionProperties(String destName, Properties properties) {
        connectionProperties.put(destName.toLowerCase().trim(), properties);
    }
}

Then register the provider in your main programm

SapSystemDestinationDataProvider sapSystemProvider = new SapSystemDestinationDataProvider();
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(sapSystemProvider );

Now read all your property files from disk and place the files in the Custom DestinationDataProvider.

InputStream input = new FileInputStream(&quot;path/to/connection_1.properties&quot;)) {
Properties prop = new Properties();
prop.load(input);
InputStream input2 = new FileInputStream(&quot;path/to/connection2.properties&quot;)) {
Properties prop2 = new Properties();
prop2.load(input2);

sapSystemProvider.addConnectionProperties(&quot;CONNECTION_1&quot;,prop);
sapSystemProvider.addConnectionProperties(&quot;CONNECTION_2&quot;, prop2);

Now you have 2 connections. You can access the connections via JCoDestinationManager.getDestination.

JCoDestination dest1 = JCoDestinationManager.getDestination(&quot;CONNECTION_1&quot;);
JCoDestination dest2 = JCoDestinationManager.getDestination(&quot;CONNECTION_2&quot;);

I never tried it, cause i use an external database to build up the Propertie Objects, but i think it should work. Let me know, how it works 可以在文件夹中创建我的 JcoDestination 文件吗?(JCO3 Java – SAP RFC 连接)

huangapple
  • 本文由 发表于 2023年2月13日 22:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定