Tips and tricks for using JiBX
start > axis2-jibx > example > jibx-client

jibx-client

Created by dsosnoski. Last edited by dsosnoski, one year and 111 days ago. Viewed 2,008 times. #4
Here's the actual client application code for the JiBX version of the example, which uses predefined data object classes:
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/jibx-library";

// create the server instance JibxLibraryStub stub = new JibxLibraryStub(target);

// retrieve a book directly String isbn = "0061020052"; Book book = stub.getBook(isbn); 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 Type[] types = stub.getTypes(); 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 { stub.addBook("scifi", isbn, new String[] { "Cook, Glen" }, title); System.out.println("Added '" + title + '''); title = "This Should Not Work"; stub.addBook("xml", isbn, new String[] { "Nobody, Ima" }, title); 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().getBook().getTitle() + '''); }

// create a callback instance BooksByTypeCallback cb = new BooksByTypeCallback();

// retrieve all books of a type asynchronously stub.startgetBooksByType("scifi", cb); long start = System.currentTimeMillis(); synchronized (cb) { while (!cb.m_done) { try { cb.wait(100L); } catch (Exception e) {} } } Book[] 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 JibxLibraryCallbackHandler { private boolean m_done; private Exception m_exception; private Book[] m_books;

public synchronized void receiveResultgetBooksByType(Book[] resp) { m_books = resp; m_done = true; }

public synchronized void receiveErrorgetBooksByType(Exception e) { m_done = true; } }; }

JiBX unwrapping makes the interface easy to use, and working with existing data classes can be a major advantage in some cases. By way of comparison, look at the code following the "retrieve the list of types defined" and "add a new book" comments and compare that with the corresponding ADB client and XMLBeans client code.
no comments
snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt