Thursday, August 21, 2008

Adapters (Flat File adapter app)

Reading and Writing Files Application (Adapters) How to

Websphere Integration Developer v612

Contact us...

Contents

Completed Adapter application (Reading and Writing Files)

How to build the Adapter application (Reading and Writing Files)

Completed Adapter application (Reading and Writing Files)

Related links
Source IBM Websphere Integration Developer 6.1.2 sample
Introduction

This tutorial will demonstrate how an application can read from a file on the file system and write to a file on the file system using the IBM® WebSphere® Adapter for Flat Files. You will also learn how to create an application with WebSphere Integration Developer that processes the incoming data before writing the processed data to the file on the file system. This tutorial is for developers who work with adapters generally and the Flat Files adapter specifically.

After completing this tutorial, you will understand how to use WebSphere Integration Developer with the WebSphere Adapter for Flat Files to develop a service that reads data from a file on the file system, processes the data and writes the processed data to a file on the file system. Specifically, you will learn how to perform the following tasks:

  • Develop an inbound service using the adapter pattern wizard with the Flat Files adapter to read data from a file on the file system
  • Develop an outbound service with the same wizard and adapter to write data to a file on the file system
  • Implement the business logic to process the data read from a file on the file system
  • Test your application.

The article An introduction to the WebSphere Adapter for Flat Files formed the basis of this tutorial.

Overview When working with this tutorial, you should know several concepts related to flat files, adapters, processing data to and from an application created in WebSphere® Integration Developer with an adapter and have a high-level understanding of the application you will create.

The following concepts are discussed in this section:

What is a Flat file?

The term flat file refers to any file stored on a local file system, as opposed to a more complex set of files, such as those in a structured database. The data records in a flat file are typically stored sequentially and without any metadata, such as the indices, keys, and relationships that you would find in database storage. You can use the adapter for flat files to read and write any type of file. However, you typically use the adapter for flat files to read and write text files that store records in a flat file format such as XML or Comma-Separated Values (CSV) records. The following example shows records in each format.

XML format

<?xml version="1.0" encoding="UTF-8"?> <customer> 	<title>Mr</title> 	<name>Smith</name> 	<city>Ottawa</city> 	<state>ON</state> </customer> <customer> 	<title>Mrs</title> 	<name>Jones</name> 	<city>Winnipeg</city> 	<state>MB</state> </customer>
CSV format
Mr,Smith,Ottawa,ON
Mrs,Jones,Winnipeg,MB
What is the WebSphere Adapter for Flat Files?

Suppose you have an external system that outputs sets of files to a directory. An example might be an order processing system that produces text files containing order information to be processed in a batch mode during off-peak hours. You would use the WebSphere Adapter for Flat Files along with WebSphere Integration Developer to help you process these files. Another information system might use sets of text files in a directory as input to programs. You would use the WebSphere Adapter for Flat Files along with WebSphere Integration Developer to help you create and manage these files.

Any WebSphere application can use the adapter for flat files to create and manage files or to monitor a directory and read files from this directory. You have two options when creating a service associated with adapter for flat files, both of which are documented in the information center of the product:

  • Adapter pattern wizard: With this wizard, you create a service by specifying the directories that the WebSphere Adapter for Flat Files reads from and writes to and formats of the data in those directories. Then the adapter pattern wizard generates the service. For many users, this quick and easy way of creating a service is sufficient. We will use this approach in this tutorial.
  • External service wizard: With this wizard, you create a service by a longer, more comprehensive process. Some users may find this more detailed approach more suitable. You begin by specifying the type of adapter you will require and then specify the directories that the WebSphere Adapter for Flat Files reads from and writes to, and the format of the data in those directories. Then you continue using further pages with fields in an advanced section to add more control over the generated service.

What are inbound and outbound processing?

Inbound processing is the mode of operation in which the WebSphere Adapter for Flat Files monitors the file system, reads new files, and sends the data to an operation in a WebSphere application.

