2024년 12월 2일 월요일

[CAMEL]_Apache CAMEL과 SAP RFC 연동

 [ 출처 : https://www.cdata.com/ ]

 

Integrate with SAP Data using Apache Camel2 

Create a simple Java app that uses Apache Camel routing and the CData JDBC Driver to copy SAP data to a JSON file on disk.

Apache Camel is an open source integration framework that allows you to integrate various systems consuming or producing data. When paired with the CData JDBC Driver for SAP ERP, you can write Java apps that use Camel routes that integrate with live SAP data. This article walks through creating an app in NetBeans that connects, queries, and routes SAP data to a JSON file.

With built-in optimized data processing, the CData JDBC Driver offers unmatched performance for interacting with live SAP data. When you issue complex SQL queries to SAP, the driver pushes supported SQL operations, like filters and aggregations, directly to SAP and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations). Its built-in dynamic metadata querying allows you to work with and analyze SAP data using native data types.
 

About SAP Data Integration

CData provides the easiest way to access and integrate live data from SAP. Customers use CData connectivity to:

    Access every edition of SAP, including SAP R/3, SAP NetWeaver, SAP ERP / ECC 6.0, and SAP S/4 HANA on premises data that is exposed by the RFC.
    Perform actions like sending IDoc or IDoc XML files to the server and creating schemas for functions or queries through SQL stored procedures.
    Connect optimally depending on where a customer's SAP instance is hosted.
        Customers using SAP S/4HANA cloud public edition will use SAP NetWeaver Gateway connectivity
        Customers using SAP S/4HANA private edition will use either SAP ERP or SAP NetWeaver Gateway connectivity.

While most users leverage our tools to replicate SAP data to databases or data warehouses, many also integrate live SAP data with analytics tools such as Tableau, Power BI, and Excel.

Getting Started

Creating A New Maven/Java Project

Follow the steps below to create a new Java project and add the appropriate dependencies:

    Open NetBeans and create a new project.
    Select Maven from the categories list and Java Application from the projects list, then click Next.
    Name the project (and adjust any other properties) and click Finish.
    In the source package, create a new Java class (we used App.java for this article) and add the main method to the class.

Adding Project Dependencies

With the project created, we can start adding the dependencies needed to work with live SAP data from our App. If you have not already done so, install Maven in your environment, as it is required to add the JAR file for the CData JDBC Driver to your project.


Installing the CData JDBC Driver for SAP ERP with Maven
1. Download the CData JDBC Driver for SAP ERP installer, unzip the package, and run the JAR file to install the driver.
2. Use Maven to install the JDBC Driver as a connector.

    mvn install:install-file
    -Dfile="C:\Program Files\CData[product_name] 2019\lib\cdata.jdbc.saperp.jar"
    -DgroupId="org.cdata.connectors"
    -DartifactId="cdata-saperp-connector"
    -Dversion="19"
    -Dpackaging=jar

Once the JDBC Driver is installed, we can add dependencies to our project. To add a dependency, you can either edit the pom.xml file or right-click the dependencies folder and click Add Dependency. The properties for each dependency follow, but you can search through the available libraries by typing the name of the dependency in the Query box in the Add Dependency wizard.




























Accessing SAP Data in Java Apps with Camel

After adding the required dependencies, we can use the Java DSL (Domain Specific Language) to create routes with access to live SAP data. Code snippets follow. Download the sample project (zip file) to follow along (make note of the TODO comments).

Start by importing the necessary classes into our main class.

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.SimpleRegistry;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.log4j.BasicConfigurator;

Then in the main method, we configure logging, create a new BasicDataSource and add it to the registry, create a new CamelContext, and finally add a route to the context. In this sample, we route SAP data to a JSON file.


Configure Logging

BasicConfigurator.configure();

Create a BasicDataSource

Create a BasicDataSource and set the driver class name (cdata.jdbc.salesforce.SalesforceDriver) and URL (using the required connection properties).

The driver supports connecting to an SAP system using the SAP Java Connector (SAP JCo). Install the files (sapjco3.jar and sapjco3.dll) to the appropriate directory for the hosting application or platform. See the "Getting Started" chapter in the help documentation for information on using the SAP JCo files.

In addition, you can connect to an SAP system using Web services (SOAP). To use Web services, you must enable SOAP access to your SAP system and set the Client, RFCUrl, User, and Password properties, under the Authentication section.

For more information, see this guide on obtaining the connection properties needed to connect to any SAP system.

BasicDataSource basic = new BasicDataSource();
basic.setDriverClassName("cdata.jdbc.saperp.SAPERPDriver");
basic.setUrl("jdbc:saperp:Host=sap.mydomain.com;User=EXT90033;Password=xxx;Client=800;System Number=09;ConnectionType=Classic;Location=C:/mysapschemafolder;");>

The CData JDBC Driver includes a built-in connection string designer to help you configure the connection URL.


Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the SAP JDBC Driver. Either double-click the JAR file or execute the jar file from the command line.

java -jar cdata.jdbc.saperp.jar



Fill in the connection properties and copy the connection string to the clipboard.

















Add the BasicDataSource to the Registry and Create a CamelContext

SimpleRegistry reg = new SimpleRegistry();
reg.bind("myDataSource", basic);
 
