Access Java Events from .NET Using J-Integra® for .NET

Java/J2EE .NET Interoperability Products Page

This example demonstrates how to access Java Events from .NET client. J-Integra® for .NET is a Java interoperability component that bridges Java/J2EE and Microsoft .NET. It provides bi-directional access of Java objects and .NET components.

Synopsis

Use this example as a guideline to show you how to access Java events using J-Integra for .NET.

This example illustrates a very simple multi-client chat program. Each .NET client will access a Java Singleton and add itself as a listener. An event occurs when a message is typed by one of the .NET clients. Each message is then sent to every listening .NET client.

The Steps to Follow will show you how to deploy J-Integra® for .NET correctly for this example. Refer to the diagram below for an overview of the scenario:

Access Java Events from .NET Using J-Integra® for .NET

Note: $USER_INSTALL_DIR$ is the directory where J-Integra for .NET has been installed. If this location contains whitespace characters (e.g. C:\Program Files\...), you may need to include it within "" when referencing it at the command-line.

Prerequisites

  1. A platform with Microsoft Visual Studio .NET 2003 (7.1.3088 or higher) installed, or at least the .NET Framework 1.0.3705 or higher. This will be known throughout the example as the .NET platform.

  2. A platform with J-Integra for .NET installed. This will be known throughout the example as the Java platform.

  3. A version of JDK (1.3.1 or higher) installed on the Java platform, and the bin directory included in the path. This required to compile and run the Java server.

  4. Verify that GenService is installed correctly on the .NET platform.

Steps to Follow

  1. Run GenNet and Compile the Proxies

  2. Compile the .NET Client

  3. Run GenJava and Compile the Proxies

  4. Run Janetor to Configure J-Integra for .NET

  5. Run the J-Integra® for .NET Server

  6. Compile and Run the .NET Client(s)

Run GenNet and Compile the Proxies

Perform this step on the Java platform.

In this step, you will use the GenNet tool to generate .NET proxies for the Java classes.

Note: GenNet saves configuration details to an XML file. For your convenience, the settings described below have already been saved to the file gennet.xml in the directory $USER_INSTALL_DIR$\examples\csharp_java_events. If you'd prefer not to manually enter the settings as instructed below, simply copy this file over to $USER_INSTALL_DIR$ before starting GenNet.
  1. The first step is to compile the Java source. At the command-line, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events and enter:

    javac -classpath . java_server\*.java
    
  2. Windows

    Launch GenNet from the Start Menu.

    Command-Line

    Change directory to $USER_INSTALL_DIR$ and then enter:

    java -jar lib\gennet.jar
    
  3. The first screen of GenNet shows a list of machines that have GenService running (screen shot not shown). Select the appropriate one and click Next.

  4. The next screen prompts for the components (JAR files or directories) to be added to the CLASSPATH. Each of these components are required in order for the Java runtime to resolve references to Java classes when generating .NET proxies. You'll need to add the following component:

    - $USER_INSTALL_DIR$\examples\csharp_java_events

    Using GenNet generate .NET assemblies for Java objects

    Move on to the next step by clicking Next.

  5. The next screen prompts for the specific Java classes that will have .NET proxies generated for them. Click on Browse... to add the following classes (use the CTRL key to select multiple classes):

    - java_server.Message
    - java_server.Messenger
    - java_server.MyEventListener

    Make java_server.Message a "marshal-by-value" object by clicking on Options... and checking Pass by value.

    Select Java classes to be exported to .NET in GenNet

    Move on to the next step by clicking Next.

  6. The next screen prompts you to specify the .NET Assembly name along with the output directory. The screen shot may show a different output directory than the value of Output Directory shown here, but it should be pointing to $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Debug.

    Setup .NET assembly name and output directory in GenNET

  7. You should see a message that the proxy generation was successful. Click Exit to exit GenNet.

    GenNet proxy generation successful

Compile the .NET Client

Perform this step on the .NET platform.

In this step, you will need to compile the .NET client code.

  1. Load Client.sln located in $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client with Visual Studio .NET 2003. Press Ctrl + Shift + B to build. If successful, this should build Client.exe in the bin\Debug directory.

    Note: If you happen to have an older version of Visual Studio .NET, it is still possible to compile the code, but you will not be able to open the solution file. See build.bat in $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client.

Run GenJava and Compile the Proxies

Perform this step on the Java platform.