Outbound processing is the mode of operation in which the WebSphere Adapter for Flat Files receives requests (from a component of a WebSphere application) to perform an operation on an enterprise information system and, when applicable, returns the results to the caller. Example operations include creating a file, writing to a file, or checking if a specific file exists.

Inbound processing

During inbound processing, the adapter for flat files listens for events that are produced by an event directory. An event is a record of what changes have occurred in the event directory. The directory that the adapter for flat files monitors for new files is called the event directory.

The following diagram shows the four steps that occur during inbound processing:

  1. An external system outputs its files to the event directory.
  2. The adapter for flat files polls for files from the event directory and converts the data from those files into events. The adapter can be configured to poll for only certain types of files, or for files created during a certain time period.
  3. Events are temporarily placed in an event store. This is either a database or an in-memory representation of the event table. If you use a database, events will not be lost before your application can process them if the server goes down after an event is created, for example. This is referred to as assured delivery. Using in-memory tables, the event processing will be faster but you lose the event recovery capability.
  4. The adapter for flat files retrieves the events from the event store and passes each event in the form of a business object through an exported inbound interface, which the adapter pattern wizard or the external service wizard created.

This method of retrieving data, that is, passing data in the form of a business object, is called non-pass-through or Data Transformation Framework (DTF), and it operates on structured data.

If the application does not know the format of the data files, you can configure the adapter for flat files to run in pass-through mode, operating on unstructured data. In that case, it would not transform an event into a business object.

File splitting

You use file splitting when the files you want to retrieve are large or they each contain more than one record. You can split files into smaller chunks based on a delimiter or on a fixed-size value, which allows parts of the file to be processed in parallel. Each chunk is considered a separate event and is individually sent to an operation in a WebSphere application through the exported inbound interface.

Record delimiter

Typically, input is stored as a single record per file. However, when the input file contains more than one record, you often separate the records in the file with a delimiter, which can be any text string and is usually a combination of characters followed by rn (platform dependent new line character). The adapter can both read and write files that contain a delimiter.

Using our XML format and CVS format examples discussed earlier, we have the string #### followed by a new line as the delimiter in the XML file shown in the following example. We have a new line as the delimiter in the CSV file.

XML format with #### as a delimiter

<?xml version="1.0" encoding="UTF-8"?> <customer> 	<title>Mr</title> 	<name>Smith</name> 	<city>Ottawa</city> 	<state>ON</state> </customer> #### <customer> 	<title>Mrs</title> 	<name>Jones</name> 	<city>Winnipeg</city> 	<state>MB</state> </customer> ####

CVS format with a new line as a delimiter
Mr,Smith,Ottawa,ON Mrs,Jones,Winnipeg,MB
Split by size

The split-by-size feature is similar to splitting by delimiter because you use it to divide a file into smaller chunks and transfer them to the operation in the WebSphere application one by one. However, use this feature with unstructured data in a pass-though scenario because the unstructured data is not going to be put into business objects. You specify the split criterion in the adapter as a number of bytes. The adapter reads the file as chunks (events) of that byte size. Each chunk will be the size defined by split criteria, except the last one, which might be smaller.

Custom data binding

Data in flat files can come in many different formats, such as the previously discussed XML and CSV formats. Others formats include name and value pairs, tab-separated, and fixed-width formats. A data binding maps from the format in the data files to the attributes of a business object.

Using the adapter for flat files default data binding, you can convert files to and from XML format. In other formats, you have to use custom data bindings that define the mapping between the file format and a business object. To implement a custom binding, create a Java™ class that converts the data for the specific format and enter this class in the adapter pattern wizard or external service wizard when you configure the adapter.

For more information about creating a custom data binding, refer to the WebSphere Adapters documentation.

Outbound processing

