Many large software vendors are still developing their own SOA products. Therefore, the SOA field currently integrates various technologies, including Java Business Integration (JBI), Intelligent Event Processing, and Business Process Execution Language (BPEL) servers. Therefore, prior to integrating various technologies into a solution, user organizations that want to benefit from SOA may need to invest heavily. By having such a high level of complexity on SOA, the industry has paved the way for supplier positioning in advance, even if one of SOA's commitments is to implement standards-based, component-oriented, vendor-independent computing. Can a user organization gain some practical SOA user experience before starting a costly migration process?
To answer this question, this article demonstrates several important SOA principles through simple XML and some Java code. This article does not describe everything in the SOA field; instead, we will only introduce some key parts. For example, you can use RSS to publish XML service definitions. However, in this example, the transmission mechanism uses Java tools.
Java and XML
Java and XML have almost become synonyms. In fact, I used the IBM tool to apply the XML format to the entire article. Despite the obvious success of XML, you still need to note that XML is a very clumsy technology, such as adding a lot of bandwidth and Processing System overhead when data is transmitted over the network. However, ignoring this feature, XML does provide some powerful processing tools. These tools (two of which will be discussed in this article) basically solve the problem of data representation and parsing (the latter has been hard to solve for a long time ). Now, by using XML tools, programmers at various technical levels can achieve professional standardized parsing.
Java provides some powerful tools for XML data processing (see Java and XML on the sidebar ). If you think of the content in Listing 1 as an XML-based representation of a given dataset, you can also render it in other ways. The raw data that constitutes the basic content of Listing 1 is generally stored in the database. Therefore, how do you package the data into XML?
The advantage of this aggregation method is that Java developers in the user organization can use the concept to build their own simple experimental SOA. This pilot model helps organizations achieve the commercial benefits of SOA. The latter includes business service modeling, such as computing services, user self-service, better automation services, and more responsive services. You can use the preceding migration as an independent test to operate in parallel with existing business processes.
A user organization can implement a pilot SOA without large investment. In this way, the SOA requirements specific to the Organization can be achieved without any vendor. In fact, some smaller user organizations can continue to use their pilot SOA model and only migrate to large-scale commercial solutions provided by software vendors later.
Self-service Trend
For most service providers, self-service is evolving into a trend-especially those with insufficient funds. Therefore, if you need more bandwidth (to download or play online games), you can log on to the provider's Web site and automatically upgrade your connection to the provider through the Web page. Let's take a look at a specific example: Listing 1 shows a simple XML-based user service configuration file.
Listing 1. A simple XML-based service description
<ServiceInstance>
<Customer> Joseph Bloggs </Customer>
<Package> Internet </Package>
<Bandwidth> 1 mbps </Bandwidth>
<Downloadlimit> 1 Gbyte </downloadlimit>
<Uptime> 95 </uptime>
</Serviceinstance>
The Code explains the XML service model of the user. This model includes:
One service instance
CUSTOMER NAME
Service package name
Configured bandwidth
Download limit per month
Guarantee of normal running time of the provider
Undoubtedly, the service definition can be much more complicated than the example here. Other content may include the customer address, bill details, round-trip delay, encryption, and service credit information. The point is that more and more providers provide Web access details, as shown in Listing 1. To some extent, such attempts can reduce the cost and frequency of supporting calls. Interestingly, this web-based service can make users feel that they are a more advanced provider of services. This achieves a win-win situation, because customers can better access their service data, and providers can sell service packages without their intervention. Authorized users can modify some service parameters shown in Listing 1, such as the configured bandwidth. The monthly subscription fee is changed accordingly.
Therefore, the code in Listing 1 forms the basis of the XML-based service model. By simply interacting with online forms, you can modify writable service elements (such as bandwidth ). Modifications made through the online form will be recorded and then reflected in the backend services that can be modified by the user configuration file. This is a standard way to implement self-service.
However, what you will understand is another loosely coupled self-service-using this service, users can modify data by transmitting the XML content in Listing 1 over the network. In this scenario, the XML content transmitted can be modified through a Java client, which can run on a desktop, laptop, or even a device with limited resources (such as a mobile phone ), then, send the data back to the network service provider. This mechanism goes beyond the basic HTML page model and adopts the SOA idea.
Transmit service definition XML documents to clients using Java Technology
The content in Listing 2 is extracted from the Java file "encodeXML. java." included in this article. (the relevant files can be obtained from the download section ). The encodeXML. java class instantiates the object of the XMLEncoder class. As you can see, this object then creates a file named xmldata. xml in the current directory. The next step is to insert XML data values into the file, which can be achieved by calling a series of writeObject () methods (listing 2 describes this ). Obviously, in the production environment, hard-coded text strings in Listing 2 will come from persistent storage such as databases. In any case, we can see that the creation of XML data files is very simple.
Listing 2. encoding data in XML format
XMLEncoder e = new XMLEncoder (
New BufferedOutputStream (
New FileOutputStream ("xmldata. xml ")));
E. writeObject ("Joseph Bloggs ");
E. writeObject ("Internet ");
E. writeObject ("1 mbps ");
E. writeObject ("Gbyte ");
E. writeObject ("295 ");
E. close ();
After the program in Listing 2 is executed, a file named xmldata. xml will appear in the program execution directory. Listing 3 describes the content contained in the newly created file.
Listing 3. generated XML data
<? Xml version = "1.0" encoding = "UTF-8"?>
<Java version = "1.5.0 _ 06" class = "java. beans. XMLDecoder">
<String> Joseph Bloggs </string>
<String> Internet </string>
<String> 1 mbps </string>
<String> Gbyte </string>
<String> 295 </string>
</Java>
You can transmit the files in listing 3 to the client in the waiting state through the network-you can create them using Java technology. Listing 4 shows a simple example.
Listing 4. Transferring files across networks
Byte [] bytes = new byte [BUFFER_SIZE];
FileInputStream inputFile = null;
Try
{
File file = new File ("xmldata. xml ");
If (file. exists ())
{
InputFile = new FileInputStream (file );
Int ch = inputFile. read (bytes, 0, BUFFER_SIZE );
While (ch! =-1)
{
Output. write (bytes, 0, ch );
Ch = inputFile. read (bytes, 0, BUFFER_SIZE );
}
}
The code in Listing 4 creates a Length Buffer buffer_size. The value of the buffer_size constant can be 1024 or higher. Call inputfile. Read () to read the content of the input file (xmldata. XML) to the buffer. After buffering, the output. Write () method writes the file data to the socket of the outputstream object. The last step is to send data to the waiting client over the network. These functions only need so little code!
Next, you need to have the client process the incoming XML data.
The Java client receiving data obtains XML content (not an XML file)
How does the client receive XML data? Similarly, for Java technology, this is only a small matter. Data is received through a socket object. The code shown in listing 5 receives incoming data and pushes the data into the arraylist object.
The client must now solve two problems related to the number of received data items. Since this is a loosely coupled scenario, you must assume that the client does not know how many XML data items are contained in the service configuration file (the code in Listing 1. Therefore, you must determine some methods to receive and process precise data items. The second problem is easier to solve, that is, how to save the processed data. As you can see, listing 5 solves both of these problems.
Listing 5. Extracting embedded XML data
Xmldecoder d = new xmldecoder (input );
Try
{
While (true)
ArrayList <Object []> rowList = new ArrayList <Object []> ();
{
String dataItem = (String) d. readObject ();
System. out. println ("XML decoded data:" + dataItem );
RowList. add (dataItem );
}
}
}
Catch (Exception exc)
{
If (exc instanceof ArrayIndexOutOfBoundsException)
{
// No more records to process
System. out. println ("Parsed all XML records-" +
"Threw exception. Number of rows:" + rowList. size ());
}
}
D. close ();
By using a finite loop while (true), you can determine the number of expected incoming data items. This code runs cyclically until the last data item is received, and an exception (ArrayIndexOutOfBoundsException) is thrown ). You must use this exception mechanism unless the client has understood the expected number of data items.
XML data received from the InputStream object is stored in an object of the ArrayList class. This class is very useful for such applications. After defining a class, ArrayList has a specific capacity, which always matches the size of the underlying list. When an element is added, the capacity of the ArrayList object is automatically expanded. Therefore, you do not have to worry about exceeding the limit of the array because the class will handle this problem for you.
In this case, the client has a copy of the data in Listing 1. The client can now change the bandwidth element to the desired value, and then reverse execute the file transfer process and send it from the client to the server. By moving XML files from the server to the client, the client actually uses this service. The updated data is sent back to the server to complete the transaction. Of course, the service provider must verify the incoming data and provide the required bandwidth changes.
This mode described in this article first transfers an XML file to the client over the network. The client receives the file data as a stream and parses it into a memory resident object. The client then changes the memory resident object, and then executes the transmission process in reverse order to send the object back to the server.
There is also a service where XML data is completely transmitted from the server to the client. In this scenario, the client uses a certain form of File Transfer Protocol (such as FTP) to obtain a complete copy of the file. Since file transfer is a standard technology, we will not introduce it much here. You just need to know that the client will download a copy of the service configuration data in Listing 1. In this case, the client needs to parse and modify the file and transmit it back to the server. What is the working principle of this mode?
A Java Mechanism Based on XML files
The client now saves a copy of the service configuration file to the disk. The file must be parsed to extract XML data. Surprisingly, this is difficult to implement, especially for large files. The key to solving the problem is to use appropriate parsing tools. The tool used in this article is dom4j, which allows you to parse XML data into a Java object. You can also use a parser based on Simple API for XML (SAX), but SAX is a relatively low-level technology. As you can see, the dom4j tool only requires a small amount of work to complete the parsing. Listing 6 references the ProcessEventXml. java file included in this article, showing the main elements required to use dom4j to parse the file.
Listing 6. dom4j processing XML data
Try
{
Handler. treeWalk (handler. parse (new File (argv [0]);
}
Catch (Throwable t)
{
T. printStackTrace ();
}
}
Public Document parse (File url)
Throws into entexception
{
SAXReader reader = new SAXReader ();
Document document = reader. read (url );
Return document;
}
Public void treeWalk (Document document)
Throws Exception
{
TreeWalk (document. getRootElement ());
}
Basically, you only need two methods: parse () and treeWalk (). When I run compiled classes, I get the output shown in listing 7. If you want to run the code yourself, make sure to download and install the dom4j copy and add it to CLASSPATH (the last step is to add the corresponding JAR file to your CLASSPATH variable ). Then, compile the ProcessEventXml. java file and run the program using the following command:
Java ProcessEventXml ServiceDefinition. xml
Listing 7. Using dom4j to process XML files
Java ProcessEventXml ServiceDefinition. xml
Joseph Bloggs Internet 1 mbps 1 Gbyte 95
As you can see, only a small amount of work is required to display XML data neatly. These tasks are all handled by dom4j. In fact, most of the work is done through the treeWalk () method, which is a recursive method that is called only when the end of the file is reached. Here we show you a function of dom4j: Processing in memory. Note that this technology is not suitable for very large XML files, especially if your Java device is very small. However, in this example, the XML dataset is very small, so using this function will not cause problems.
Your file-based client has now successfully accessed XML data. The client can modify the data as needed and write a new XML file. Then it is transmitted back to the server for processing. As above, the client uses the service in this process.
Throws into entexception
{
SAXReader reader = new SAXReader ();
Document document = reader. read (url );
Return document;
}
Public void treeWalk (Document document)
Throws Exception
{
Treewalk (document. getrootelement ());
}
Basically, you only need two methods: parse () and treewalk (). When I run compiled classes, I get the output shown in listing 7. If you want to run the code yourself, make sure to download and install the dom4j copy and add it to classpath (the last step is to add the corresponding JAR file to your classpath variable ). Then, compile the processeventxml. Java file and run the program using the following command:
Java processeventxml servicedefinition. xml
Listing 7. Using dom4j to process XML files
Java processeventxml servicedefinition. xml
Joseph Bloggs Internet 1 Mbps 1 Gbyte 95
As you can see, only a small amount of work is required to display XML data neatly. These tasks are all handled by dom4j. In fact, most of the work is done through the treewalk () method, which is a recursive method that is called only when the end of the file is reached. Here we show you a function of dom4j: Processing in memory. Note that this technology is not suitable for very large XML files, especially if your Java device is very small. However, in this example, the XML dataset is very small, so using this function will not cause problems.
Your file-based client has now successfully accessed XML data. The client can modify the data as needed and write a new XML file. Then it is transmitted back to the server for processing. As above, the client uses the service in this process.