Developer Resources:

Customer Quotes:

Remote Java method invocation from Silverlight

This article reviews an example of creating a Silverlight client and integrating it with WebORB for Java. At the end of the article, you will have a working Silverlight application which makes a remoting invocation of a Java object hosted inside of WebORB. The invoked server-side method runs a database query and returns a result as an instance of java.sql.ResultSet. The Silverlight client receives the response, adapts it to an array of objects, each representing a row in the result set and then renders it in a Silverlight DataGrid component using data binding. The same identical approach can be used to invoke any Java service type supported by WebORB, including EJBs, Spring Beans and 3rd party web services.

Requirements

  • WebORB for Java 3.0.2 or newer
  • JDK 1.5 or above
  • Visual Studio 2008

Download and install WebORB for Java. The installer provides an option for setting up Visual Studio template for developing Silverlight applications with WebORB. Make sure to keep the option selected as shown in the image below:

Running WebORB

WebORB for Java can be integrated into any Java EE application server or Servlet Engine. To keep things simple, this article assumes that WebORB is running standalone. However the integration demonstrated in the article does not change when WebORB is deployed in another container.

Open a command prompt window, change the directory to the folder where WebORB is installer and run the following command to start the server:

java -jar weborb.jar

Creating Visual Studio Project

Start Visual Studio 2008 and select File > New > Project. In the New Project window, select Silverlight under Visual C# or Visual Basic. One of the available template is going to be "Silverlight Application with WebORB" as shown in the image below. Select the template, enter a name for the project and click OK. (This article uses the 'SL2Java' project name).

Silverlight application can be loaded into a browser from a web server or the file system. Visual Studio simplifies the deployment and offers a choice of creating a web project which will contain an HTML page referencing your Silverlight application. The popup shown below provides a choice for the configuration. Select the first option as shown in the image below and click OK.

Once the project is created, its structure should be the same as shown in the image below:

Adding Client Code

The client application will consist of a button and a datagrid. The button's Click event will send a remoting request to a Java object. The datagrid will render data received from the server-side. Double-click Page.xaml and add the following XAML code:

<UserControl
   xmlns
:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

   x
:Class
="SL2Java.Page"
  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns
:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Width
="700" Height
="500">
  
<Grid x:Name="LayoutRoot" Background="White">
    
<Grid.RowDefinitions>
    
<RowDefinition Height="30" />
    
<RowDefinition Height="*" />
  
</Grid.RowDefinitions>

  
<Button x:Name="DataLoaderButton" Grid.Row="0"
     Content
="Load Data From Java"

     Click
="DataLoaderButton_Click" />

  
<data:DataGrid
     Grid.Row
="1"
     x:Name="customersGrid"
     AlternatingRowBackground="Beige"
     AutoGenerateColumns="True"
     CanUserResizeColumns
="True"
     Width="700" Height="450" />
 
</Grid>
</
UserControl>

Expand the Page.xaml node in the Solution Explorer and double-click Page.xaml.cs to open up the code view. Since the project is created from a WebORB template, it already includes a sample of making a remoting invocation. Modify the code as shown below (assuming you are developing in C#):

using System;
using
System.Collections.Generic;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Browser;
using
Weborb.Client;

namespace
SL2Java
{

  public
partial class Page :
UserControl
  {

    private
WeborbClient
proxy;

    public
Page()
    {
      InitializeComponent();
      proxy =
new WeborbClient( App.WeborbURL, this );
    }


    private
void DataLoaderButton_Click( object sender, RoutedEventArgs
e )
    {
      Responder<List<Customer>> responder = new Responder<List<Customer
>>( GotCustomers, GotError );
      proxy.Invoke( "weborb.examples.DataBinding", "getCustomers", null, responder );
    }

   
public void GotCustomers( List<Customer> customers )
    {
      customersGrid.ItemsSource = customers;
    }

   
public void GotError( Fault faultObject )
    {
      HtmlPage.Window.Alert( "Got error from remote method invocation - "
+ faultObject.Message );
    }
  }


  public
class
Customer
  {

    public
string CONTACTNAME { get; set
; }
    public
string COMPANYNAME { get; set
; }
    public
string CONTACTTITLE { get; set
; }
    public
string COUNTRY { get; set
; }
    public
string CITY { get; set
; }
  }
}

The code is fairly straight-forward, however, some things may be worth clarifying:

  1. An instance of the Customer class represents a row from the returned ResultSet.
  2. All Silverlight to Java communication is handled by the WeborbClient API. When an instance of the WeborbClient class is created it accepts the URL for the WebORB server. The URL is defined in App.xaml.cs
  3. The remoting invocation takes place in the DataLoaderButton_Click event handler. It invoikes the "getCustomers" method in the "weborb.examples.DataBinding" class. The method takes no arguments, thus the third parameter in the Invoke method is null.
  4. The Responder class is a generic type. The type it is created with represents the type of the returned value. WebORB Client API automatically adapts the data it receives from the client to the type specified in the Responder generic. In this example the type adaptation is done between Java's ResultSet and .NET's List<Customer>
  5. The Responder constructor accepts two function references: one for the successful invocation of the remoting call (GotCustomers) and the other is a fault handler (GotError)
  6. The argument in the successful invocation handler (GotCustomers), must be the same type as the type used in the Responder generic. In this example it is List<Customer>.

Running the Silverlight client

Before running the example, it is important to deploy this and this files into the root of the WebORB for Java web server. The reason is the Silverlight client is loaded from one web server and will send invocation requests to another one. The files must be copied into the webapp folder located in the root of the WebORB for Java installation (see the image below):

Alternatively, the Silverlight client application can be deployed into the WebORB for Java server and thus avoid additional policy files.

Compile and run the Silverlight application. Once it runs, click the "Load Data From Java" button. Silverlight makes a remoting invocation of a java object and renders the received response in the datagrid as shown below:

Download complete Silverlight to Java project.

Debugging client server communications

Occasionally it may be important to understand what goes on between the client Silverlight application and the server-side objects to diagnose communication or application logic problems. A special tool which enables monitoring and debugging of client/server traffic is RIA AppPuncher. The product runs a proxy server and visualizes all the requests and responses a client sends and receives. For a full list of features see the following page:
http://www.themidnightcoders.com/products/ria-apppuncher/debugger-edition.html