To write or modify files, the application uses the operations defined in the outbound interface, which you create using the adapter pattern wizard or the external service wizard. You can create both unstructured data (in pass-through mode) and structured data (in non-pass-through mode) using the different operation types:

  • Create – stores data to a new file or creates an empty file
  • Append – appends data to an existing file
  • Overwrite – overwrites an existing file with a new data
  • Delete – deletes an existing file
  • Exists – checks if a specific file exists
  • List – lists files in a directory
  • Retrieve – reads content from a file (binary only)

Only the create and append operations are used with the adapter pattern wizard.

Overview of the application in this tutorial

Imagine that you want to create an application that monitors a certain directory in a file system so that you can collect customer record information. When a file is created, the adapter calls the export with the data that is stored in a business object. The export is wired to a business process component that separates the data from the file into individual records that are each placed into their own file in an output directory.

This scenario is shown in the following diagram:

When files that contain customer records are in the inbound events directory, the resource adapter retrieves them, creates a customer business object, and calls an export in the module, which initiates a business process. The process generates a new file name based on the number of existing files, and specifies the output directory. Then, the business process writes the customer business object data to a separate XML file in the output directory of the file system.

Reading and Writing Files Application

Business Integration View

The business integration view shows all the resources used by the application (i.e data types, interfaces, etc..).

Assembly Diagram

The Assembly Diagram shows all the components used by the application (i.e export component, interface mapping component, and import components).

How to build the Adapter application (Reading and Writing Files)

Steps
  1. Build the library
  2. Build a Mediation Module
  3. Assemble the mediation module
  4. Implement the mediation flow component
Step1 Build the library
Resources such as interfaces and business objects should be contained in libraries so that they can be easily shared by modules.
Start building the Stock Quote sample by creating the resources library and its contents.
  1. Create a library and call it Resources
  2. Import a WSDL file into the Resources library. Refer to WSDL file at right for further details.
  3. Create business objects (3) in the Resource library. Refer to business objects diagram at right for further details.
  4. Create an interface (1) called StockQuoteService in the Resource library. Refer to interface diagram at right for further details.
Completed Resource library
WSDL File
Note: use the wsdl file found in IBM Websphere Integration Developer 6.1.2.
E:Program FilesIBMSDP70Sharedpluginscom.ibm.wbit.samples.content_6.1.200.v20080615_0659artifactsstockquotewsdl
Business objects diagram
Interface diagram
Step2 Build a Mediation Module
Create a mediation module called StockQuote.
Configure it to use the Resources library and to generate a default mediation flow component called StockQuote_MediationFlow
Step3 Assemble the Mediation Module
Mediation modules contain assembly diagrams that are wired together to form the integrated service application.
The StockQuote mediation module consists of the following elements:

  1. StockQuoteService has a WSDL interface, called StockQuoteService, and uses SOAP/JMS web service binding so that the servlet front end can connect to the mediation module by using JAX-RPC. In this sample, you will create the StockQuoteService interface and generate the WSDL file.

    - Drag the Resources Interfaces StockQuoteService to the Assembly diagram to create an Export with no Binding component.

    - Rename the component to StockQuoteService.

    -Generate Web Services binding on StockQuoteService
  2. StockQuote_MediationFlow contains the mediation flow. In this sample, you will create and implement the StockQuote_MediationFlow component.
  3. RealtimeService has a web service binding and an interface that matches the real-time (premium) service. In this sample, you will import the WSDL file RealtimeService.wsdl.

    - Drag the Resources Interfaces RealtimeServicePortType to the Assembly diagram to create an Import with Web Service Binding component.

    - Rename the component to RealTimeService.

  4. DelayedService has a web service binding and an interface that matches the delayed (standard) service. In this sample, you will import the WSDL file DelayedService.wsdl.

    - Drag the Resources Interfaces DelayedServicePortType to the Assembly diagram to create an Import with Web Service Binding component.

    - Rename the component to DelayedService.


Step4

1 comment:

Anonymous said...

these tutorials are not usefull..
you just copied the ibm docs...