CamelContext context = new DefaultCamelContext(reg);


Add Routing to the CamelContext

The routing below uses a timer component to run one time and passes a SQL query to the JDBC Driver. The results are marshaled as JSON (and formatted for pretty print) and passed to a file component to write to disk as a JSON file.

context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("timer://foo?repeatCount=1")
.setBody(constant("SELECT * FROM Account LIMIT 10"))
.to("jdbc:myDataSource")
.marshal().json(true)
.to("file:C:\\Users\\USER\\Documents?fileName=account.json");
}
});

Managing the CamelContext Lifecycle

With the route defined, start the CamelContext to begin the lifecycle. In this example, we wait 10 seconds and then shut down the context.

context.start();
Thread.sleep(10000);
context.stop();

 

Free Trial, Sample Project & Technical Support

Now, you have a working Java application that uses Camel to route data from SAP to a JSON file. Download a free, 30-day trial of the CData JDBC Driver for SAP ERP and the sample project (make note of the TODO comments) and start working with your live SAP data in Apache Camel. Reach out to our Support Team if you have any questions.

 

 

 

 

 [ 출처 : https://camel.apache.org/ ]

SAP NetWeaver

Since Camel 2.12

Only producer is supported

The SAP Netweaver integrates with the SAP NetWeaver Gateway using HTTP transports.

This camel component supports only producer endpoints.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-sap-netweaver</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

The URI scheme for a sap netweaver gateway component is as follows

sap-netweaver:https://host:8080/path?username=foo&password=secret

Prerequisites

You would need to have an account on the SAP NetWeaver system to be able to leverage this component. SAP provides a demo setup where you can request an account.

This component uses the basic authentication scheme for logging into SAP NetWeaver.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The SAP NetWeaver component supports 2 options, which are listed below.

Name Description Default Type

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The SAP NetWeaver endpoint is configured using URI syntax:

sap-netweaver:url

With the following path and query parameters:

Path Parameters (1 parameters)

Name Description Default Type

url (producer)

Required Url to the SAP net-weaver gateway server.


String

Query Parameters (6 parameters)

Name Description Default Type

flatternMap (producer)

If the JSON Map contains only a single entry, then flattern by storing that single entry value as the message body.

true

boolean

json (producer)

Whether to return data in JSON format. If this option is false, then XML is returned in Atom format.

true

boolean

jsonAsMap (producer)

To transform the JSON from a String to a Map in the message body.

true

boolean

password (producer)

Required Password for account.


String

username (producer)

Required Username for account.


String

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

Message Headers

The SAP NetWeaver component supports 3 message header(s), which is/are listed below:

Name Description Default Type

CamelNetWeaverCommand (producer)

Constant: COMMAND

Required The command to execute in http://msdn.microsoft.com/en-us/library/cc956153.aspxMS ADO.Net Data Service format.


String

CamelHttpPath (producer)

Constant: HTTP_PATH

The http path.


String

Accept (producer)

Constant: ACCEPT

The media type.


String

Examples

This example is using the flight demo example from SAP, which is available online over the internet here.

In the route below we request the SAP NetWeaver demo server using the following url

https://sapes4.sapdevcenter.com/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT

And we want to execute the following command

FlightCollection(carrid='AA',connid='0017',fldate=datetime'2016-04-20T00%3A00%3A00')

To get flight details for the given flight. The command syntax is in MS ADO.Net Data Service format.

We have the following Camel route

from("direct:start")
    .setHeader(NetWeaverConstants.COMMAND, constant(command))
    .toF("sap-netweaver:%s?username=%s&password=%s", url, username, password)
    .to("log:response")
    .to("velocity:flight-info.vm")

Where url, username, password and command are defined as:

    private String username = "P1909969254";
    private String password = "TODO";
    private String url = "https://sapes4.sapdevcenter.com/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT";
    private String command = "FlightCollection(carrid='AA',connid='0017',fldate=datetime'2016-04-20T00%3A00%3A00')";

The password is invalid. You would need to create an account at SAP first to run the demo.

The velocity template is used for formatting the response to a basic HTML page

<html>
  <body>
  Flight information:

  <p/>
  <br/>Airline ID: $body["AirLineID"]
  <br/>Aircraft Type: $body["AirCraftType"]
  <br/>Departure city: $body["FlightDetails"]["DepartureCity"]
  <br/>Departure airport: $body["FlightDetails"]["DepartureAirPort"]
  <br/>Destination city: $body["FlightDetails"]["DestinationCity"]
  <br/>Destination airport: $body["FlightDetails"]["DestinationAirPort"]

  </body>
</html>

When running the application, you get sample output:

Flight information:
Airline ID: AA
Aircraft Type: 747-400
Departure city: new york
Departure airport: JFK
Destination city: SAN FRANCISCO
Destination airport: SFO

Spring Boot Auto-Configuration

When using sap-netweaver with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-sap-netweaver-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

The component supports 3 options, which are listed below.

Name Description Default Type

camel.component.sap-netweaver.autowired-enabled

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

Boolean

camel.component.sap-netweaver.enabled

Whether to enable auto configuration of the sap-netweaver component. This is enabled by default.


Boolean

camel.component.sap-netweaver.lazy-start-producer

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

Boolean

 

댓글 없음:

댓글 쓰기