Here's the actual client application code for the XMLBeans version of the example, which uses its own generated data object classes with a wrapped interface:
public class WebServiceClient
{
public static void main(String[] args) throws Exception { // allow override of target address
String host = args.length > 0 ? args[0] : "localhost";
String port = args.length > 1 ? args[1] : "8080";
String target = "http://" + host + ":" + port +
"/axis2/services/xmlbeans-library"; // create the client stub
XmlbeansLibraryStub stub = new XmlbeansLibraryStub(target); // retrieve a book directly
String isbn = "0061020052";
GetBookDocument gbd = GetBookDocument.Factory.newInstance();
GetBookDocument.GetBook gb = gbd.addNewGetBook();
gb.setIsbn(isbn);
gbd.setGetBook(gb);
GetBookResponseDocument gbrd = stub.getBook(gbd);
BookInformation book = gbrd.getGetBookResponse().getGetBookReturn();
if (book == null) {
System.out.println("No book found with ISBN '" + isbn + ''');
} else {
System.out.println("Retrieved '" + book.getTitle() + ''');
} // retrieve the list of types defined
GetTypesDocument gtd = GetTypesDocument.Factory.newInstance();
gtd.addNewGetTypes();
GetTypesResponseDocument gtrd = stub.getTypes(gtd);
TypeInformation[] types =
gtrd.getGetTypesResponse().getGetTypesReturnArray();
System.out.println("Retrieved " + types.length + " types:");
for (int i = 0; i < types.length; i++) {
System.out.println(" '" + types[i].getName() + "' with " +
types[i].getCount() + " books");
} // add a new book
String title = "The Dragon Never Sleeps";
isbn = "0445203498";
try {
AddBookDocument abd = AddBookDocument.Factory.newInstance();
AddBookDocument.AddBook ab = abd.addNewAddBook();
ab.setAuthorArray(new String[] { "Cook, Glen" });
ab.setIsbn(isbn);
ab.setTitle(title);
ab.setType("scifi");
stub.addBook(abd);
System.out.println("Added '" + title + ''');
title = "This Should Not Work";
ab.setTitle(title);
stub.addBook(abd);
System.out.println("Added duplicate book - should not happen!");
} catch (AddDuplicateFaultException e) {
System.out.println("Failed adding '" + title +
"' with ISBN '" + isbn + "' - matches existing title '" +
e.getFaultMessage().getAddDuplicate().getBook().getTitle() +
''');
} // create a callback instance
BooksByTypeCallback cb = new BooksByTypeCallback(); // retrieve all books of a type asynchronously
GetBooksByTypeDocument gbtd =
GetBooksByTypeDocument.Factory.newInstance();
gbtd.addNewGetBooksByType().setType("scifi");
stub.startgetBooksByType(gbtd, cb);
long start = System.currentTimeMillis();
synchronized (cb) {
while (!cb.m_done) {
try {
cb.wait(100L);
} catch (Exception e) {}
}
}
BookInformation[] books = cb.m_books;
System.out.println("Asynchronous operation took " +
(System.currentTimeMillis()-start) + " millis");
if (cb.m_books != null) {
System.out.println("Retrieved " + books.length +
" books of type 'scifi':");
for (int i = 0; i < books.length; i++) {
System.out.println(" '" + books[i].getTitle() + ''');
}
} else {
System.out.println("Returned exception:");
cb.m_exception.printStackTrace(System.out);
}
} public static class BooksByTypeCallback extends XmlbeansLibraryCallbackHandler
{
private boolean m_done;
private Exception m_exception;
private BookInformation[] m_books; public void receiveResultgetBooksByType(GetBooksByTypeResponseDocument resp) {
m_books = resp.getGetBooksByTypeResponse().getGetBooksByTypeReturnArray();
m_done = true;
} public synchronized void receiveErrorgetBooksByType(Exception e) {
m_done = true;
}
};
}
The techniques needed for working with XMLBeans can be confusing to new users, with multiple layers of factories and wrapper classes involved. Look at the code following the "retrieve the list of types defined" and "add a new book" comments, for instance, and compare that with the corresponding
JiBX client and
ADB client code.