Passing Java objects by value

Introduction

When referencing Java objects from COM clients using J-Integra®'s DCOM mode, the COM client will obtain a reference to the Java object and each subsequent access to the Java object results in at least one round trip communication between the client and the server. In the case of many objects being accessed by the COM client, a significant performance overhead may occur.

To avoid this J-Integra® also offers the possibility to treat a Java object as a COM struct. This means that public members of the Java object will be passed by value to the COM client, not by reference, and access to these data members by the COM client requires no further network communication. This is the case for both single Java objects and arrays of Java objects.

Currently the following restrictions apply to the members of a Java class which may be passed by value:

Other restrictions include:

Example usage

This example is not a complete step-by-step example, it presumes that you have already used J-Integra® to access Java objects from Visual Basic using early binding and that you are familiar with the steps. If this is not the case then you should attempt this first. Once you have completed a Visual Basic to Java early binding example, the following should make sense.

If you wish to pass Java classes by value to Visual Basic clients, this outlines how to proceed. There is also a C++ example in the J-Integra® Knowledge Base.

  1. Run java2com with the following Syntax:

    java -DJINTEGRA_CLASSES_BY_VALUE=<classes to be passed by value> com.linar.java2com.Main
     
    where <classes to be passed by value> is a semicolon separated list of classes which you require to be passed by value.
     
    Assuming I have the following Java class:

    public class Person {
      public String name;

      public java.util.Date dateOfBirth;

      public String addressLine1;

      public String addressLine2;

      public int zip;

      public String city;

    }

    I would run java2com as follows:
    java -DJINTEGRA_CLASSES_BY_VALUE=Person com.linar.java2com.Main

  2. Use java2com as usual, and compile all the generated IID*.java files.

  3. Also compile the generated IDL file using midl, use the /mktyplib203 option:
    midl /mktyplib203 MyIDL.idl

  4. Copy the generated type library (.tlb file) to the machine where the COM client resides and register the type library using the regtlb command.

  5. Assuming I have the following Java class whose methods return Java objects of type Person:

    public class PeopleFinder {
      public Person getOldestPerson(){

           //some implementation

      }

      public Person[] getAllPeople(){

           //some implementation

      }

    }

    Then my Visual Basic code to use this class to obtain the Person Java objects returned by the methods getOldestPerson and  getAllPeople by value could look like:

    Set Finder = GetObject("myjvm:PeopleFinder")

    Dim oldest As Variant

    oldest = Finder.getOldestPerson

    MsgBox "Oldest person was born on " & oldest.dateOfBirth


    Dim allPeople As Variant

    allPeople = Finder.getAllPeople

    For i = 0 To UBound(allPeople, 1)

      MsgBox "Person " & i & " is called " & allPeople(i).name

    Next