Build a distributed system with Java and XML

xiaoxiao2021-03-06  19

Scalable Markup Language (XML) As a simple, neutral, readable data representation has become more and more popular, and many software vendors announced that their products will be generated or Process XML data. XML is also seen as the best format between re-exchange data. It allows companies to match the document type definition of the XML of the exchanged data (SCHEMA). These DTDS or Schema are database modes that are independent of the enterprise.

This article will use the construction method of communication and processing XML data between different computers, mainly to run XML communication between Java applications on different virtual machines. XML communication

The World Wide Web Consortium (W3C) defines the syntax and semantics of XML in the XML specification. In order to process XML data, the XML document must be parsed. W3C defines a document object model (DOM), which is an interface for applying XML data. The DOM already has the implementation of many languages ​​including Java. Java applications can access XML data through the DOM API. The XML parser will generate the DOM representation of the XML document. Figure 1 illustrates a simple model of the Java distributed application for processing an XML document. This model assumes that data can be obtained from a data source such as a relational database. Java code processing data and eventually produces a DOM representation, which is represented as a processor in the figure.

The processor code passes the XML data represented by the DOM to the sender. The sender is a Java code with the recipient XML data communication. The recipient Java code is to accept XML data, generate data represented by the DOM and transmit it to another processor. In short, the sender and the recipient abstract the communication of the XML data represented by the DOM.

The sender and the recipient is not performed on the same Java virtual machine. They are connected by components of a distributed system. Whether the recipient or senders are both the client and the server, both data transfer is two-way. Xbeans

As will be seen, the three implementations of the senders and recipients described herein are implemented by XBeans. Xbeans is a software component that accepts XML data as input and processes this input and then outputs XML results to the next XBeans. The XBeans's input and outputs are XML DOM documents, i.e., is transmitted to the XBeans string that requires an XML parser parsing, but is parsed by the W3C's standard DOM API to document objects. Figure 2 illustrates an XBeans.

Xbeans is JavaBeans, support packaging, reuse, connection, and customized Java code. Through proper Some XBeans and JavaBeans design tools, we can build very useful distributed applications. Xbeans comes from IBM's XML Java Development Tools, on which a small amount of modifications are made to make it more suitable for distributed applications. Xbeans can get free of charge from www.xbeans.org open source items. Realize senders and recipients

Three different methods of implementing senders and recipients will be described below. Then make a simple analysis for each method.

Method 1: Use standard web servers

This method will only simply send XML as a web server to a remote computer. The sender must convert the XML of the DOM to text to communicate with the recipient. Then, the acceptor must restore the text to the DOM, as shown in Figure 3:

The following code segments use HTTP to implement the sender. Here, the DomWriter class in the IBM Java development package is used to implement the DOM representation to the conversion of text XML.

