Remote Interfaces
In order to access your business logic from the web, you'll need to use a remote interface. These are generated by JsOrb on the fly by using the<jsorb:importinterface/> tag.
For example, if you have an interface (which you configured) named
com.foo.BarManager, like this:
public interface BarManager {
public Bar getBar(Integer barId);
public void saveBar(Bar bar);
}
There are no limits on what you can put in your interface, other
than whatever parameters and return values you have must be POJOs
as previously
described. You can have JsOrb generate a remote proxy to that
class by inserting the following in your JSP:
<jsorb:importinterface entity="com.foo.BarManager"/>This generates a proxy which you can use to access your business logic residing on the application server. To use the remote proxy, you first need to instantiate it:
var barManager = new com.foo.BarManager();This creates an instance of a remote proxy to the BarManager instance running on your application server. This proxy manages all of the AJAX semantics necessary to transport your parameters to the application server, invoke whichever method you choose, unmarshall any return value, and call an error handler for you. Using the proxy is simple, with one caveat that owes to the asynchronous aspect of AJAX:
var handler = new js.rmi.ResponseHandler();
handler.onSuccess = function(bar) {
alert(bar.getName());
};
barManager.getBar(123, handler);
Because AJAX is by nature asynchronous, you need to supply a
callback that the remote proxy can invoke when your call has
finished executing. JsOrb provides a wrapper class called
js.rmi.ResponseHandler for this purpose. You simply
set the onSuccess method of your ResponseHandler to
point to a function which takes as a parameter whatever your
interface method returns. In the example above, we're invoking
BarManager.getBar() which returns a Bar
instance, which is passed to our handler's onSuccess
method when the call completes successfully.
Error Handling
Let's face it: stuff happens. And it's going to happen to your web application. In any distributed computing environment, there can be issues with the network, the remote server, remote databases, and any number of other points of failure. If an error occurs during a call to a remote proxy, JsOrb will invoke your handler'sonFailure method. By default, onFailure
just throws the error (which you can catch using
window.onerror), but you can override this behavior in
other ways too:
handler.onFailure = function(e) {
alert("Error occurred: " + e);
};