In this step, you will use the GenJava tool to generate Java proxies from the .NET Assembly. These proxies are used when a message event is fired and J-Integra for .NET calls the event handler of each subscribed .NET client.

Note: GenJava saves configuration details to an XML file. For your convenience, the settings described below have already been saved to the file genjava.xml in the directory $USER_INSTALL_DIR$\examples\csharp_java_events. If you'd prefer not to manually enter the settings as instructed below, simply copy this file over to $USER_INSTALL_DIR$ before starting GenJava.
  1. Windows

    Launch GenJava from the Start Menu.

    Command-Line

    Change directory to $USER_INSTALL_DIR$ and then enter:

    java -jar lib\genjava.jar com.intrinsyc.janet.genjava.GenJavaApp		
    
  2. The first screen of GenJava prompts for the location of the .NET assembly along with the output directory where the Java proxies will go. They should be (respectively):

    - $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Client.exe
    - $USER_INSTALL_DIR$\examples\csharp_java_events

    Setup source assembly name and output directory for Java proxy in GenJava

    Move on to the next step by clicking Next.

  3. You should see a message that the proxy generation was successful. Click Finish to exit GenJava.

    GenJava Java Proxies for Remote Objects generation successful

  4. To compile the Java proxies, at the command-line, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events and enter:

    javac -classpath $USER_INSTALL_DIR$\lib\janet.jar;. csharp_client\*.java
    

Run Janetor to Configure J-Integra® for .NET

Perform this step on the Java platform.

In this step, you will need to configure J-Integra for .NET as a server so it can respond to incoming TCP requests. This is done through the Janetor tool.

  1. Windows

    Launch Janetor from the Windows Start Menu.

    Command-Line
    java -jar $USER_INSTALL_DIR$\lib\janetor.jar
    
  2. Go to File - Open and open janet.xml located in $USER_INSTALL_DIR$\csharp_java_events.

  3. Install your J-Integra for .NET product license. See Installing the J-Integra® for .NET license for further information.

  4. Select java_server.Messenger under Local Objects. Here you see that J-Integra for .NET exposes this class as a Singleton at the given machine address and port using TCP. The default node above exposes the remaining classes with the same configuration details, except that they are not Singletons.

    Setup local objects in Janetor configuration tool

  5. Select default under Remote Objects. Here you see that J-Integra for .NET is configured as a client to access a CAO using TCP binary. Notice that URI is blank - this is because there is no one specific endpoint for J-Integra for .NET to call the .NET client's event handler. This is fine, since the client will pass over the correct URI when it first accesses the J-Integra® for .NET server.

    Setup default remote objects in Janetor configuration tool

  6. Before you exit Janetor, save the configuration by going to File - Save.

Run the J-Integra® for .NET Server

Perform this step on the Java platform.

In this step, start the J-Integra for .NET server.

  1. At the command-line, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events and enter:

    java -cp $USER_INSTALL_DIR$\lib\janet.jar;. com.intrinsyc.janet.Main
    

Compile and Run the .NET Client

Perform this step on the .NET platform.

In this final step, you will configure the .NET client(s) using a configuration file, then compile and run it.

  1. Modify remoting_tcp.config in $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Debug to look like:

    <configuration>
     <system.runtime.remoting>
       <application>
         <client>
           <wellknown type="java_server.Messenger, JanetExample" url="tcp://localhost:8765/java_server.Messenger.soap"/>
         </client>
         <channels>
            <channel port="0" ref="tcp"/>
         </channels>
       </application>
     </system.runtime.remoting>
    </configuration>
    

    Notice that tcp://localhost:8765 matches what was entered under Local Objects in Janetor. Change accordingly if necessary.

  2. We will now launch two different chat clients. In general, the syntax is:

    Client [username]		
    

    Launch a command prompt, change directory to $USER_INSTALL_DIR$\examples\csharp_java_events\csharp_client\bin\Debug and enter:

    Client Tom
    

    Launch another command prompt, and in the same directory as above, enter:

    Client Jen		
    
  3. At this point, you should see two different client processes started. Type a message in one of the clients and you will see that all clients receive it:

    Client A window - Access Java Events from .NET

    Client B window - Access Java Events from .NET

  4. After a client sends a message, take a look at the J-Integra for .NET server console. You should see the message received and sent back out:

    Sending "Hi Tom" from Jen to 2 clients.
    Sending "Hi Jen, how are you?" from Tom to 2 clients.		
    

© 2007 Intrinsyc Software International, Inc. All rights reserved. Legal