public void documentReady (DOMEvent evt) throws XbeansException {try {URL receiver = new URL (getRemoteURL ()); URLConnection receiverConnection = receiver.openConnection (); receiverConnection.setDoOutput (true); // open an output stream is then sent to the sender text XML data OutputStream out = receiverConnection.getOutputStream (); DOMWriter writer = new DOMWriter (); writer.setPrintWriter (new PrintWriter (out)); writer.documentReady (new com.ibm.xml.xpk4j.dom.DOMEvent ( This, evt.getdocument ()); out.close (); // Open an input stream for the result BUFFEREDReader in = New buffReader (ReceiverConnection.getInputStream ()))))); // Process: "OK" means Successfully; "exception" means input streaming serialization exception ... in .close ();} catch (throwable e) {E.PrintStackTrace (System.err);}} noticed the above documentReady () method with RemoteURL properties Get the URL of the CGI script on the server. In order to compatibility with HTTP, the CGI script class is a string "Content-Type: Text / HTML" package recipient's output. This script then calls the ReceiverMain () method on the server. The main () function is just a simple instantiated recipient and then calls its ReceiveDocument () method. import org.xbeans.communication.stdio.receiver *;. public class receiverMain {static Bean theReceiver = new Bean (); public static void main (String [] args) {theReceiver.receiveDocument ();}} final receiveDocument () method The code segment will regenerate the DOM representation for further processing. The IBM XML parser is used here.

DOMPARSER PARSER = new domparser (); // Configuration parser try {// Call parser Parser.Pars (New INPUTSOURCE (ISTEM.IN));} catch (throwable e) {throw new xbeansexception ("", "receiver" "IO Error Parsing Incoming Document", "IO Error Parsing Incoming Document" E);} // Transfer the document to the next bean Domlistener.DocumentReady (this, Parser.getDocument ())); method 2: Call serialization document via Java remote method

This method transmits an XML DOM document from the sender through the Java remote method call (Java RMI) and DOM Serialization. Figure 4:

The following code is implemented using JAVA Remote Method Invocation communication .public void documentReady sender and the recipient (DOMEvent evt) throws XbeansException {if (DOMListener == null) {try {DOMListener = (DOMListener) Naming.lookup (getReceiverName ()); Catch (Exception E) {throw new xbeansexception (evt.getdocument (). getnodename (), "sender", "Error Obtaining Remote Receiver", "The Name May Be Wrong or the Network May BE Down.");}} Domlistener.documentReady (evt);

The following is an implementation of the acceptor's Java remote method call. The setName () method will accept this transfer to RMI registration (registry),

The DocumentReady () method only transmits the received document to the next component. public void setReceiverName (String newName) {try {if (receiverName = null!) Naming.unbind (receiverName); receiverName = newName; Naming.rebind (receiverName, this);} catch (Exception e) {System.out.println ( e);}} public void documentReady (Document incomingDocument) throws RemoteException, XbeansException {if (dOMListener == null) {throw new XbeansException (incomingDocument.getNodeName (), "rmiReceiver", "next component not established", "The component needs To be configured. ");} Domlistener.documentReady (New Domevent (INCOMINGDocument);} Method 3: CORBA-IIOP

The third method transmits data with CORBA-IIOP (CORBA over Internet Inter-ORB Protocol). Object Management Organization (OMG) is proposed to extend the interface definition language (IDL) to include XML data types. In this way, the CORBA product will be able to transmit XML data in the future. As shown in Figure 5:

The following OMG IDL gives the interface implemented by the sender and the recipient CORBA.

exception RemoteReceiverException {string remoteIdentifier; string documentName; string componentName; string message; string moreMessage;}; typedef sequence byteArray; interface XMLReceiver {void documentReady (in byteArray serializedDocument) raises (RemoteReceiverException);}; JAVA serial code in the following Dom and CORBA implementation senders. public void documentReady (DOMEvent evt) throws XbeansException {Document documentToSend = evt.getDocument (); try {ByteArrayOutputStream bastream = new ByteArrayOutputStream (); ObjectOutputStream p = new ObjectOutputStream (bastream); p.writeObject (documentToSend); p.flush () ; org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (new String [0], System.getProperties ()); XMLReceiver receiver = urlToObject (orb, getReceiverURL ()); receiver.documentReady (bastream .toByteArray ());} catch (RemoteReceiverException rre) {throw new XbeansException (rre.remoteIdentifier, rre.documentName, rre.componentName, rre.message, rre.moreMessage);} catch (Throwable e) {throw new XbeansException ( " "," Sender "," Error Sending Document " E," Error Sending Document " E);}} The following code uses Java serialization DOM and CORBA to implement the recipient.

public void documentReady (byte [] serializedDocument) throws RemoteReceiverException {// anti serialized byte stream ByteArrayInputStream bais = new ByteArrayInputStream (serializedDocument); Document theDocument; try {ObjectInputStream ois = new ObjectInputStream (bais); theDocument = (Document) ois .readObject ();} catch (Throwable e) {throw new RemoteReceiverException (corbaName, "incoming document", "receiver", "error deserializing document", "error deserializing document" e);} try {// the document transfer the listener local.DOMListener.documentReady (new DOMEvent (this, theDocument));} catch (XbeansException xbe) {throw new RemoteReceiverException (xbe.remoteIdentifier (), xbe.documentName (), xbe.componentName (), xbe.message (), Xbe.moreMessage ());}} The following table summarizes the excellent ( ) lack of various methods:

Test indicates that the XML representation of plain text is better than DOM serialization. At the same time, the time used to resolve DOM and text is also less than the time used by Java direct serialization and method.

The advantage of standard web server mode is that its application basis should be a wide range. The CGI script can run on the vast majority of web servers, and the reception can be easily identified by the URL. For RMI, RMI registration is required. Corba's solution requires the Object Request Broker, ORB, and the implementation of the CORBA sender is used by a URL's name model instead of the recipient's CORBA object reference, with a string and A URL is connected, then transforms at the client.

Corba and RMI support the Java client to the Java server solution. No CGI scripts do not need to read coding exceptions from standard inputs. Moreover, it is not necessary to start a Java virtual machine when the sender uses XML communication. They both support automatic activation of the recipient.

The Java RMI mode can only work between Java code, and for web servers, including CORBA, in any programming language. For Java Serialized DOM, even if the client and server side needs to be a Java code, there is another difficulty, that is, Java serialization requires the same DOM. achieve. in conclusion

As mentioned above, there are many ways to implement XML data in Java distributed applications, each of which is different. What is important is to extract XML messages from a distributed application logic. That is, implementation of the code that transmits and accepts XML should be separated from the code of the application logic. By packing the code into a software component, you can change the sender and the acceptor's code without affecting the rest of the application.

转载请注明原文地址:https://www.9cbs.com/read-71604.html

New Post(0)