The Poster class takes care of doing the actual
remote procedure call. It does so in a separate thread and delivers the result
or error message asynchronously. This is very important to avoid any conflicts
that may appear if the call is done from the GUI thread.
To create this class:
Create the
class Poster.
Assign the
class to the example.capitals package and import the
required classes.
package example.capitals; import javax.xml.rpc.Stub; import java.rmi.RemoteException;
Set the class
to implement Runnable. Set up a listener that allows
the MIDlet to receive the results of asynchronous remote calls. See also class PosterListener.
Note that PosterListener is implemented in the class CapitalMIDlet.
/** * The Poster class does the call to the remote web service * It creates the stub and fills the parameters * The call is done in a separate thread and the results * are delivered asynchronously via PosterListener */ public class Poster implements Runnable { // true if IO thread is running, false if not private boolean isThreadRunning = false; // The listener private PosterListener listener; private String nation; private String endPoint; public Poster(PosterListener _listener, String _endPoint) { if (_listener == null) { throw new IllegalArgumentException("Listener cannot be null"); } endPoint = _endPoint; listener = _listener; }
Start a separate thread for handling the remote procedure call.
/** * Start the WebService call in a separate thread. * It checks that only one call is active at the same time */ public synchronized void requestCapital(String _country) { if (!isThreadRunning) { isThreadRunning = true; this.nation = _country; new Thread(this).start(); } }
Execute the IO operation in a separate thread.
/** * This is an IO operation and thus is executed in a * separate thread */ public void run() { CapitalBinding_Stub capitalServiceStub = new CapitalBinding_Stub(); capitalServiceStub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, endPoint); CapitalPortType capitalService = (CapitalPortType)capitalServiceStub; try { String capital = capitalService.getCapital(nation); listener.onCapitalRequestComplete(nation, capital); } catch (RemoteException e) { listener.onCapitalRequestError(e.getMessage()); } } }
This interface is used to deliver the results of the remote procedure
call or otherwise any errors produced during the call. CapitalMIDlet implements this and registers itself as the listener of Poster.
To create this class:
Create the
class PosterListener.
Assign the
class to the example.capitals package.
package example.capitals;
Implement the functionality of the interface.
/** * Implementers of this interface can request to be informed * of the results of the Web Service remote call asynchronously */ public interface PosterListener { /** * Called as a result of a successful call containing the nation * and the resulting capital */ public void onCapitalRequestComplete(String nation, String capital); /** * Called as a result of an error while calling the WebService */ public void onCapitalRequestError(String code); }